summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-09-17 13:21:20 -0400
committerBen Burwell <ben@benburwell.com>2019-09-17 13:21:20 -0400
commit7a96c50022405b5c01c8b3a58966655dcb5bf754 (patch)
treeb99c05bfdef623623faec396b99742bf68b4250f /main.go
parentd4eebc634e4edaa9a8789fd7e3877b4d56423293 (diff)
Use a TOML file for configuration0.1.1
A configuration file is more suitable for packaging than environment variables.
Diffstat (limited to 'main.go')
-rw-r--r--main.go33
1 files changed, 22 insertions, 11 deletions
diff --git a/main.go b/main.go
index 9d6f5cf..81c69e4 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "flag"
"html/template"
"log"
"net/http"
@@ -8,7 +9,7 @@ import (
"strings"
"time"
- "github.com/virtyx-technologies/readenv"
+ "github.com/BurntSushi/toml"
)
func main() {
@@ -18,21 +19,30 @@ func main() {
}
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"`
+ BindAddress string `toml:"bind_address"`
+ CacheExpiry time.Duration `toml:"cache_expiry"`
+ UpstreamTimeout time.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"`
}
func run() error {
+ cfgPath := flag.String("c", "", "path to configuration file")
+ flag.Parse()
+
+ if cfgPath == nil || *cfgPath == "" {
+ flag.Usage()
+ os.Exit(1)
+ }
+
var opts options
- if err := readenv.ReadEnv(&opts); err != nil {
+ if _, err := toml.DecodeFile(*cfgPath, &opts); err != nil {
return err
}
+
cache := &PackageCache{
CanonicalPrefix: opts.CanonicalPrefix,
ExpireAfter: opts.CacheExpiry,
@@ -44,7 +54,8 @@ func run() error {
},
}
handler := handlePackage(cache)
- return http.ListenAndServe(":"+opts.Port, handler)
+ log.Printf("starting server to listen on %s...", opts.BindAddress)
+ return http.ListenAndServe(opts.BindAddress, handler)
}
func handlePackage(pkgs *PackageCache) http.HandlerFunc {