Adding config.json
This commit is contained in:
parent
fac0e5a7a3
commit
78618fc1e8
3 changed files with 31 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
snuffler-web
|
||||
database.db
|
||||
config.json
|
3
config.json.example
Normal file
3
config.json.example
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"port": 6900
|
||||
}
|
28
main.go
28
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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue