refactor mux path routes
legend has it that if you refactor your code enough times, one day you will finally be happy
This commit is contained in:
parent
82fd17c836
commit
21912d4ec2
17 changed files with 102 additions and 469 deletions
16
view/blog.go
16
view/blog.go
|
|
@ -44,17 +44,13 @@ var mdRenderer = html.NewRenderer(html.RendererOptions{
|
|||
})
|
||||
|
||||
func BlogHandler(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.Count(r.URL.Path, "/") > 1 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
|
||||
if len(r.URL.Path) > 1 {
|
||||
ServeBlogPost(app, r.URL.Path[1:]).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
mux.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
ServeBlogPost(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
dbPosts, err := controller.GetBlogPosts(app.DB, true, -1, 0)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
|
|
@ -111,6 +107,8 @@ func BlogHandler(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
})
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func ServeBlogPost(app *model.AppState, blogPostID string) http.Handler {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue