2024-02-16 21:31:00 +00:00
|
|
|
package server
|
2024-02-10 11:59:58 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-16 21:31:00 +00:00
|
|
|
"context"
|
2024-02-10 11:59:58 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal"
|
2024-02-15 22:47:56 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/config"
|
2024-02-11 19:28:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/handlers"
|
2024-02-16 21:31:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/temporal"
|
2024-02-11 19:28:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/web/static"
|
2024-02-16 21:31:00 +00:00
|
|
|
"github.com/gorilla/mux"
|
2024-02-10 11:59:58 +00:00
|
|
|
)
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
type Server struct {
|
|
|
|
server *http.Server
|
|
|
|
cfg *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(cfg *config.Config) (*Server, error) {
|
|
|
|
return &Server{
|
|
|
|
cfg: cfg,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Name() string {
|
|
|
|
return "HTTP WEB and API Server"
|
|
|
|
}
|
2024-02-11 09:15:00 +00:00
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
func (s *Server) Start() error {
|
2024-02-10 11:59:58 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
db, query, err := internal.ConnectToDatabase(s.cfg.DatabasePath)
|
2024-02-11 10:56:21 +00:00
|
|
|
if err != nil {
|
2024-02-16 21:31:00 +00:00
|
|
|
return err
|
2024-02-11 10:56:21 +00:00
|
|
|
}
|
|
|
|
log.Println("Connected to database")
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
temporalClient, err := temporal.ConnectServerToTemporal(s.cfg)
|
2024-02-16 12:07:29 +00:00
|
|
|
if err != nil {
|
2024-02-16 21:31:00 +00:00
|
|
|
return err
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
2024-02-18 21:37:17 +00:00
|
|
|
log.Println("Connected to Temporal")
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
h := handlers.NewBaseHandler(db, query, temporalClient, s.cfg)
|
2024-02-11 19:28:00 +00:00
|
|
|
|
|
|
|
// 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-15 22:47:56 +00:00
|
|
|
r.HandleFunc("/settings/healthchecks/create", h.Authenticated(h.SettingsHealthchecksCreateGET)).Methods("GET")
|
|
|
|
r.HandleFunc("/settings/healthchecks/create", h.Authenticated(h.SettingsHealthchecksCreatePOST)).Methods("POST")
|
2024-02-16 12:41:18 +00:00
|
|
|
r.HandleFunc("/settings/healthchecks/{slug}", h.Authenticated(h.SettingsHealthchecksDescribeGET)).Methods("GET")
|
2024-02-18 21:37:17 +00:00
|
|
|
r.HandleFunc("/settings/workers", h.Authenticated(h.SettingsWorkersGET)).Methods("GET")
|
|
|
|
r.HandleFunc("/settings/workers/create", h.Authenticated(h.SettingsWorkersCreateGET)).Methods("GET")
|
|
|
|
r.HandleFunc("/settings/workers/create", h.Authenticated(h.SettingsWorkersCreatePOST)).Methods("POST")
|
|
|
|
r.HandleFunc("/settings/workers/{slug}", h.Authenticated(h.SettingsWorkersDescribeGET)).Methods("GET")
|
|
|
|
r.HandleFunc("/settings/workers/{slug}/token", h.Authenticated(h.SettingsWorkersTokenGET)).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-13 11:00:48 +00:00
|
|
|
// Temporal UI
|
|
|
|
r.PathPrefix("/temporal").HandlerFunc(h.Authenticated(h.Temporal))
|
|
|
|
|
2024-02-12 10:22:14 +00:00
|
|
|
// 404
|
|
|
|
r.PathPrefix("/").HandlerFunc(h.Error404).Methods("GET")
|
|
|
|
|
2024-02-16 21:31:00 +00:00
|
|
|
s.server = &http.Server{
|
|
|
|
Addr: ":" + s.cfg.Port,
|
|
|
|
Handler: r,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.server.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
ctx := context.Background()
|
|
|
|
return s.server.Shutdown(ctx)
|
2024-02-10 11:59:58 +00:00
|
|
|
}
|