2022-09-19 15:00:19 +00:00
|
|
|
// main
|
2022-08-24 06:16:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-19 12:46:16 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-09-12 17:42:45 +00:00
|
|
|
"log"
|
2022-08-24 06:16:27 +00:00
|
|
|
"net/http"
|
2022-09-20 10:21:01 +00:00
|
|
|
"officeHours/config"
|
|
|
|
"officeHours/controllers"
|
|
|
|
"officeHours/repositories"
|
|
|
|
"officeHours/sqldb"
|
2022-09-24 13:01:33 +00:00
|
|
|
"officeHours/templating"
|
2022-09-21 13:40:16 +00:00
|
|
|
"os"
|
2022-08-24 06:16:27 +00:00
|
|
|
)
|
|
|
|
|
2022-08-29 20:58:19 +00:00
|
|
|
func main() {
|
2022-09-19 12:46:16 +00:00
|
|
|
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)
|
2022-09-12 17:42:45 +00:00
|
|
|
}
|
2022-08-24 06:16:27 +00:00
|
|
|
|
2022-09-21 13:40:16 +00:00
|
|
|
// serve static files
|
|
|
|
staticHandler := http.FileServer(http.Dir("./static"))
|
2022-09-24 13:01:33 +00:00
|
|
|
// parse templates
|
|
|
|
if err = templating.InitTemplates(); err != nil {
|
2022-09-26 10:42:08 +00:00
|
|
|
log.Fatal(err.Error())
|
2022-09-24 13:01:33 +00:00
|
|
|
}
|
|
|
|
// database connection
|
2022-09-21 20:24:08 +00:00
|
|
|
db, err := sqldb.Connect(conf)
|
|
|
|
if err != nil {
|
2022-09-26 10:42:08 +00:00
|
|
|
log.Fatal(err.Error())
|
2022-09-21 20:24:08 +00:00
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
// Create repos
|
|
|
|
roomRepo := repositories.NewRoomRepo(db)
|
|
|
|
courseRepo := repositories.NewCourseRepo(db)
|
|
|
|
tutorRepo := repositories.NewTutorRepo(db)
|
2022-09-19 16:32:15 +00:00
|
|
|
officeHourRepo := repositories.NewOfficeHourRepo(db, roomRepo, tutorRepo, courseRepo, conf)
|
2022-09-19 12:46:16 +00:00
|
|
|
requestRepo := repositories.NewRequestRepo(db, officeHourRepo, conf)
|
|
|
|
h := controllers.NewBaseHandler(roomRepo, officeHourRepo, courseRepo, tutorRepo, requestRepo, conf)
|
2022-08-24 06:16:27 +00:00
|
|
|
|
2022-08-29 20:58:19 +00:00
|
|
|
http.HandleFunc("/getByRoom", h.GetByRoomHandler)
|
|
|
|
http.HandleFunc("/getByCourse", h.GetByCourseHandler)
|
2022-08-31 20:49:14 +00:00
|
|
|
http.HandleFunc("/addOfficeHour", h.AddOfficeHourHandler)
|
2022-09-05 15:55:08 +00:00
|
|
|
http.HandleFunc("/confirmRequest", h.ConfirmRequestHandler)
|
2022-09-05 18:10:35 +00:00
|
|
|
http.HandleFunc("/deleteOfficeHour", h.DeleteOfficeHourHandler)
|
2022-08-29 20:58:19 +00:00
|
|
|
http.HandleFunc("/", h.RootHandler)
|
2022-09-21 13:40:16 +00:00
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
|
2022-08-24 06:16:27 +00:00
|
|
|
|
2022-09-19 12:46:16 +00:00
|
|
|
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.ListenAddress, conf.Server.ListenPort), nil)
|
2022-09-26 10:42:08 +00:00
|
|
|
log.Fatal(err.Error())
|
2022-08-24 06:16:27 +00:00
|
|
|
}
|