STUFF
This commit is contained in:
+62
-20
@@ -4,36 +4,78 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"text/template"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type HTMXReturner func(http.ResponseWriter, *http.Request) (string, error)
|
||||
type WrapperFunc func(w http.ResponseWriter, r *http.Request, title, content string)
|
||||
|
||||
type Backend struct {
|
||||
blogCol *mongo.Collection
|
||||
authCol *mongo.Collection
|
||||
projCol *mongo.Collection
|
||||
|
||||
tmpl *template.Template
|
||||
wrapper WrapperFunc
|
||||
|
||||
back *backend.Backend
|
||||
|
||||
cacheMutex sync.RWMutex
|
||||
cache map[string]string
|
||||
}
|
||||
|
||||
func (b *Backend) AddToMux(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /editor/{page}", b.editorPage)
|
||||
func New(c *mongo.Client, back *backend.Backend, wrapper WrapperFunc) (*Backend, error) {
|
||||
var b = &Backend{
|
||||
blogCol: c.Database("blog").Collection("blog"),
|
||||
authCol: c.Database("blog").Collection("blog"),
|
||||
projCol: c.Database("blog").Collection("blog"),
|
||||
wrapper: wrapper,
|
||||
back: back,
|
||||
cache: make(map[string]string),
|
||||
}
|
||||
return b, b.parseTemplates()
|
||||
}
|
||||
|
||||
func (b *Backend) cacheMiddleware(h HTMXReturner) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b.cacheMutex.RLock()
|
||||
if pag, ok := b.cache[r.URL.EscapedPath()]; ok {
|
||||
w.Write([]byte(pag))
|
||||
b.cacheMutex.RUnlock()
|
||||
return
|
||||
func (b *Backend) RegisterToMux(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /editor", b.editorReq)
|
||||
|
||||
mux.HandleFunc("GET /editor/blog", b.blogPageReq)
|
||||
mux.HandleFunc("GET /editor/blog/edit", b.blogFormReq)
|
||||
mux.HandleFunc("POST /editor/blog/post", b.postBlogReq)
|
||||
|
||||
// mux.HandleFunc("GET /editor/portfolio", b.portfolioPageReq)
|
||||
// mux.HandleFunc("GET /editor/portfolio/edit", b.portfolioFormReq)
|
||||
// mux.HandleFunc("POST /editor/portfolio/post", b.postPortfolioReq)
|
||||
|
||||
// mux.HandleFunc("GET /editor/author", b.authorPageReq)
|
||||
// mux.HandleFunc("GET /editor/author/edit", b.authorFormReq)
|
||||
// mux.HandleFunc("POST /editor/author/post", b.postAuthorReq)
|
||||
}
|
||||
|
||||
func (b *Backend) verifyEditorCookie(r *http.Request) *backend.User {
|
||||
authCookie, err := r.Cookie("blogAuthToken")
|
||||
if err != nil {
|
||||
if err != http.ErrNoCookie {
|
||||
log.Println("error getting auth cookie:", err)
|
||||
}
|
||||
b.cacheMutex.RUnlock()
|
||||
b.cacheMutex.Lock()
|
||||
defer b.cacheMutex.Unlock()
|
||||
res, err := h(w, r)
|
||||
if err != nil {
|
||||
log.Printf("error getting %v: %v", r.URL.EscapedPath(), err)
|
||||
} else {
|
||||
b.cache[r.URL.EscapedPath()] = res
|
||||
return nil
|
||||
}
|
||||
usr, err := b.back.VerifyUser(r.Context(), authCookie.Value)
|
||||
if err != nil {
|
||||
if err != backend.ErrTokenUnauthorized {
|
||||
log.Println("error authorizing JWT token:", err)
|
||||
}
|
||||
w.Write([]byte(res))
|
||||
})
|
||||
return nil
|
||||
}
|
||||
return usr
|
||||
}
|
||||
|
||||
func redirect(w http.ResponseWriter, r *http.Request, path string) {
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("HX-Location", `{"path": "`+path+`", "target":"#content"}`)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "https://darkstorm.tech"+path, http.StatusFound)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user