Files
ersteller/starter/routes/routing.go
T
2025-11-15 12:04:39 +01:00

116 lines
3.4 KiB
Go

package routes
import (
. "ersteller-lib"
"ersteller-lib/authentication"
google_http "ersteller-lib/authentication/google/http"
"git.gorlug.de/code/ersteller/starter/about"
"git.gorlug.de/code/ersteller/starter/contact"
"git.gorlug.de/code/ersteller/starter/ent"
"git.gorlug.de/code/ersteller/starter/env"
"git.gorlug.de/code/ersteller/starter/google"
"git.gorlug.de/code/ersteller/starter/index"
"git.gorlug.de/code/ersteller/starter/login"
"git.gorlug.de/code/ersteller/starter/page"
"git.gorlug.de/code/ersteller/starter/todos"
"net/http"
"github.com/gorilla/sessions"
. "maragu.dev/gomponents"
)
func CreateApi(environment env.Environment, db *ent.Client) http.Handler {
server := NewHtmxServer()
HtmxRouteDebugTrace = true
sessionStore := sessions.NewCookieStore([]byte(environment.SessionSecret))
//sessionStore.Options.Secure = false
if !environment.IsDev {
sessionStore.Options.Secure = true
sessionStore.Options.HttpOnly = true
}
googleAuth := google_http.NewGoogleAuth(google.NewDatabase(db), server.GetHttpServer(), google_http.Environment{
ClientId: environment.GoogleClientId,
ClientSecret: environment.GoogleClientSecret,
IsLocal: environment.IsLocal,
BaseUrl: environment.BaseUrl,
}, sessionStore)
googleAuth.AddRoutes()
indexActivePath := NewActivePath(map[Language]string{
En: "Home",
De: "Startseite",
}, LanguagePaths{
En: index.IndexPath,
De: index.IndexPathDe,
})
aboutActivePath := NewActivePath(map[Language]string{
En: "About",
De: "Über uns",
}, LanguagePaths{
En: about.AboutPath,
De: about.AboutPathDe,
})
contactActivePath := NewActivePath(map[Language]string{
En: "Contact",
De: "Kontakt",
}, LanguagePaths{
En: contact.ContactPath,
De: contact.ContactPathDe,
})
// Todos navigation item
todosActivePath := NewActivePath(map[Language]string{
En: "Todos",
De: "Todos",
}, LanguagePaths{
En: todos.TodosPath,
De: todos.TodosPathDe,
})
// Main navigation items
activePaths := []ActivePath{indexActivePath, aboutActivePath, contactActivePath, todosActivePath}
// Footer navigation items (placeholder - can be customized)
footerPaths := []ActivePath{}
createPageFunc := createPage(activePaths, footerPaths)
indexPage := index.NewPage(createPageFunc, server, &indexActivePath)
server.HandleStaticAndDefaultPath(indexPage.ViewRoute, En)
// Create About and Contact pages using the new packages
_ = about.NewPage(createPageFunc, server, &aboutActivePath)
_ = contact.NewPage(createPageFunc, server, &contactActivePath)
// Create Todos page
_ = todos.NewPage(createPageFunc, server, &todosActivePath, db)
// Create Login page
loginPaths := LanguagePaths{
En: login.LoginPath,
De: login.LoginPathDe,
}
loginActivePath := NewActivePath(map[Language]string{
En: "Login",
De: "Anmelden",
}, loginPaths)
_ = login.NewPage(createPageFunc, server, &loginActivePath)
serverWithMiddleWare := UseMiddleware(server, LoggingMiddleware, MakeGzipHandler,
authentication.Middleware(sessionStore,
[]string{"/de" + login.LoginPathDe, "/en" + authentication.LoginPath, google_http.GoogleLogin, google_http.GoogleLoginCallback, "/static"}, loginPaths))
return serverWithMiddleWare
}
func createPage(activePaths []ActivePath, footerPaths []ActivePath) CreateHtmxPageFunc {
return func(req HtmxContext, metadata PageWebsiteMetaData, content ...Node) {
metadata.NavItems = activePaths
metadata.FooterNavItems = footerPaths
page.CreatePage(req, metadata, content...)
}
}