]> Cypherpunks.ru repositories - gocheese.git/blob - hr.go
More convenient trusted-host
[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         "log"
25         "net/http"
26         "os"
27         "sort"
28         "strings"
29 )
30
31 var (
32         //go:embed hr-root.tmpl
33         HRRootTmplRaw string
34         HRRootTmpl    = template.Must(template.New("hr-root").Parse(HRRootTmplRaw))
35
36         //go:embed hr-pkg.tmpl
37         HRPkgTmplRaw string
38         HRPkgTmpl    = template.Must(template.New("hr-pkg").Parse(HRPkgTmplRaw))
39 )
40
41 func serveHRRoot(w http.ResponseWriter, r *http.Request) {
42         files, err := os.ReadDir(Root)
43         if err != nil {
44                 log.Println("error", r.RemoteAddr, "hr-root", err)
45                 http.Error(w, err.Error(), http.StatusInternalServerError)
46                 return
47         }
48         packages := make([]string, 0, len(files))
49         for _, f := range files {
50                 packages = append(packages, f.Name())
51         }
52         sort.Strings(packages)
53         var buf bytes.Buffer
54         err = HRRootTmpl.Execute(&buf, struct {
55                 Version  string
56                 Packages []string
57         }{
58                 Version:  UserAgent,
59                 Packages: packages,
60         })
61         if err != nil {
62                 log.Println("error", r.RemoteAddr, "hr-root", err)
63                 http.Error(w, err.Error(), http.StatusInternalServerError)
64                 return
65         }
66         w.Write(buf.Bytes())
67 }
68
69 func serveHRPkg(w http.ResponseWriter, r *http.Request) {
70         cols := strings.Split(strings.TrimRight(r.URL.Path, "/"), "/")
71         pkgName := cols[len(cols)-1]
72         meta, releases, err := getMD(pkgName, "")
73         if err != nil {
74                 if os.IsNotExist(err) {
75                         http.NotFound(w, r)
76                 } else {
77                         log.Println("error", r.RemoteAddr, "json", pkgName, err)
78                         http.Error(w, err.Error(), http.StatusInternalServerError)
79                 }
80                 return
81         }
82         var buf bytes.Buffer
83         err = HRPkgTmpl.Execute(&buf, struct {
84                 Version  string
85                 PkgName  string
86                 Info     PkgInfo
87                 Releases []*PkgReleaseInfo
88         }{
89                 Version:  UserAgent,
90                 PkgName:  pkgName,
91                 Info:     meta.Info,
92                 Releases: releases,
93         })
94         if err != nil {
95                 log.Println("error", r.RemoteAddr, "root", err)
96                 http.Error(w, err.Error(), http.StatusInternalServerError)
97                 return
98         }
99         w.Write(buf.Bytes())
100 }