2024-02-10 11:59:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal"
|
2024-02-11 19:28:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/handlers"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/web/static"
|
2024-02-10 11:59:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-02-11 19:28:00 +00:00
|
|
|
config := internal.NewConfig()
|
2024-02-11 09:15:00 +00:00
|
|
|
|
2024-02-10 11:59:58 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2024-02-11 19:28:00 +00:00
|
|
|
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")
|
|
|
|
|
2024-02-11 19:28:00 +00:00
|
|
|
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
|
|
|
|
2024-02-10 11:59:58 +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))))
|
2024-02-10 11:59:58 +00:00
|
|
|
|
2024-02-11 19:28:00 +00:00
|
|
|
r.HandleFunc("/", h.Index).Methods("GET")
|
2024-02-11 22:48:37 +00:00
|
|
|
|
|
|
|
// Authenticated routes
|
2024-02-12 13:20:38 +00:00
|
|
|
r.HandleFunc("/settings", h.Authenticated(h.SettingsOverviewGET)).Methods("GET")
|
|
|
|
r.HandleFunc("/settings/healthchecks", h.Authenticated(h.SettingsHealthchecksGET)).Methods("GET")
|
2024-02-11 19:28:00 +00:00
|
|
|
|
|
|
|
// OAuth2
|
|
|
|
r.HandleFunc("/oauth2/login", h.OAuth2LoginGET).Methods("GET")
|
|
|
|
r.HandleFunc("/oauth2/callback", h.OAuth2CallbackGET).Methods("GET")
|
2024-02-11 22:48:37 +00:00
|
|
|
r.HandleFunc("/oauth2/logout", h.Authenticated(h.OAuth2LogoutGET)).Methods("GET")
|
2024-02-10 11:59:58 +00:00
|
|
|
|
2024-02-12 10:22:14 +00:00
|
|
|
// 404
|
|
|
|
r.PathPrefix("/").HandlerFunc(h.Error404).Methods("GET")
|
|
|
|
|
2024-02-11 19:28:00 +00:00
|
|
|
log.Println("Server started on", config.PORT)
|
|
|
|
log.Fatal(http.ListenAndServe(":"+config.PORT, r))
|
2024-02-10 11:59:58 +00:00
|
|
|
}
|