87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package routes
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
|
|
"gitea.com/go-chi/session"
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"gitea.zokki.net/zokki/uni/web43-diary/internal/config"
|
|
"gitea.zokki.net/zokki/uni/web43-diary/internal/middleware"
|
|
"gitea.zokki.net/zokki/uni/web43-diary/web/templates"
|
|
diaryTemplates "gitea.zokki.net/zokki/uni/web43-diary/web/templates/diary"
|
|
userTemplates "gitea.zokki.net/zokki/uni/web43-diary/web/templates/user"
|
|
)
|
|
|
|
const (
|
|
DiaryIDValue = "diaryID"
|
|
UserIDValue = "userID"
|
|
ImageIDValue = "imageID"
|
|
)
|
|
|
|
func RegisterRoutes(router *chi.Mux, assetFS *embed.FS) {
|
|
if config.Config.Environment.IsDevelopment() {
|
|
router.Handle("GET /assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
|
|
} else {
|
|
router.Handle("GET /assets/*", http.FileServer(http.FS(assetFS)))
|
|
}
|
|
|
|
router.Route("/", func(router chi.Router) {
|
|
router.Use(session.Sessioner(session.Options{
|
|
Provider: "file",
|
|
ProviderConfig: path.Join(os.TempDir(), "web43", "sessions"),
|
|
CookieName: "i-love-cookies",
|
|
Domain: "localhost",
|
|
Maxlifetime: 86400 * 30, // 30 days
|
|
}))
|
|
router.Use(middleware.WrapContext)
|
|
|
|
router.Get("/", render(templates.Index))
|
|
router.Get("/settings", render(templates.Settings))
|
|
|
|
router.Route("/diary", func(router chi.Router) {
|
|
router.Get("/", render(diaryTemplates.CreateDiary))
|
|
router.Post("/", createDiary)
|
|
|
|
router.Route(fmt.Sprintf("/{%s:[0-9]+}", DiaryIDValue), func(router chi.Router) {
|
|
router.Get("/", render(diaryTemplates.ViewDiary))
|
|
router.Put("/", updateDiary)
|
|
router.Delete("/", deleteDiary)
|
|
|
|
router.Get("/edit", render(diaryTemplates.EditDiary))
|
|
})
|
|
})
|
|
|
|
router.Route("/user", func(router chi.Router) {
|
|
router.Post("/theme", updateTheme)
|
|
|
|
router.Get("/register", render(userTemplates.Register))
|
|
router.Post("/register", createUser)
|
|
|
|
router.Get("/login", render(userTemplates.Login))
|
|
router.Post("/login", loginUser)
|
|
|
|
router.Route(fmt.Sprintf("/{%s:[0-9]+}", UserIDValue), func(router chi.Router) {
|
|
router.Put("/", updateUser)
|
|
router.Delete("/", deleteUser)
|
|
|
|
router.Put("/password", updateUserPassword)
|
|
|
|
router.Get("/logout", logoutUser)
|
|
})
|
|
})
|
|
|
|
router.Route("/image", func(router chi.Router) {
|
|
router.Post("/", createImage)
|
|
|
|
router.Route(fmt.Sprintf("/{%s:[0-9]+}", ImageIDValue), func(router chi.Router) {
|
|
router.Get("/", getImage)
|
|
})
|
|
})
|
|
})
|
|
}
|