]> Cypherpunks.ru repositories - gocheese.git/blob - hr.go
Raised copyright years
[gocheese.git] / hr.go
1 /*
2 GoCheese -- Python private package repository and caching proxy
3 Copyright (C) 2019-2022 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "bytes"
22         _ "embed"
23         "html/template"
24         "io/ioutil"
25         "log"
26         "net/http"
27         "os"
28         "sort"
29         "strings"
30 )
31
32 var (
33         //go:embed hr-root.tmpl
34         HRRootTmplRaw string
35         HRRootTmpl    = template.Must(template.New("hr-root").Parse(HRRootTmplRaw))
36
37         //go:embed hr-pkg.tmpl
38         HRPkgTmplRaw string
39         HRPkgTmpl    = template.Must(template.New("hr-pkg").Parse(HRPkgTmplRaw))
40 )
41
42 func serveHRRoot(w http.ResponseWriter, r *http.Request) {
43         files, err := ioutil.ReadDir(Root)
44         if err != nil {
45                 log.Println("error", r.RemoteAddr, "hr-root", err)
46                 http.Error(w, err.Error(), http.StatusInternalServerError)
47                 return
48         }
49         packages := make([]string, 0, len(files))
50         for _, f := range files {
51                 packages = append(packages, f.Name())
52         }
53         sort.Strings(packages)
54         var buf bytes.Buffer
55         err = HRRootTmpl.Execute(&buf, struct {
56                 Version  string
57                 Packages []string
58         }{
59                 Version:  UserAgent,
60                 Packages: packages,
61         })
62         if err != nil {
63                 log.Println("error", r.RemoteAddr, "hr-root", err)
64                 http.Error(w, err.Error(), http.StatusInternalServerError)
65                 return
66         }
67         w.Write(buf.Bytes())
68 }
69
70 func serveHRPkg(w http.ResponseWriter, r *http.Request) {
71         cols := strings.Split(strings.TrimRight(r.URL.Path, "/"), "/")
72         pkgName := cols[len(cols)-1]
73         meta, releases, err := getMD(pkgName, "")
74         if err != nil {
75                 if os.IsNotExist(err) {
76                         http.NotFound(w, r)
77                 } else {
78                         log.Println("error", r.RemoteAddr, "json", pkgName, err)
79                         http.Error(w, err.Error(), http.StatusInternalServerError)
80                 }
81                 return
82         }
83         var buf bytes.Buffer
84         err = HRPkgTmpl.Execute(&buf, struct {
85                 Version  string
86                 PkgName  string
87                 Info     PkgInfo
88                 Releases []*PkgReleaseInfo
89         }{
90                 Version:  UserAgent,
91                 PkgName:  pkgName,
92                 Info:     meta.Info,
93                 Releases: releases,
94         })
95         if err != nil {
96                 log.Println("error", r.RemoteAddr, "root", err)
97                 http.Error(w, err.Error(), http.StatusInternalServerError)
98                 return
99         }
100         w.Write(buf.Bytes())
101 }