From d961fb65e86f91a2d56efa31f5e4193d65bfd8f1 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 24 Jul 2022 16:19:21 +0200 Subject: [PATCH] serving static files --- cmd/kiss-web/main.go | 2 + cmd/kiss-web/static/handler.go | 12 ++++++ cmd/kiss-web/static/handler_test.go | 62 +++++++++++++++++++++++++++++ embed/initial/base.html | 1 + kiss_test.go | 24 ++++++++++- repository.go | 2 +- repository_test.go | 30 +++----------- user.go | 2 +- 8 files changed, 107 insertions(+), 28 deletions(-) diff --git a/cmd/kiss-web/main.go b/cmd/kiss-web/main.go index f850842..da06764 100644 --- a/cmd/kiss-web/main.go +++ b/cmd/kiss-web/main.go @@ -2,6 +2,7 @@ package main import ( "h4kor/kiss-social" + "h4kor/kiss-social/cmd/kiss-web/static" "net/http" "os" "strconv" @@ -143,6 +144,7 @@ func main() { os.Exit(1) } + http.Handle("/static/", static.StaticHandler(repo)) http.HandleFunc("/", handler(repo)) println("Listening on port", port) diff --git a/cmd/kiss-web/static/handler.go b/cmd/kiss-web/static/handler.go index 4a9e036..dccbbcb 100644 --- a/cmd/kiss-web/static/handler.go +++ b/cmd/kiss-web/static/handler.go @@ -1 +1,13 @@ package static + +import ( + "h4kor/kiss-social" + "net/http" +) + +func StaticHandler(repo kiss.Repository) http.Handler { + return http.StripPrefix( + "/static/", + http.FileServer(http.Dir(repo.StaticDir())), + ) +} diff --git a/cmd/kiss-web/static/handler_test.go b/cmd/kiss-web/static/handler_test.go index 127a507..c929ea5 100644 --- a/cmd/kiss-web/static/handler_test.go +++ b/cmd/kiss-web/static/handler_test.go @@ -1 +1,63 @@ package static_test + +import ( + "h4kor/kiss-social" + "h4kor/kiss-social/cmd/kiss-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() kiss.Repository { + repo, _ := kiss.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) + } + +} diff --git a/embed/initial/base.html b/embed/initial/base.html index 20cab4b..4afaa2b 100644 --- a/embed/initial/base.html +++ b/embed/initial/base.html @@ -6,6 +6,7 @@ {{ .Title }} + diff --git a/kiss_test.go b/kiss_test.go index 66e6b50..d1f0b15 100644 --- a/kiss_test.go +++ b/kiss_test.go @@ -1,6 +1,28 @@ package kiss_test -import "h4kor/kiss-social" +import ( + "h4kor/kiss-social" + "math/rand" + "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 randomUserName() string { + return randomName() +} func getTestUser() kiss.User { repo, _ := kiss.CreateRepository(testRepoName()) diff --git a/repository.go b/repository.go index 86b6bae..673370b 100644 --- a/repository.go +++ b/repository.go @@ -67,7 +67,7 @@ func (repo Repository) UsersDir() string { } func (repo Repository) Users() ([]User, error) { - userNames := listDir(path.Join(repo.Dir(), "users")) + userNames := listDir(repo.UsersDir()) users := make([]User, len(userNames)) for i, name := range userNames { users[i] = User{repo: repo, name: name} diff --git a/repository_test.go b/repository_test.go index 2bc18a1..8c5acff 100644 --- a/repository_test.go +++ b/repository_test.go @@ -2,31 +2,11 @@ package kiss_test import ( "h4kor/kiss-social" - "math/rand" "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 randomUserName() string { - return randomName() -} - func TestCanCreateRepository(t *testing.T) { repoName := testRepoName() _, err := kiss.CreateRepository(repoName) @@ -92,19 +72,19 @@ func TestCreateUserAddsPublicFolder(t *testing.T) { } } -func CanListRepoUsers(t *testing.T) { +func TestCanListRepoUsers(t *testing.T) { // Create a new user repo, _ := kiss.CreateRepository(testRepoName()) user1, _ := repo.CreateUser(randomUserName()) user2, _ := repo.CreateUser(randomUserName()) // Create a new post users, _ := repo.Users() - if len(users) == 2 { - t.Error("No users found") + if len(users) != 2 { + t.Error("Wrong number of users returned, expected 2, got ", len(users)) } for _, user := range users { - if user.Name() == user1.Name() || user.Name() == user2.Name() { - t.Error("User found") + if user.Name() != user1.Name() && user.Name() != user2.Name() { + t.Error("User found: " + user.Name()) } } } diff --git a/user.go b/user.go index 22577cc..c6ba20e 100644 --- a/user.go +++ b/user.go @@ -15,7 +15,7 @@ type User struct { } func (user User) Dir() string { - return path.Join(user.repo.Dir(), "users", user.name) + return path.Join(user.repo.UsersDir(), user.name) } func (user User) Path() string {