summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config.go25
-rw-r--r--entry.go10
-rw-r--r--main.go19
3 files changed, 32 insertions, 22 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..61e666f
--- /dev/null
+++ b/config.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "log"
+ "os"
+)
+
+type Config struct {
+ Port string
+ DB string
+}
+
+func LoadConfig() Config {
+ at := os.Getenv("STATSAT")
+ if at == "" {
+ log.Println("Defaulting to STATSAT=:8080")
+ at = ":8080"
+ }
+ db := os.Getenv("STATSDB")
+ if db == "" {
+ log.Println("Defaulting to STATSDB=./stats.db")
+ db = "./stats.db"
+ }
+ return Config{Port: at, DB: db}
+}
diff --git a/entry.go b/entry.go
index 44ca42d..43547e8 100644
--- a/entry.go
+++ b/entry.go
@@ -2,7 +2,6 @@ package main
import (
"log"
- "os"
"time"
"github.com/jmoiron/sqlx"
@@ -30,14 +29,9 @@ type Entry struct {
var db *sqlx.DB
-func InitEntries() {
- file := os.Getenv("STATSDB")
- if file == "" {
- log.Println("Defaulting to STATSDB=./stats.db")
- file = "./stats.db"
- }
+func InitEntries(pathname string) {
var err error
- db, err = sqlx.Connect("sqlite3", file)
+ db, err = sqlx.Connect("sqlite3", pathname)
if err != nil {
log.Fatalln(err)
}
diff --git a/main.go b/main.go
index 011e8c4..e3cd540 100644
--- a/main.go
+++ b/main.go
@@ -7,7 +7,6 @@ import (
"html/template"
"log"
"net/http"
- "os"
"strconv"
"time"
@@ -118,28 +117,20 @@ func handleHome(pathname string) func(http.ResponseWriter, *http.Request) {
}
}
-func handleRequests() {
+func handleRequests(port string) {
router := mux.NewRouter()
router.HandleFunc("/entries", handleEntryOptions).Methods("OPTIONS")
router.HandleFunc("/entries", handleEntryPost).Methods("POST")
router.HandleFunc("/entries", handleEntryGet).Methods("GET")
router.HandleFunc("/", handleHome("index.html.in")).Methods("GET")
router.PathPrefix("/").Handler(http.FileServer(http.FS(content)))
- log.Fatal(http.ListenAndServe(configure(), router))
-}
-
-func configure() string {
- at := os.Getenv("STATSAT")
- if at == "" {
- log.Println("Defaulting to STATSAT=:8080")
- at = ":8080"
- }
- return at
+ log.Fatal(http.ListenAndServe(port, router))
}
func main() {
log.Println("Starting up")
- InitEntries()
+ cfg := LoadConfig()
+ InitEntries(cfg.DB)
defer CloseEntries()
- handleRequests()
+ handleRequests(cfg.Port)
}