package main import ( "h4kor/kiss-social" "net/http" "os" "strconv" "strings" ) 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 } // 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 } // 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) { users, err := repo.Users() if err != nil { println("Error getting users: ", err.Error()) w.Write([]byte("Error getting users")) return } w.Write([]byte("Index")) w.Write([]byte("