clean up handlers
This commit is contained in:
parent
08328e1000
commit
a08740dec4
|
@ -3,11 +3,13 @@ package main
|
|||
import (
|
||||
"h4kor/owl-blogs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func RepoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
func repoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
html, err := owl.RenderUserList(repo)
|
||||
|
||||
|
@ -21,3 +23,87 @@ func RepoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
|
|||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
func userIndexHandler(repo owl.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())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
html, err := owl.RenderIndexPage(user)
|
||||
if err != nil {
|
||||
println("Error rendering index page: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering index page for user", userName)
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
func postHandler(repo owl.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())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
post, err := user.GetPost(postId)
|
||||
if err != nil {
|
||||
println("Error getting post: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("Post not found"))
|
||||
return
|
||||
}
|
||||
html, err := owl.RenderPost(post)
|
||||
if err != nil {
|
||||
println("Error rendering post: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering post", postId)
|
||||
w.Write([]byte(html))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func postMediaHandler(repo owl.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")
|
||||
filepath := ps.ByName("filepath")
|
||||
user, err := repo.GetUser(userName)
|
||||
|
||||
if err != nil {
|
||||
println("Error getting user: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
post, err := user.GetPost(postId)
|
||||
if err != nil {
|
||||
println("Error getting post: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("Post not found"))
|
||||
return
|
||||
}
|
||||
filepath = path.Join(post.MediaDir(), filepath)
|
||||
if _, err := os.Stat(filepath); err != nil {
|
||||
println("Error getting file: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("File not found"))
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, r, filepath)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,101 +4,16 @@ import (
|
|||
"h4kor/owl-blogs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func userHandler(repo owl.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())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
html, err := owl.RenderIndexPage(user)
|
||||
if err != nil {
|
||||
println("Error rendering index page: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering index page for user", userName)
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
func postHandler(repo owl.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())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
post, err := user.GetPost(postId)
|
||||
if err != nil {
|
||||
println("Error getting post: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("Post not found"))
|
||||
return
|
||||
}
|
||||
html, err := owl.RenderPost(post)
|
||||
if err != nil {
|
||||
println("Error rendering post: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering post", postId)
|
||||
w.Write([]byte(html))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func postMediaHandler(repo owl.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")
|
||||
filepath := ps.ByName("filepath")
|
||||
user, err := repo.GetUser(userName)
|
||||
|
||||
if err != nil {
|
||||
println("Error getting user: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("User not found"))
|
||||
return
|
||||
}
|
||||
post, err := user.GetPost(postId)
|
||||
if err != nil {
|
||||
println("Error getting post: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("Post not found"))
|
||||
return
|
||||
}
|
||||
filepath = path.Join(post.MediaDir(), filepath)
|
||||
if _, err := os.Stat(filepath); err != nil {
|
||||
println("Error getting file: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("File not found"))
|
||||
return
|
||||
}
|
||||
http.ServeFile(w, r, filepath)
|
||||
}
|
||||
}
|
||||
|
||||
func Router(repo owl.Repository) http.Handler {
|
||||
router := httprouter.New()
|
||||
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
|
||||
router.GET("/", RepoIndexHandler(repo))
|
||||
router.GET("/user/:user/", userHandler(repo))
|
||||
router.GET("/", repoIndexHandler(repo))
|
||||
router.GET("/user/:user/", userIndexHandler(repo))
|
||||
router.GET("/user/:user/posts/:post/", postHandler(repo))
|
||||
router.GET("/user/:user/posts/:post/media/*filepath", postMediaHandler(repo))
|
||||
return router
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package static
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func StaticHandler(repo owl.Repository) http.Handler {
|
||||
return http.StripPrefix(
|
||||
"/static/",
|
||||
http.FileServer(http.Dir(repo.StaticDir())),
|
||||
)
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
package static_test
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
"h4kor/owl-blogs/cmd/owl-web/static"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func randomName() string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyz")
|
||||
b := make([]rune, 8)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func testRepoName() string {
|
||||
return "/tmp/" + randomName()
|
||||
}
|
||||
|
||||
func getTestRepo() owl.Repository {
|
||||
repo, _ := owl.CreateRepository(testRepoName())
|
||||
return repo
|
||||
}
|
||||
|
||||
func TestDeliversStaticFilesOfRepo(t *testing.T) {
|
||||
repo := getTestRepo()
|
||||
// create test static file
|
||||
fileName := "test.txt"
|
||||
filePath := path.Join(repo.StaticDir(), fileName)
|
||||
expected := "ok"
|
||||
err := os.WriteFile(filePath, []byte(expected), 0644)
|
||||
|
||||
// Create Request and Response
|
||||
req, err := http.NewRequest("GET", "/static/test.txt", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
handler := http.Handler(static.StaticHandler(repo))
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check the status code is what we expect.
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check the response body is what we expect.
|
||||
if rr.Body.String() != expected {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
rr.Body.String(), expected)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue