feat: add handlers to serve template and styles

This commit is contained in:
Stevan Freeborn
2025-11-15 09:00:06 -06:00
parent b42c63532f
commit 03cbe49a30
10 changed files with 225 additions and 2 deletions
+53 -2
View File
@@ -1,7 +1,58 @@
package main
import "fmt"
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/StevanFreeborn/links.stevanfreeborn.com/internal/handlers"
"github.com/StevanFreeborn/links.stevanfreeborn.com/internal/middleware"
)
func main() {
fmt.Println("Hello, World!")
mux := http.NewServeMux()
mux.HandleFunc("/", handlers.Index)
mux.HandleFunc("/css/", handlers.CSS)
loggingMux := middleware.Logging(mux)
const addr = ":7777"
server := &http.Server{
Addr: addr,
Handler: loggingMux,
}
go func() {
fmt.Printf("Starting server on %s\n", addr)
err := server.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("HTTP server error: %v", err)
}
log.Println("Stopped serving new connections.")
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()
if err := server.Shutdown(shutdownCtx); err != nil {
log.Fatalf("HTTP shutdown error: %v", err)
}
log.Println("Graceful shutdown complete.")
}