mirror of https://github.com/jlelse/GoBlog
Allow root post
This commit is contained in:
parent
9ee3c0c730
commit
8dbf795902
8
http.go
8
http.go
|
@ -56,15 +56,13 @@ func buildHandler() (http.Handler, error) {
|
|||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.StripSlashes)
|
||||
|
||||
r.Get("/", hello)
|
||||
|
||||
allPostPaths, err := allPostPaths()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, path := range allPostPaths {
|
||||
if path != "" {
|
||||
r.Get("/"+path, servePost)
|
||||
r.Get(path, servePost)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -72,10 +70,6 @@ func buildHandler() (http.Handler, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func hello(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte("Hello World!"))
|
||||
}
|
||||
|
||||
type dynamicHandler struct {
|
||||
realHandler http.Handler
|
||||
changeMutex *sync.Mutex
|
||||
|
|
6
posts.go
6
posts.go
|
@ -18,7 +18,11 @@ type post struct {
|
|||
}
|
||||
|
||||
func servePost(w http.ResponseWriter, r *http.Request) {
|
||||
post, err := getPost(strings.TrimSuffix(strings.TrimPrefix(r.RequestURI, "/"), "/"), r.Context())
|
||||
path := r.RequestURI
|
||||
if len(path) > 1 {
|
||||
path = strings.TrimSuffix(path, "/")
|
||||
}
|
||||
post, err := getPost(path, r.Context())
|
||||
if err == postNotFound {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue