diff --git a/.gitignore b/.gitignore index 4840892..3737798 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ snuffler-web database.db +config.json \ No newline at end of file diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..9765ae6 --- /dev/null +++ b/config.json.example @@ -0,0 +1,3 @@ +{ + "port": 6900 +} \ No newline at end of file diff --git a/main.go b/main.go index 877a32b..ebc0cad 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,20 @@ package main import ( "database/sql" + "encoding/json" + "fmt" "html/template" "log" "net/http" + "os" _ "modernc.org/sqlite" ) +type Config struct { + Port uint +} + type user struct { id uint name string @@ -57,14 +64,33 @@ var db *sql.DB func main() { var err error + + // Load config + fmt.Println("Loading config.json") + cfgfile, err := os.Open("config.json") + if err != nil { + log.Fatal("Error reading config.json!") + return + } + defer cfgfile.Close() + + decoder := json.NewDecoder(cfgfile) + Config := Config{} + err = decoder.Decode(&Config) + if err != nil { + log.Fatal("Can't decode config.json!") + return + } + db, err = sql.Open("sqlite", "database.db") if err != nil { log.Fatal(err) } + fmt.Printf("Starting listener on :%v", Config.Port) http.Handle("/static/", http.FileServer(http.Dir("./"))) http.HandleFunc("/", httpRootHandler) - err = http.ListenAndServe(":6900", nil) + err = http.ListenAndServe(fmt.Sprintf(":%v", Config.Port), nil) if err != nil { log.Fatal(err) }