aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-09-25 19:36:19 +0100
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-09-25 19:36:19 +0100
commite13c16a261b71b7e590dda6c9ef826385b7b5b02 (patch)
tree57a75e5d44362b07da88d44f437983128e52a9ab
parentd167a89638b4eecad852c66385737ef89354ec99 (diff)
Made config variable.
-rw-r--r--.gitignore1
-rw-r--r--TODO4
-rw-r--r--cache.go4
-rw-r--r--config.go53
-rw-r--r--db.go17
-rw-r--r--files.go2
-rw-r--r--handlers.go3
-rw-r--r--main.go4
-rw-r--r--router.go3
9 files changed, 77 insertions, 14 deletions
diff --git a/.gitignore b/.gitignore
index 3a7359c..487a62e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
audiocache
+*.swp
diff --git a/TODO b/TODO
index b671699..3c417af 100644
--- a/TODO
+++ b/TODO
@@ -1,3 +1,3 @@
-* Remove things that should be configy to a config file (FileRoot, db params, server port/addr, ...)
* Remove mp3 assumption
-* Betterness the Path <-> URI functions
+* 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.
diff --git a/cache.go b/cache.go
index 6eed536..820d977 100644
--- a/cache.go
+++ b/cache.go
@@ -4,8 +4,6 @@ import (
"time"
)
-var FileRoot string = "http://localhost:8080/files/"
-
type APICache struct {
Id uint64 `json:"id"`
Latitude float64 `json:"latitude"`
@@ -33,7 +31,7 @@ type PostCache struct {
}
func PathToURI(path string) string {
- return FileRoot + path
+ return config.API.Location + config.API.Files + path
}
func DBToAPI(db DBCache) APICache {
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..0805cec
--- /dev/null
+++ b/config.go
@@ -0,0 +1,53 @@
+package main
+
+type Config struct {
+ Database Database
+ Storage Storage
+ Server Server
+ API API
+}
+
+type Database struct {
+ Username string
+ Password string
+ Hostname string
+ Database string
+ Adapter string
+ SSLMode string
+}
+
+type Storage struct {
+ Location string
+}
+
+type Server struct {
+ Port string
+ Listen string
+}
+
+type API struct {
+ Location string
+ 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/",
+ },
+}
diff --git a/db.go b/db.go
index b71cb54..65f5041 100644
--- a/db.go
+++ b/db.go
@@ -5,10 +5,21 @@ import (
_ "github.com/lib/pq"
)
+func connect() (*sqlx.DB, error) {
+ db := config.Database
+
+ return sqlx.Connect(
+ db.Adapter,
+ "user="+db.Username+
+ " password="+db.Password+
+ " dbname="+db.Database+
+ " sslmode="+db.SSLMode)
+}
+
func getCaches() (DBCaches, error) {
var caches DBCaches
- db, err := sqlx.Connect("postgres", "user=audiocache password=audiocache dbname=audiocache sslmode=disable")
+ db, err := connect()
if err != nil {
return caches, err
}
@@ -25,7 +36,7 @@ func getCaches() (DBCaches, error) {
func getCache(id uint64) (DBCache, error) {
var cache DBCache
- db, err := sqlx.Connect("postgres", "user=audiocache password=audiocache dbname=audiocache sslmode=disable")
+ db, err := connect()
if err != nil {
return cache, err
}
@@ -42,7 +53,7 @@ func getCache(id uint64) (DBCache, error) {
func postCache(incache DBCache) (DBCache, error) {
var outcache DBCache
- db, err := sqlx.Connect("postgres", "user=audiocache password=audiocache dbname=audiocache sslmode=disable")
+ db, err := connect()
if err != nil {
return outcache, err
}
diff --git a/files.go b/files.go
index 2effe6e..fbbf703 100644
--- a/files.go
+++ b/files.go
@@ -14,7 +14,7 @@ func writeFile(postcache PostCache, filename string) error {
return err
}
- err = ioutil.WriteFile("files/"+filename, binary, 0644)
+ err = ioutil.WriteFile(config.API.Files+filename, binary, 0644)
if err != nil {
return err
}
diff --git a/handlers.go b/handlers.go
index 77fdeed..99ef718 100644
--- a/handlers.go
+++ b/handlers.go
@@ -12,6 +12,9 @@ import (
)
func Index(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
+ w.WriteHeader(http.StatusOK)
+
fmt.Fprintln(w, "Welcome!")
}
diff --git a/main.go b/main.go
index 3fcb722..dd3935e 100644
--- a/main.go
+++ b/main.go
@@ -6,8 +6,6 @@ import (
)
func main() {
-
router := NewRouter()
-
- log.Fatal(http.ListenAndServe(":8080", router))
+ log.Fatal(http.ListenAndServe(config.Server.Listen+":"+config.Server.Port, router))
}
diff --git a/router.go b/router.go
index 32caba3..203f4be 100644
--- a/router.go
+++ b/router.go
@@ -1,9 +1,8 @@
package main
import (
- "net/http"
-
"github.com/gorilla/mux"
+ "net/http"
)
func NewRouter() *mux.Router {