diff options
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | config.go | 35 | ||||
-rw-r--r-- | config.json | 21 | ||||
-rw-r--r-- | handlers.go | 11 | ||||
-rw-r--r-- | main.go | 10 |
5 files changed, 53 insertions, 27 deletions
@@ -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 @@ -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" @@ -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)) } |