57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/Teajey/sqlstore"
|
|
)
|
|
|
|
type VisibilityRouter struct {
|
|
Store *sqlstore.Store
|
|
Public *http.ServeMux
|
|
Private *http.ServeMux
|
|
}
|
|
|
|
func (s *VisibilityRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
session, _ := s.Store.Get(r, "lishwist_user")
|
|
authorized, _ := session.Values["authorized"].(bool)
|
|
|
|
if authorized {
|
|
s.Private.ServeHTTP(w, r)
|
|
} else {
|
|
s.Public.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
type Router struct {
|
|
Json VisibilityRouter
|
|
Html VisibilityRouter
|
|
}
|
|
|
|
func (s *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
accept := r.Header.Get("Accept")
|
|
|
|
switch {
|
|
case strings.HasPrefix(accept, "application/json"):
|
|
s.Json.ServeHTTP(w, r)
|
|
default:
|
|
s.Html.ServeHTTP(w, r)
|
|
}
|
|
}
|
|
|
|
func New(store *sqlstore.Store) *Router {
|
|
return &Router{
|
|
Json: VisibilityRouter{
|
|
Store: store,
|
|
Public: http.NewServeMux(),
|
|
Private: http.NewServeMux(),
|
|
},
|
|
Html: VisibilityRouter{
|
|
Store: store,
|
|
Public: http.NewServeMux(),
|
|
Private: http.NewServeMux(),
|
|
},
|
|
}
|
|
}
|