Ronbun/cmd/web/routes.go

68 lines
2.9 KiB
Go

package main
import (
"net/http"
"time"
"github.com/Thomasorus/Ronbun-CMS/ui"
"github.com/go-chi/httprate"
"github.com/justinas/alice"
)
// Creates a server, serve static files, list all routes and how to respond to them
func (app *application) routes() http.Handler {
// Create server
mux := http.NewServeMux()
// Serve static files
mux.Handle("GET /static/", http.FileServerFS(ui.Files))
// Bluesky handle route
mux.Handle("GET /.well-known/atproto-did", http.StripPrefix("/.well-known/", http.FileServer(http.Dir("./.well-known/"))))
// Serve files
mux.Handle("GET /uploaded/", http.StripPrefix("/uploaded/", http.FileServer(http.Dir("./uploaded/"))))
mux.Handle("GET /processed/", http.StripPrefix("/processed/", http.FileServer(http.Dir("./processed/"))))
// Dynamic routes
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
mux.Handle("GET /feed", dynamic.ThenFunc(app.feed))
mux.Handle("GET /sitemap", dynamic.ThenFunc(app.sitemap))
mux.Handle("GET /hire", dynamic.ThenFunc(app.hireme))
mux.Handle("GET /set-theme", dynamic.ThenFunc(app.setTheme))
mux.Handle("GET /{$}", dynamic.ThenFunc(app.home))
mux.Handle("GET /{slug}", dynamic.ThenFunc(app.pageView))
mux.Handle("GET /user/signup", dynamic.ThenFunc(app.userSignup))
mux.Handle("POST /user/signup", httprate.LimitByIP(1, 10*time.Second)(dynamic.ThenFunc(app.userSignupPost)))
mux.Handle("GET /user/login", dynamic.ThenFunc(app.userLogin))
mux.Handle("POST /user/login", httprate.LimitByIP(1, 10*time.Second)(dynamic.ThenFunc(app.userLoginPost)))
mux.Handle("GET /search", dynamic.ThenFunc(app.search))
mux.Handle("POST /search", httprate.LimitByIP(1, 10*time.Second)(dynamic.ThenFunc(app.searchPost)))
// mux.Handle("GET /import", dynamic.ThenFunc(app.ImportKaku))
// Serve OG images
mux.Handle("GET /og/{slug}", dynamic.ThenFunc(app.getEmbed))
// Protected routes
protected := dynamic.Append(app.requireAuthentication)
mux.Handle("GET /page/create", protected.ThenFunc(app.pageCreate))
mux.Handle("POST /page/create", protected.ThenFunc(app.pageCreatePost))
mux.Handle("GET /page/edit/{slug}", protected.ThenFunc(app.pageEdit))
mux.Handle("POST /page/edit/{slug}", protected.ThenFunc(app.PageEditPost))
mux.Handle("POST /page/delete/{id}", protected.ThenFunc(app.PageDeletePost))
mux.Handle("POST /user/logout", protected.ThenFunc(app.userLogoutPost))
mux.Handle("GET /account/password/update", protected.ThenFunc(app.accountPasswordUpdate))
mux.Handle("POST /account/password/update", protected.ThenFunc(app.accountPasswordUpdatePost))
mux.Handle("POST /files/upload", protected.ThenFunc(app.uploadFiles))
mux.Handle("POST /files/delete/{slug}", protected.ThenFunc(app.deleteFile))
mux.Handle("POST /page/preview", protected.ThenFunc(app.prewiewText))
// Chain of middlware
standard := alice.New(app.recoverPanic, app.logRequest, commonHeaders)
// Return the 'standard' middleware chain followed by the servemux.
return standard.Then(mux)
}