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