From 267c89fe42050d056b8c041ee66008e304c0bcf3 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Sat, 26 Sep 2015 13:12:37 +0100 Subject: Config now read from file at startup --- TODO | 3 +-- config.go | 35 +++++++++++++++-------------------- config.json | 21 +++++++++++++++++++++ handlers.go | 11 ++++++----- main.go | 10 ++++++++++ 5 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 config.json diff --git a/TODO b/TODO index 3c417af..fd5551b 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,2 @@ * Remove mp3 assumption -* Separate config var from config defs, and routes var from routes defs -* Should config and routes be broken out into json files to be read into variables at startup? Yes, probably. +* Routes for files diff --git a/config.go b/config.go index 0805cec..93b8423 100644 --- a/config.go +++ b/config.go @@ -1,5 +1,10 @@ package main +import ( + "encoding/json" + "io/ioutil" +) + type Config struct { Database Database Storage Storage @@ -30,24 +35,14 @@ type API struct { Files string } -var config = Config{ - Database{ - Username: "audiocache", - Password: "audiocache", - Hostname: "localhost", - Database: "audiocache", - Adapter: "postgres", - SSLMode: "disable", - }, - Storage{ - Location: "/tmp/audiocache/", - }, - Server{ - Listen: "127.0.0.1", - Port: "8080", - }, - API{ - Location: "http://localhost:8080/", - Files: "files/", - }, +func LoadFile(filename string) (Config, error) { + var config Config + + file, err := ioutil.ReadFile(filename) + if err != nil { + return config, err + } + + err = json.Unmarshal(file, &config) + return config, err } diff --git a/config.json b/config.json new file mode 100644 index 0000000..be08795 --- /dev/null +++ b/config.json @@ -0,0 +1,21 @@ +{ + "Database": { + "Username": "audiocache", + "Password": "audiocache", + "Hostname": "localhost", + "Database": "audiocache", + "Adapter": "postgres", + "SSLMode": "disable" + }, + "Storage": { + "Location": "/tmp/audiocache/" + }, + "Server": { + "Listen": "127.0.0.1", + "Port": "8080" + }, + "API": { + "Location": "http://localhost:8080/", + "Files": "files/" + } +} diff --git a/handlers.go b/handlers.go index 99ef718..227251b 100644 --- a/handlers.go +++ b/handlers.go @@ -32,13 +32,16 @@ func CacheIndex(w http.ResponseWriter, r *http.Request) { apicaches = append(apicaches, DBToAPI(dbcaches[db])) } - if err := json.NewEncoder(w).Encode(apicaches); err != nil { + out, err := json.MarshalIndent(apicaches, "", " ") + if err != nil { panic(err) } + + fmt.Fprintf(w, string(out)) } func MarshalCache(apicache APICache) (string, error) { - str, err := json.Marshal(apicache) + str, err := json.MarshalIndent(apicache, "", " ") return string(str), err } @@ -77,9 +80,7 @@ func CacheCreate(w http.ResponseWriter, r *http.Request) { if err := json.Unmarshal(body, &postcache); err != nil { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(422) // unprocessable entity - if err := json.NewEncoder(w).Encode(err); err != nil { - panic(err) - } + panic(err) } filename := uuid.New() + ".mp3" diff --git a/main.go b/main.go index dd3935e..225d022 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,21 @@ package main import ( + "fmt" "log" "net/http" + "os" ) +var config Config + func main() { + var err error + config, err = LoadFile("config.json") + if err != nil { + fmt.Fprintln(os.Stderr, "Error loading config") + os.Exit(1) + } router := NewRouter() log.Fatal(http.ListenAndServe(config.Server.Listen+":"+config.Server.Port, router)) } -- cgit v1.2.1