60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
// main
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"log/syslog"
|
|
"net/http"
|
|
"officeHours/config"
|
|
"officeHours/controllers"
|
|
"officeHours/repositories"
|
|
"officeHours/sqldb"
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
logwriter, e := syslog.New(syslog.LOG_ERR, "office hours")
|
|
if e == nil {
|
|
log.SetOutput(logwriter)
|
|
}
|
|
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)
|
|
}
|
|
|
|
// serve static files
|
|
staticHandler := http.FileServer(http.Dir("./static"))
|
|
|
|
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, conf)
|
|
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)
|
|
http.HandleFunc("/addOfficeHour", h.AddOfficeHourHandler)
|
|
http.HandleFunc("/confirmRequest", h.ConfirmRequestHandler)
|
|
http.HandleFunc("/deleteOfficeHour", h.DeleteOfficeHourHandler)
|
|
http.HandleFunc("/", h.RootHandler)
|
|
http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
|
|
|
|
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.ListenAddress, conf.Server.ListenPort), nil)
|
|
log.Println(err.Error())
|
|
}
|