39 lines
817 B
Go
39 lines
817 B
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"lishwist/http/response"
|
|
"lishwist/http/session"
|
|
)
|
|
|
|
type VisibilityRouter struct {
|
|
store *session.Store
|
|
Public *response.ServeMux
|
|
Private *response.ServeMux
|
|
}
|
|
|
|
func (s *VisibilityRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
session, _ := s.store.Get(r, "lishwist_user")
|
|
_, authorized := session.Values["sessionKey"]
|
|
|
|
if authorized {
|
|
s.Private.ServeHTTP(w, r)
|
|
} else {
|
|
s.Public.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
func New(store *session.Store) *VisibilityRouter {
|
|
return &VisibilityRouter{
|
|
store: store,
|
|
Public: response.NewServeMux(store),
|
|
Private: response.NewServeMux(store),
|
|
}
|
|
}
|
|
|
|
func (r *VisibilityRouter) HandleFunc(pattern string, handler response.HandlerFunc) {
|
|
r.Public.HandleFunc(pattern, handler)
|
|
r.Private.HandleFunc(pattern, handler)
|
|
}
|