summaryrefslogtreecommitdiff
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
parentd4eebc634e4edaa9a8789fd7e3877b4d56423293 (diff)
Use a TOML file for configuration0.1.1
A configuration file is more suitable for packaging than environment variables.
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--main.go33
3 files changed, 25 insertions, 14 deletions
diff --git a/go.mod b/go.mod
index 7711949..2fa4d5f 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,4 @@ module git.sr.ht/~benburwell/goredir
go 1.13
-require github.com/virtyx-technologies/readenv v0.1.0
+require github.com/BurntSushi/toml v0.3.1
diff --git a/go.sum b/go.sum
index 3a6104a..9cb2df8 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,2 @@
-github.com/virtyx-technologies/readenv v0.1.0 h1:Ryd/IEDH88epiWUT2WfEv1A0dJSUC0wOadRNuGMvxnc=
-github.com/virtyx-technologies/readenv v0.1.0/go.mod h1:WJCkmFD7G/h27NgQE/GQaEsjrY4DEwxVL9tt/mUD1Iw=
+github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
+github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
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 {