Speichere Konfiguration in config/config.json

This commit is contained in:
Gonne 2022-09-19 14:46:16 +02:00
parent 43b3631da2
commit c38286bcc5
14 changed files with 249 additions and 78 deletions

40
main.go
View file

@ -1,42 +1,47 @@
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"log/syslog"
"net/http"
"os"
"sprechstundentool/config"
"sprechstundentool/controllers"
"sprechstundentool/repositories"
"sprechstundentool/sqldb"
"strings"
)
func main() {
logwriter, e := syslog.New(syslog.LOG_ERR, "sprechstunden")
logwriter, e := syslog.New(syslog.LOG_ERR, "office hours")
if e == nil {
log.SetOutput(logwriter)
}
var db *sql.DB
switch os.Getenv("ohtDbType") {
case "mysql":
db = sqldb.ConnectMysql(os.Getenv("ohtDbMysqlConnection"))
default:
if os.Getenv("ohtDbFile") != "" && !strings.Contains(os.Getenv("ohtDbFile"), "/") {
db = sqldb.ConnectSQLite(os.Getenv("ohtDbFile"))
} else {
db = sqldb.ConnectSQLite("sprechstunden.db")
}
configFile := flag.String(
"config",
"config/config.json",
"File path to the configuration file")
flag.Parse()
if *configFile == "" {
flag.Usage()
os.Exit(1)
}
var conf config.Config
err := config.ReadConfigFile(*configFile, &conf)
if err != nil {
log.Fatalf("%s: %s", "Reading JSON config file into config structure", err)
}
db := sqldb.Connect(conf)
// Create repos
roomRepo := repositories.NewRoomRepo(db)
courseRepo := repositories.NewCourseRepo(db)
tutorRepo := repositories.NewTutorRepo(db)
officeHourRepo := repositories.NewOfficeHourRepo(db, roomRepo, tutorRepo, courseRepo)
requestRepo := repositories.NewRequestRepo(db, officeHourRepo)
h := controllers.NewBaseHandler(roomRepo, officeHourRepo, courseRepo, tutorRepo, requestRepo)
requestRepo := repositories.NewRequestRepo(db, officeHourRepo, conf)
h := controllers.NewBaseHandler(roomRepo, officeHourRepo, courseRepo, tutorRepo, requestRepo, conf)
http.HandleFunc("/getByRoom", h.GetByRoomHandler)
http.HandleFunc("/getByCourse", h.GetByCourseHandler)
@ -45,5 +50,6 @@ func main() {
http.HandleFunc("/deleteOfficeHour", h.DeleteOfficeHourHandler)
http.HandleFunc("/", h.RootHandler)
http.ListenAndServe(":8080", nil)
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.ListenAddress, conf.Server.ListenPort), nil)
fmt.Println(err.Error())
}