lishwist/http/mux/serve.go
2026-02-19 20:16:04 +09:00

60 lines
1.3 KiB
Go

package mux
import (
"net/http"
"github.com/Teajey/rsvp"
lishwist "lishwist/core"
"lishwist/http/session"
"lishwist/http/templates"
"lishwist/http/templates/text"
)
type Serve struct {
store *session.Store
rsvpCfg rsvp.Config
Public Public
Private Private
}
func (m *Serve) GetSession(r *http.Request) *session.Session {
innerSession, _ := m.store.Get(r, "lishwist_user")
return session.New(innerSession)
}
func (m *Serve) ServeHTTP(w http.ResponseWriter, r *http.Request) {
innerSession, _ := m.store.Get(r, "lishwist_user")
s := session.New(innerSession)
_, inSession := s.GetString("sessionKey")
if inSession {
m.Private.ServeHTTP(w, r)
return
}
m.Public.ServeHTTP(w, r)
}
func New(store *session.Store) *Serve {
cfg := rsvp.Config{}
cfg.HtmlTemplate = templates.Template
cfg.TextTemplate = text.Template
sv := Serve{
store: store,
rsvpCfg: cfg,
Public: newPublic(store, cfg),
Private: newPrivate(store, cfg),
}
return &sv
}
func (m *Serve) HandleFunc(pattern string, hnd rsvp.HandlerFunc) {
m.Public.HandleFunc(pattern, func(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
return hnd(w, r)
})
m.Private.HandleFunc(pattern, func(app *lishwist.Session, s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
return hnd(w, r)
})
}