github.com/10ego/gthp@v0.0.0-20241025155251-e1514fa71fbb/cmd/server/main.go (about) 1 package main 2 3 import ( 4 "net/http" 5 "os" 6 "path/filepath" 7 8 "github.com/10ego/gthp/internal/auth" 9 "github.com/10ego/gthp/internal/config" 10 "github.com/10ego/gthp/internal/database" 11 "github.com/10ego/gthp/internal/handlers" 12 "github.com/10ego/gthp/internal/logger" 13 ) 14 15 func main() { 16 cfg, err := config.Load() 17 if err != nil { 18 os.Exit(1) 19 } 20 log := logger.New(cfg.Development) 21 defer log.Sync() 22 23 db, err := database.Connect(cfg.DatabaseURL, log) 24 if err != nil { 25 log.Fatalw("Failed to connect to database", "error", err) 26 } 27 defer db.Close() 28 29 ldapClient := auth.NewClient( 30 cfg.LDAPHost, 31 cfg.LDAPPort, 32 cfg.LDAPBaseDN, 33 cfg.LDAPUserFilter, 34 cfg.LDAPGroupDN, 35 ) 36 37 h := handlers.New(cfg, db, ldapClient, log) 38 39 mux := http.NewServeMux() 40 mux.HandleFunc("GET /", h.IndexHandler) 41 mux.HandleFunc("GET /login", h.LoginHandler) 42 mux.HandleFunc("POST /login", h.LoginHandler) 43 mux.HandleFunc("POST /logout", h.LogoutHandler) 44 45 fs := http.FileServer(http.Dir(filepath.Join("internal", "static"))) 46 mux.Handle("GET /static/", http.StripPrefix("/static/", fs)) 47 48 log.Infow("Server starting", "address", cfg.ServerAddr) 49 log.Fatal(http.ListenAndServe(cfg.ServerAddr, mux)) 50 // 51 // srv := &http.Server{ 52 // Addr: cfg.ServerAddr, 53 // Handler: mux, 54 // } 55 // 56 // // Start the server in a goroutine 57 // go func() { 58 // log.Infow("Server starting", "address", cfg.ServerAddr) 59 // if err := srv.ListenAndServe(); err != http.ErrServerClosed { 60 // log.Fatalw("ListenAndServe Failed", "error", err) 61 // } 62 // }() 63 // 64 // // Wait for interrupt signal to gracefully shutdown the server 65 // quit := make(chan os.Signal, 1) 66 // signal.Notify(quit, os.Interrupt) 67 // <-quit 68 // log.Info("Server is shutting down...") 69 // 70 // ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 71 // defer cancel() 72 // if err := srv.Shutdown(ctx); err != nil { 73 // log.Fatalw("Server forced to shutdown", "error", err) 74 // } 75 // 76 // log.Info("Server exiting") 77 }