Compare commits
4 Commits
70936659da
...
a08740dec4
Author | SHA1 | Date |
---|---|---|
Niko Abeler | a08740dec4 | |
Niko Abeler | 08328e1000 | |
Niko Abeler | 52ce044f88 | |
Niko Abeler | 2d17551b1d |
|
@ -0,0 +1,109 @@
|
|||
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) {
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
html, err := owl.RenderUserList(repo)
|
||||
|
||||
if err != nil {
|
||||
println("Error rendering index: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering index")
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
package main_test
|
||||
|
||||
import (
|
||||
"h4kor/owl-blogs"
|
||||
"h4kor/owl-blogs/cmd/owl-web"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"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 TestRepoIndexHandler(t *testing.T) {
|
||||
repo := getTestRepo()
|
||||
repo.CreateUser("user_1")
|
||||
repo.CreateUser("user_2")
|
||||
|
||||
// Create Request and Response
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
router := main.Router(repo)
|
||||
router.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 contains names of users
|
||||
if !strings.Contains(rr.Body.String(), "user_1") {
|
||||
t.Error("user_1 not listed on index page. Got: ")
|
||||
t.Error(rr.Body.String())
|
||||
}
|
||||
if !strings.Contains(rr.Body.String(), "user_2") {
|
||||
t.Error("user_2 not listed on index page. Got: ")
|
||||
t.Error(rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserIndexHandler(t *testing.T) {
|
||||
repo := getTestRepo()
|
||||
user, _ := repo.CreateUser("test-1")
|
||||
user.CreateNewPost("post-1")
|
||||
|
||||
// Create Request and Response
|
||||
req, err := http.NewRequest("GET", user.UrlPath(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
router := main.Router(repo)
|
||||
router.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 contains names of users
|
||||
if !strings.Contains(rr.Body.String(), "post-1") {
|
||||
t.Error("post-1 not listed on index page. Got: ")
|
||||
t.Error(rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostHandler(t *testing.T) {
|
||||
repo := getTestRepo()
|
||||
user, _ := repo.CreateUser("test-1")
|
||||
post, _ := user.CreateNewPost("post-1")
|
||||
|
||||
// Create Request and Response
|
||||
req, err := http.NewRequest("GET", post.UrlPath(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
router := main.Router(repo)
|
||||
router.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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostMediaHandler(t *testing.T) {
|
||||
repo := getTestRepo()
|
||||
user, _ := repo.CreateUser("test-1")
|
||||
post, _ := user.CreateNewPost("post-1")
|
||||
|
||||
// Create test media file
|
||||
path := path.Join(post.MediaDir(), "data.txt")
|
||||
println("Creating test media file:", path)
|
||||
err := os.WriteFile(path, []byte("test"), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create Request and Response
|
||||
println(post.UrlMediaPath("data.txt"))
|
||||
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
router := main.Router(repo)
|
||||
router.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 contains data of media file
|
||||
if !(rr.Body.String() == "test") {
|
||||
t.Error("Got wrong media file content. Expected 'test' Got: ")
|
||||
t.Error(rr.Body.String())
|
||||
}
|
||||
}
|
|
@ -9,111 +9,14 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// func handler(repo owl.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 owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
html, err := owl.RenderUserList(repo)
|
||||
|
||||
if err != nil {
|
||||
println("Error rendering index: ", err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering index")
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
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 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/", userIndexHandler(repo))
|
||||
router.GET("/user/:user/posts/:post/", postHandler(repo))
|
||||
router.GET("/user/:user/posts/:post/media/*filepath", postMediaHandler(repo))
|
||||
return router
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -144,12 +47,7 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
router := Router(repo)
|
||||
println("Listening on port", port)
|
||||
http.ListenAndServe(":"+strconv.Itoa(port), 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)
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
{{range .}}
|
||||
<ul>
|
||||
<li>
|
||||
<a href="{{ .Path }}">
|
||||
<a href="{{ .UrlPath }}">
|
||||
{{ .Name }}
|
||||
</a>
|
||||
</li>
|
||||
|
|
10
post.go
10
post.go
|
@ -17,6 +17,10 @@ type Post struct {
|
|||
title string
|
||||
}
|
||||
|
||||
func (post Post) Id() string {
|
||||
return post.id
|
||||
}
|
||||
|
||||
func (post Post) Dir() string {
|
||||
return path.Join(post.user.Dir(), "public", post.id)
|
||||
}
|
||||
|
@ -26,7 +30,11 @@ func (post Post) MediaDir() string {
|
|||
}
|
||||
|
||||
func (post Post) UrlPath() string {
|
||||
return post.user.Path() + "/posts/" + post.id
|
||||
return post.user.UrlPath() + "posts/" + post.id + "/"
|
||||
}
|
||||
|
||||
func (post Post) UrlMediaPath(filename string) string {
|
||||
return post.UrlPath() + "media/" + filename
|
||||
}
|
||||
|
||||
func (post Post) Title() string {
|
||||
|
|
33
post_test.go
33
post_test.go
|
@ -22,3 +22,36 @@ func TestMediaDir(t *testing.T) {
|
|||
t.Error("Wrong MediaDir. Got: " + result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostUrlPath(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/"
|
||||
if !(post.UrlPath() == expected) {
|
||||
t.Error("Wrong url path")
|
||||
t.Error("Expected: " + expected)
|
||||
t.Error(" Got: " + post.UrlPath())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostUrlMediaPath(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/data.png"
|
||||
if !(post.UrlMediaPath("data.png") == expected) {
|
||||
t.Error("Wrong url path")
|
||||
t.Error("Expected: " + expected)
|
||||
t.Error(" Got: " + post.UrlPath())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostUrlMediaPathWithSubDir(t *testing.T) {
|
||||
user := getTestUser()
|
||||
post, _ := user.CreateNewPost("testpost")
|
||||
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/foo/data.png"
|
||||
if !(post.UrlMediaPath("foo/data.png") == expected) {
|
||||
t.Error("Wrong url path")
|
||||
t.Error("Expected: " + expected)
|
||||
t.Error(" Got: " + post.UrlPath())
|
||||
}
|
||||
}
|
||||
|
|
4
user.go
4
user.go
|
@ -25,8 +25,8 @@ func (user User) Dir() string {
|
|||
return path.Join(user.repo.UsersDir(), user.name)
|
||||
}
|
||||
|
||||
func (user User) Path() string {
|
||||
return "/user/" + user.name
|
||||
func (user User) UrlPath() string {
|
||||
return "/user/" + user.name + "/"
|
||||
}
|
||||
|
||||
func (user User) PostDir() string {
|
||||
|
|
|
@ -138,3 +138,10 @@ func TestCanLoadPost(t *testing.T) {
|
|||
t.Error("Wrong title, Got: " + post.Title())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserUrlPath(t *testing.T) {
|
||||
user := getTestUser()
|
||||
if !(user.UrlPath() == "/user/"+user.Name()+"/") {
|
||||
t.Error("Wrong url path, Expected: " + "/user/" + user.Name() + "/" + " Got: " + user.UrlPath())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue