43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
type SitePages struct {
|
|
router *httprouter.Router
|
|
}
|
|
|
|
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 *SitePages) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
s.router.ServeHTTP(w, r)
|
|
}
|
|
|
|
// the function for route /
|
|
func PageIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
Templates.ExecuteTemplate(w, "welcome", nil)
|
|
}
|
|
|
|
func PagePosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
posts := GetPosts()
|
|
Templates.ExecuteTemplate(w, "posts/index", posts)
|
|
}
|
|
|
|
func PagePostsWithID(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
posts := GetPostsWithID(ps.ByName("id"))
|
|
Templates.ExecuteTemplate(w, "posts/index", posts)
|
|
}
|
|
|
|
func NoOp(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
|
|
fmt.Fprintf(w, "Not implemented yet!")
|
|
}
|