zdravko/cmd/server/main.go

63 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"code.tjo.space/mentos1386/zdravko/internal"
"code.tjo.space/mentos1386/zdravko/internal/handlers"
"code.tjo.space/mentos1386/zdravko/web/static"
)
func main() {
config := internal.NewConfig()
2024-02-11 09:15:00 +00:00
r := mux.NewRouter()
db, query, err := internal.ConnectToDatabase(config.SQLITE_DB_PATH)
2024-02-11 10:56:21 +00:00
if err != nil {
log.Fatal(err)
}
log.Println("Connected to database")
h := handlers.NewBaseHandler(db, query, config)
// Health
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
d, err := db.DB()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = d.Ping()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
_, err = w.Write([]byte("OK"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
2024-02-11 10:56:21 +00:00
// Server static files
2024-02-11 10:56:21 +00:00
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.Static))))
r.HandleFunc("/", h.Index).Methods("GET")
// Authenticated routes
r.HandleFunc("/settings", h.Authenticated(h.SettingsOverviewGET)).Methods("GET")
r.HandleFunc("/settings/healthchecks", h.Authenticated(h.SettingsHealthchecksGET)).Methods("GET")
// OAuth2
r.HandleFunc("/oauth2/login", h.OAuth2LoginGET).Methods("GET")
r.HandleFunc("/oauth2/callback", h.OAuth2CallbackGET).Methods("GET")
r.HandleFunc("/oauth2/logout", h.Authenticated(h.OAuth2LogoutGET)).Methods("GET")
// 404
r.PathPrefix("/").HandlerFunc(h.Error404).Methods("GET")
log.Println("Server started on", config.PORT)
log.Fatal(http.ListenAndServe(":"+config.PORT, r))
}