diff --git a/Dockerfile b/Dockerfile index ca7b1ed..e47026b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,7 @@ RUN set -ex \ EXPOSE 3000 +VOLUME ["/config"] + ENTRYPOINT ["/go/bin/badge-gen"] -CMD ["--"] +CMD ["--config", "/config/config.yaml"] diff --git a/app.go b/app.go index 3196f18..e7cd4ed 100644 --- a/app.go +++ b/app.go @@ -5,14 +5,18 @@ import ( "crypto/sha1" "errors" "fmt" + "io/ioutil" "log" "net/http" "net/url" + "os" "sort" "strings" "text/template" "time" + "gopkg.in/yaml.v2" + "golang.org/x/net/context" "github.com/Luzifer/badge-gen/cache" @@ -29,13 +33,17 @@ const ( var ( cfg = struct { - Port int64 `env:"PORT"` - Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` - Cache string `flag:"cache" default:"mem://" description:"Where to cache query results from thirdparty APIs"` + Port int64 `env:"PORT"` + Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` + Cache string `flag:"cache" default:"mem://" description:"Where to cache query results from thirdparty APIs"` + ConfStorage string `flag:"config" default:"config.yaml" description:"Configuration store"` }{} + serviceHandlers = map[string]serviceHandler{} version = "dev" - cacheStore cache.Cache + + cacheStore cache.Cache + configStore = configStorage{} ) type serviceHandlerDocumentation struct { @@ -82,6 +90,13 @@ func main() { log.Fatalf("Unable to open cache: %s", err) } + if _, err := os.Stat(cfg.ConfStorage); err == nil { + rawConfig, _ := ioutil.ReadFile(cfg.ConfStorage) + if err := yaml.Unmarshal(rawConfig, &configStore); err != nil { + log.Fatalf("Unable to parse config: %s", err) + } + } + r := mux.NewRouter() r.HandleFunc("/v1/badge", generateBadge).Methods("GET") r.HandleFunc("/{service}/{parameters:.*}", generateServiceBadge).Methods("GET") diff --git a/configStore.go b/configStore.go new file mode 100644 index 0000000..a1d0c25 --- /dev/null +++ b/configStore.go @@ -0,0 +1,31 @@ +package main + +type configStorage map[string]interface{} + +func (c configStorage) Str(name string) string { + v, ok := c[name] + + if !ok { + return "" + } + + if sv, ok := v.(string); ok { + return sv + } + + return "" +} + +func (c configStorage) Int64(name string) int64 { + v, ok := c[name] + + if !ok { + return 0 + } + + if sv, ok := v.(int64); ok { + return sv + } + + return 0 +} diff --git a/service_github.go b/service_github.go index c260034..a46daab 100644 --- a/service_github.go +++ b/service_github.go @@ -52,6 +52,10 @@ func (g githubServiceHandler) handleLicense(ctx context.Context, params []string req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil) req.Header.Set("Accept", "application/vnd.github.drax-preview+json") + if configStore.Str("github.personal_token") != "" { + req.SetBasicAuth(configStore.Str("github.username"), configStore.Str("github.personal_token")) + } + var resp *http.Response resp, err = ctxhttp.Do(ctx, http.DefaultClient, req) if err != nil {