/* GoCheese -- Python private package repository and caching proxy Copyright (C) 2019-2021 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "bytes" "html/template" "io/ioutil" "log" "net/http" "os" "sort" "strings" ) var ( HRRootTmpl = template.Must(template.New("hr-root").Parse(` {{.Version}}: human readable listing `)) HRPkgTmpl = template.Must(template.New("hr-pkg").Parse(` {{.Version}}: package {{.PkgName}}
{{with .Info.Name}}
Name
{{.}}
{{end}} {{with .Info.Version}}
Version
{{.}}
{{end}} {{with .Info.Platform}}
Platform
    {{range .}}
  • {{.}}
  • {{end}}
{{end}} {{with .Info.SupportedPlatform}}
SupportedPlatform
    {{range .}}
  • {{.}}
  • {{end}}
{{end}}
Summary
{{.Info.Summary}}
Description
{{.Info.Description}}
      
{{with .Info.DescriptionContentType}}
DescriptionContentType
{{.}}
{{end}} {{with .Info.Keywords}}
Keywords
{{.}}
{{end}} {{with .Info.HomePage}}
HomePage
{{.}}
{{end}} {{with .Info.Author}}
Author
{{.}}
{{end}} {{with .Info.AuthorEmail}}
AuthorEmail
{{.}}
{{end}} {{with .Info.Maintainer}}
Maintainer
{{.}}
{{end}} {{with .Info.MaintainerEmail}}
MaintainerEmail
{{.}}
{{end}} {{with .Info.License}}
License
{{.}}
{{end}} {{with .Info.Classifier}}
Classifier
    {{range .}}
  • {{.}}
  • {{end}}
{{end}} {{with .Info.RequiresDist}}
RequiresDist
    {{range .}}
  • {{.}}
  • {{end}}
{{end}} {{with .Info.RequiresPython}}
RequiresPython
{{.}}
{{end}} {{with .Info.RequiresExternal}}
RequiresExternal
    {{range .}}
  • {{.}}
  • {{end}}
{{end}} {{with .Info.ProjectURL}}
ProjectURL
    {{range .}}
  • {{.}}
  • {{end}}
{{end}} {{with .Info.ProvidesExtra}}
ProvidesExtra
    {{range .}}
  • {{.}}
  • {{end}}
{{end}}

Releases

{{range .Releases}}{{if .Size}} {{end}}{{end}}
Filename Version Uploaded Size Digests
{{.Filename}} {{.Version}} {{.UploadTimeISO8601}} {{.Size}}
    {{range $a, $d := .Digests}}
  • {{$a}}: {{$d}}
  • {{end}}
`)) ) func serveHRRoot(w http.ResponseWriter, r *http.Request) { files, err := ioutil.ReadDir(*Root) if err != nil { log.Println("error", r.RemoteAddr, "hr-root", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } packages := make([]string, 0, len(files)) for _, f := range files { packages = append(packages, f.Name()) } sort.Strings(packages) var buf bytes.Buffer err = HRRootTmpl.Execute(&buf, struct { Version string Packages []string }{ Version: UserAgent, Packages: packages, }) if err != nil { log.Println("error", r.RemoteAddr, "hr-root", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Write(buf.Bytes()) } func serveHRPkg(w http.ResponseWriter, r *http.Request) { cols := strings.Split(strings.TrimRight(r.URL.Path, "/"), "/") pkgName := cols[len(cols)-1] meta, releases, err := getMetadata(pkgName, "") if err != nil { if os.IsNotExist(err) { http.NotFound(w, r) } else { log.Println("error", r.RemoteAddr, "json", pkgName, err) http.Error(w, err.Error(), http.StatusInternalServerError) } return } var buf bytes.Buffer err = HRPkgTmpl.Execute(&buf, struct { Version string PkgName string Info PkgInfo Releases []*PkgReleaseInfo }{ Version: UserAgent, PkgName: pkgName, Info: meta.Info, Releases: releases, }) if err != nil { log.Println("error", r.RemoteAddr, "root", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Write(buf.Bytes()) }