mark which functions are supposed to have pages
This commit is contained in:
parent
ea99d621bd
commit
1d6b22606a
6
main.go
6
main.go
|
@ -22,8 +22,8 @@ type (
|
|||
TemplatePath string // path to the templates
|
||||
}
|
||||
Middleware struct {
|
||||
admin *Admin
|
||||
site *Site
|
||||
admin *AdminPages
|
||||
site *SitePages
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -94,6 +94,6 @@ func readConfig(config_path string) (*Config, error) {
|
|||
|
||||
// start the server with access log
|
||||
func start_server(config *Config) {
|
||||
middleware := Middleware{NewAdmin(), NewSite()}
|
||||
middleware := Middleware{NewAdminPages(), NewSitePages()}
|
||||
log.Print(http.ListenAndServe(fmt.Sprintf("%v:%v", config.Address, config.Port), middleware))
|
||||
}
|
||||
|
|
20
site.go
20
site.go
|
@ -6,33 +6,33 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
type Site struct {
|
||||
type SitePages struct {
|
||||
router *httprouter.Router
|
||||
}
|
||||
|
||||
func NewSite() *Site {
|
||||
site := &Site{httprouter.New()}
|
||||
site.router.GET("/", RouteIndex)
|
||||
site.router.GET("/post", RoutePosts)
|
||||
site.router.GET("/post/:id", RoutePostsWithID)
|
||||
func NewSitePages() *SitePages {
|
||||
site := &SitePages{httprouter.New()}
|
||||
site.router.GET("/", PageIndex)
|
||||
site.router.GET("/post", PagePosts)
|
||||
site.router.GET("/post/:id", PagePostsWithID)
|
||||
return site
|
||||
}
|
||||
|
||||
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *SitePages) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
s.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// the function for route /
|
||||
func RouteIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
func PageIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
Templates.ExecuteTemplate(w, "welcome", nil)
|
||||
}
|
||||
|
||||
func RoutePosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
func PagePosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
posts := GetPosts()
|
||||
Templates.ExecuteTemplate(w, "posts/index", posts)
|
||||
}
|
||||
|
||||
func RoutePostsWithID(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
func PagePostsWithID(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
posts := GetPostsWithID(ps.ByName("id"))
|
||||
Templates.ExecuteTemplate(w, "posts/index", posts)
|
||||
}
|
||||
|
|
|
@ -6,32 +6,32 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
type AdminPages struct {
|
||||
router *httprouter.Router
|
||||
}
|
||||
|
||||
func NewAdmin() *Admin {
|
||||
admin := &Admin{httprouter.New()}
|
||||
func NewAdminPages() *AdminPages {
|
||||
admin := &AdminPages{httprouter.New()}
|
||||
p := "/admin/" // prefix
|
||||
admin.router.GET(p, AdminRoot)
|
||||
admin.router.GET(p + "login", AdminLogin)
|
||||
admin.router.GET(p + "foo", AdminFoo)
|
||||
admin.router.GET(p, PageAdminRoot)
|
||||
admin.router.GET(p + "login", PageAdminLogin)
|
||||
admin.router.GET(p + "foo", PageAdminFoo)
|
||||
return admin
|
||||
}
|
||||
|
||||
func (a *Admin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (a *AdminPages) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
a.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func AdminRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
func PageAdminRoot(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
fmt.Fprint(w, "landed on root!")
|
||||
}
|
||||
|
||||
// Login the user into the admin panel
|
||||
func AdminLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
func PageAdminLogin(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
fmt.Fprint(w, "You now have a valid login!")
|
||||
}
|
||||
|
||||
func AdminFoo(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
||||
func PageAdminFoo(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
||||
fmt.Fprintf(w, "Adminpanel not implemented yet!")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue