package main import ( "html/template" "log" "net/http" "os" "strings" "time" "github.com/virtyx-technologies/readenv" ) func main() { if err := run(); err != nil { log.Fatal(err) } } type options struct { Port string `env:"PORT"` CacheExpiry time.Duration `env:"CACHE_EXPIRY"` UpstreamTimeout time.Duration `env:"UPSTREAM_TIMEOUT"` SourcehutUsername string `env:"SRHT_USERNAME"` SourcehutToken string `env:"SRHT_TOKEN"` GithubUsername string `env:"GITHUB_USERNAME"` GithubToken string `env:"GITHUB_TOKEN"` CanonicalPrefix string `env:"CANONICAL_PREFIX"` } func run() error { var opts options if err := readenv.ReadEnv(&opts); err != nil { return err } cache := &PackageCache{ CanonicalPrefix: opts.CanonicalPrefix, ExpireAfter: opts.CacheExpiry, UpstreamTimeout: opts.UpstreamTimeout, 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) return http.ListenAndServe(":"+opts.Port, handler) } func handlePackage(pkgs *PackageCache) http.HandlerFunc { tmpl, err := template.New("package").Parse(`