mirror of
https://github.com/Luzifer/badge-gen.git
synced 2024-12-20 16:41:16 +00:00
add config storage
This commit is contained in:
parent
54c47998d7
commit
a33de09449
4 changed files with 57 additions and 5 deletions
|
@ -12,5 +12,7 @@ RUN set -ex \
|
|||
|
||||
EXPOSE 3000
|
||||
|
||||
VOLUME ["/config"]
|
||||
|
||||
ENTRYPOINT ["/go/bin/badge-gen"]
|
||||
CMD ["--"]
|
||||
CMD ["--config", "/config/config.yaml"]
|
||||
|
|
15
app.go
15
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"
|
||||
|
@ -32,10 +36,14 @@ var (
|
|||
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
|
||||
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")
|
||||
|
|
31
configStore.go
Normal file
31
configStore.go
Normal 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
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue