2018-01-18 05:00:26 +00:00
|
|
|
// myPrayerJournal API Server
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-12 03:38:13 +00:00
|
|
|
"encoding/json"
|
2018-01-18 17:29:01 +00:00
|
|
|
"log"
|
2018-03-12 03:38:13 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-01-18 17:29:01 +00:00
|
|
|
|
|
|
|
"github.com/danieljsummers/myPrayerJournal/src/api/data"
|
|
|
|
"github.com/danieljsummers/myPrayerJournal/src/api/routes"
|
2018-01-18 05:00:26 +00:00
|
|
|
)
|
|
|
|
|
2018-03-12 03:38:13 +00:00
|
|
|
// Web contains configuration for the web server.
|
|
|
|
type Web struct {
|
|
|
|
Port string `json:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings contains configuration for the myPrayerJournal API.
|
|
|
|
type Settings struct {
|
|
|
|
Data *data.Settings `json:"data"`
|
|
|
|
Web *Web `json:"web"`
|
|
|
|
Auth *routes.AuthConfig `json:"auth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// readSettings parses the JSON configuration file into the Settings struct.
|
|
|
|
func readSettings(f string) *Settings {
|
|
|
|
config, err := os.Open(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer config.Close()
|
|
|
|
settings := Settings{}
|
2018-03-13 02:44:43 +00:00
|
|
|
if err = json.NewDecoder(config).Decode(&settings); err != nil {
|
2018-03-12 03:38:13 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return &settings
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:00:26 +00:00
|
|
|
func main() {
|
2018-03-12 03:38:13 +00:00
|
|
|
cfg := readSettings("config.json")
|
2018-03-23 03:11:38 +00:00
|
|
|
if ok := data.Connect(cfg.Data); !ok {
|
2018-01-18 17:29:01 +00:00
|
|
|
log.Fatal("Unable to connect to database; exiting")
|
|
|
|
}
|
2018-03-23 03:11:38 +00:00
|
|
|
data.EnsureDB()
|
2018-03-12 03:38:13 +00:00
|
|
|
log.Printf("myPrayerJournal API listening on %s", cfg.Web.Port)
|
2018-03-23 03:11:38 +00:00
|
|
|
log.Fatal(http.ListenAndServe(cfg.Web.Port, routes.NewRouter(cfg.Auth)))
|
2018-01-18 05:00:26 +00:00
|
|
|
}
|