1
0
Fork 0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-11-09 13:50:03 +00:00

add config storage

This commit is contained in:
Knut Ahlers 2016-07-05 15:48:57 +02:00
parent 54c47998d7
commit a33de09449
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
4 changed files with 57 additions and 5 deletions

View file

@ -12,5 +12,7 @@ RUN set -ex \
EXPOSE 3000 EXPOSE 3000
VOLUME ["/config"]
ENTRYPOINT ["/go/bin/badge-gen"] ENTRYPOINT ["/go/bin/badge-gen"]
CMD ["--"] CMD ["--config", "/config/config.yaml"]

23
app.go
View file

@ -5,14 +5,18 @@ import (
"crypto/sha1" "crypto/sha1"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"sort" "sort"
"strings" "strings"
"text/template" "text/template"
"time" "time"
"gopkg.in/yaml.v2"
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/Luzifer/badge-gen/cache" "github.com/Luzifer/badge-gen/cache"
@ -29,13 +33,17 @@ const (
var ( var (
cfg = struct { cfg = struct {
Port int64 `env:"PORT"` Port int64 `env:"PORT"`
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"` 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"` 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{} serviceHandlers = map[string]serviceHandler{}
version = "dev" version = "dev"
cacheStore cache.Cache
cacheStore cache.Cache
configStore = configStorage{}
) )
type serviceHandlerDocumentation struct { type serviceHandlerDocumentation struct {
@ -82,6 +90,13 @@ func main() {
log.Fatalf("Unable to open cache: %s", err) 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 := mux.NewRouter()
r.HandleFunc("/v1/badge", generateBadge).Methods("GET") r.HandleFunc("/v1/badge", generateBadge).Methods("GET")
r.HandleFunc("/{service}/{parameters:.*}", generateServiceBadge).Methods("GET") r.HandleFunc("/{service}/{parameters:.*}", generateServiceBadge).Methods("GET")

31
configStore.go Normal file
View file

@ -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
}

View file

@ -52,6 +52,10 @@ func (g githubServiceHandler) handleLicense(ctx context.Context, params []string
req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil) req, _ := http.NewRequest("GET", "https://api.github.com/"+path, nil)
req.Header.Set("Accept", "application/vnd.github.drax-preview+json") 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 var resp *http.Response
resp, err = ctxhttp.Do(ctx, http.DefaultClient, req) resp, err = ctxhttp.Do(ctx, http.DefaultClient, req)
if err != nil { if err != nil {