diff --git a/cmd/kiss-web/main.go b/cmd/kiss-web/main.go index 3ca6c6a..4fa4738 100644 --- a/cmd/kiss-web/main.go +++ b/cmd/kiss-web/main.go @@ -2,54 +2,54 @@ package main import ( "h4kor/kiss-social" - "h4kor/kiss-social/cmd/kiss-web/static" "net/http" "os" "strconv" - "strings" + + "github.com/julienschmidt/httprouter" ) -func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - // normalize the path - path := r.URL.Path - // remove leading '/' - if len(path) > 0 && path[0] == '/' { - path = path[1:] - } - // remove trailing '/' - if len(path) > 0 && path[len(path)-1] == '/' { - path = path[:len(path)-1] - } +// func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) { +// return func(w http.ResponseWriter, r *http.Request) { +// // normalize the path +// path := r.URL.Path +// // remove leading '/' +// if len(path) > 0 && path[0] == '/' { +// path = path[1:] +// } +// // remove trailing '/' +// if len(path) > 0 && path[len(path)-1] == '/' { +// path = path[:len(path)-1] +// } - // index page - if path == "" { - println("Index page") - indexHandler(repo)(w, r) - return - } +// // index page +// if path == "" { +// println("Index page") +// indexHandler(repo)(w, r) +// return +// } - // parse the path - parts := strings.Split(path, "/") - userName := parts[0] +// // parse the path +// parts := strings.Split(path, "/") +// userName := parts[0] - // only one part -> user page - if len(parts) == 1 { - println("User page") - userHandler(repo, userName)(w, r) - return - } +// // only one part -> user page +// if len(parts) == 1 { +// println("User page") +// userHandler(repo, userName)(w, r) +// return +// } - // multiple parts -> post page - println("Post page") - postId := strings.Join(parts[1:], "/") - postHandler(repo, userName, postId)(w, r) +// // multiple parts -> post page +// println("Post page") +// postId := strings.Join(parts[1:], "/") +// postHandler(repo, userName, postId)(w, r) - } -} +// } +// } -func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { +func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { html, err := kiss.RenderUserList(repo) if err != nil { @@ -63,8 +63,9 @@ func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) } } -func userHandler(repo kiss.Repository, userName string) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { +func userHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + userName := ps.ByName("user") user, err := repo.GetUser(userName) if err != nil { println("Error getting user: ", err.Error()) @@ -84,8 +85,10 @@ func userHandler(repo kiss.Repository, userName string) func(http.ResponseWriter } } -func postHandler(repo kiss.Repository, userName string, postId string) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { +func postHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + userName := ps.ByName("user") + postId := ps.ByName("post") user, err := repo.GetUser(userName) if err != nil { println("Error getting user: ", err.Error()) @@ -141,10 +144,13 @@ func main() { os.Exit(1) } - http.Handle("/static/", static.StaticHandler(repo)) - http.HandleFunc("/", handler(repo)) + router := httprouter.New() + router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir())) + router.GET("/", indexHandler(repo)) + router.GET("/user/:user", userHandler(repo)) + router.GET("/user/:user/posts/*post", postHandler(repo)) println("Listening on port", port) - http.ListenAndServe(":"+strconv.Itoa(port), nil) + http.ListenAndServe(":"+strconv.Itoa(port), router) } diff --git a/go.mod b/go.mod index 637433d..a3c7cba 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module h4kor/kiss-social go 1.18 require ( + github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/yuin/goldmark v1.4.13 // indirect github.com/yuin/goldmark-meta v1.1.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect diff --git a/go.sum b/go.sum index 89e54e4..f16969a 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc= diff --git a/post.go b/post.go index ddeb697..2dc8a9b 100644 --- a/post.go +++ b/post.go @@ -26,7 +26,7 @@ func (post Post) MediaDir() string { } func (post Post) UrlPath() string { - return post.user.Path() + "/" + post.id + return post.user.Path() + "/posts/" + post.id } func (post Post) Title() string { diff --git a/user.go b/user.go index 7ec8c6d..b72a6e7 100644 --- a/user.go +++ b/user.go @@ -27,7 +27,7 @@ func (user User) Dir() string { } func (user User) Path() string { - return "/" + user.name + return "/user/" + user.name } func (user User) PostDir() string {