42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
type AdminPages struct {
|
|
router *httprouter.Router
|
|
}
|
|
|
|
func NewAdminPages() *AdminPages {
|
|
admin := &AdminPages{httprouter.New()}
|
|
p := "/admin/" // prefix
|
|
admin.router.GET(p, PageAdminRoot)
|
|
admin.router.GET(p + "login", PageAdminLogin)
|
|
admin.router.GET(p + "foo", PageAdminFoo)
|
|
return admin
|
|
}
|
|
|
|
func (a *AdminPages) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
cookie, _ := r.Cookie("sess")
|
|
if cookie == nil && r.URL.Path != "/admin/login" {
|
|
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
|
}
|
|
a.router.ServeHTTP(w, r)
|
|
}
|
|
|
|
func PageAdminRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
fmt.Fprint(w, "landed on root!")
|
|
}
|
|
|
|
// Login the user into the admin panel
|
|
func PageAdminLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
fmt.Fprint(w, "You now have a valid login!")
|
|
}
|
|
|
|
func PageAdminFoo(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
|
fmt.Fprintf(w, "Adminpanel not implemented yet!")
|
|
}
|