diff --git a/cmd/backend/main.go b/cmd/backend/main.go index d1f1127..d0c6679 100644 --- a/cmd/backend/main.go +++ b/cmd/backend/main.go @@ -6,6 +6,8 @@ import ( "github.com/joho/godotenv" "github.com/lmittmann/tint" + + "backend/internal/server" ) func main() { @@ -17,5 +19,5 @@ func main() { slog.Error("Error loading .env file", slog.Any("error", err)) } - slog.Info("Starting server", slog.Any("port", os.Getenv("PORT"))) + server.NewRouter() } diff --git a/go.mod b/go.mod index a3ffc84..28fef5f 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module backend go 1.23.1 require ( + github.com/go-chi/chi/v5 v5.1.0 // indirect github.com/joho/godotenv v1.5.1 // indirect github.com/lmittmann/tint v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index 29c8fa1..4216455 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw= diff --git a/internal/server/router.go b/internal/server/router.go new file mode 100644 index 0000000..01f4c78 --- /dev/null +++ b/internal/server/router.go @@ -0,0 +1,29 @@ +package server + +import ( + "log/slog" + "net/http" + "os" + "time" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +func NewRouter() { + r := chi.NewRouter() + + // Middleware + r.Use(middleware.RequestID) + r.Use(middleware.RealIP) + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + r.Use(middleware.Timeout(60 * time.Second)) + + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "https://axolotlmaid.com", http.StatusPermanentRedirect) + }) + + slog.Info("Starting server", slog.Any("port", os.Getenv("PORT"))) + http.ListenAndServe(":"+os.Getenv("PORT"), r) +}