package server import ( "log" "net/http" "thermalFax/controllers" ) func checkMethod(method string, r *http.Request) bool { return r.Method == method } func Serve() { // GET calls http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if !checkMethod(http.MethodGet, r) { w.WriteHeader(http.StatusMethodNotAllowed) return } http.Redirect(w, r, "/login", http.StatusPermanentRedirect) // controllers.Editor(w, r) }) http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { if !checkMethod(http.MethodGet, r) { w.WriteHeader(http.StatusMethodNotAllowed) return } controllers.Editor(w, r) }) // POST calls http.HandleFunc("/send-fax", func(w http.ResponseWriter, r *http.Request) { if !checkMethod(http.MethodPost, r) { w.WriteHeader(http.StatusMethodNotAllowed) return } controllers.SendFax(w, r) }) http.HandleFunc("/send-login", func(w http.ResponseWriter, r *http.Request) { if !checkMethod(http.MethodPost, r) { w.WriteHeader(http.StatusMethodNotAllowed) return } controllers.Authenticate(w, r) }) // Pages // On default add to a page on invalid methods or something like that http.HandleFunc("/history", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: controllers.HistoryPOST(w, r) case http.MethodGet: controllers.History(w, r) } }) http.HandleFunc("/editor", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: controllers.EditorPOST(w, r) case http.MethodGet: controllers.Editor(w, r) } }) http.HandleFunc("/personalData", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: controllers.PersonalDataPOST(w, r) case http.MethodGet: controllers.PersonalData(w, r) } }) // Serve the CSS and JS files, we might have to change this http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) log.Println("Starting server on port 8083") log.Fatal(http.ListenAndServe(":8083", nil)) }