feat: add router

This commit is contained in:
axolotlmaid 2024-09-25 17:03:55 +01:00
parent cb61a2a8b7
commit 2357f645ce
4 changed files with 35 additions and 1 deletions

View file

@ -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()
}

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

29
internal/server/router.go Normal file
View file

@ -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)
}