diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 61 |
1 files changed, 26 insertions, 35 deletions
@@ -5,7 +5,9 @@ import ( "html/template" "log" "net/http" + "net/url" "os" + "path" "strings" "time" @@ -29,14 +31,8 @@ func (d *duration) UnmarshalText(text []byte) error { } type options struct { - BindAddress string `toml:"bind_address"` - CacheExpiry duration `toml:"cache_expiry"` - UpstreamTimeout duration `toml:"upstream_timeout"` - SourcehutUsername string `toml:"srht_username"` - SourcehutToken string `toml:"srht_token"` - GithubUsername string `toml:"github_username"` - GithubToken string `toml:"github_token"` - CanonicalPrefix string `toml:"canonical_prefix"` + BindAddress string `toml:"bind_address"` + CanonicalPrefix string `toml:"canonical_prefix"` } func run() error { @@ -53,26 +49,11 @@ func run() error { return err } - log.Printf("caches expire after %s", opts.CacheExpiry.Duration) - log.Printf("upstream reqs time out after %s", opts.UpstreamTimeout.Duration) - - cache := &PackageCache{ - CanonicalPrefix: opts.CanonicalPrefix, - ExpireAfter: opts.CacheExpiry.Duration, - UpstreamTimeout: opts.UpstreamTimeout.Duration, - Logger: log.New(os.Stdout, "", log.LstdFlags), - Hosts: []RepoHost{ - Sourcehut{Username: opts.SourcehutUsername, Token: opts.SourcehutToken}, - Github{Username: opts.GithubUsername, Token: opts.GithubToken}, - }, - } - handler := handlePackage(cache) log.Printf("starting server to listen on %s...", opts.BindAddress) - return http.ListenAndServe(opts.BindAddress, handler) + return http.ListenAndServe(opts.BindAddress, handlePackage(&opts)) } -func handlePackage(pkgs *PackageCache) http.HandlerFunc { - tmpl, err := template.New("package").Parse(`<!DOCTYPE html> +var tmpl = template.Must(template.New("package").Parse(`<!DOCTYPE html> <html> <head> <meta name="go-import" content="{{ .Name }} git {{ .Repo }}"> @@ -84,10 +65,16 @@ func handlePackage(pkgs *PackageCache) http.HandlerFunc { <li><a href="{{ .Repo }}">Code</a></li> </ul> </body> -</html>`) - if err != nil { - return nil - } +</html>`)) + +// A Package represents a Go package's canonical name and its source git +// repository. +type Package struct { + Name string + Repo *url.URL +} + +func handlePackage(opts *options) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { w.WriteHeader(http.StatusBadRequest) @@ -95,13 +82,17 @@ func handlePackage(pkgs *PackageCache) http.HandlerFunc { return } parts := strings.SplitN(r.URL.Path[1:], "/", 2) - pkg, ok := pkgs.Get(r.Context(), parts[0]) - if !ok { - w.WriteHeader(http.StatusNotFound) - w.Write([]byte(http.StatusText(http.StatusNotFound))) - return + pkg := &Package{ + Name: path.Join(opts.CanonicalPrefix, parts[0]), + Repo: &url.URL{ + Scheme: "https", + Host: "git.burwell.io", + Path: parts[0], + }, } log.Printf("pkg: %v", pkg) - tmpl.Execute(w, pkg) + if err := tmpl.Execute(w, pkg); err != nil { + log.Printf("could not execute template: %v", err) + } } } |