serving static files

This commit is contained in:
Niko Abeler 2022-07-24 16:19:21 +02:00
parent 8a2496f6b7
commit d961fb65e8
8 changed files with 107 additions and 28 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"h4kor/kiss-social" "h4kor/kiss-social"
"h4kor/kiss-social/cmd/kiss-web/static"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -143,6 +144,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
http.Handle("/static/", static.StaticHandler(repo))
http.HandleFunc("/", handler(repo)) http.HandleFunc("/", handler(repo))
println("Listening on port", port) println("Listening on port", port)

View File

@ -1 +1,13 @@
package static 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())),
)
}

View File

@ -1 +1,63 @@
package static_test 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)
}
}

View File

@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title> <title>{{ .Title }}</title>
<link rel="stylesheet" href="/static/pico.min.css">
</head> </head>
<body> <body>

View File

@ -1,6 +1,28 @@
package kiss_test 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 { func getTestUser() kiss.User {
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())

View File

@ -67,7 +67,7 @@ func (repo Repository) UsersDir() string {
} }
func (repo Repository) Users() ([]User, error) { func (repo Repository) Users() ([]User, error) {
userNames := listDir(path.Join(repo.Dir(), "users")) userNames := listDir(repo.UsersDir())
users := make([]User, len(userNames)) users := make([]User, len(userNames))
for i, name := range userNames { for i, name := range userNames {
users[i] = User{repo: repo, name: name} users[i] = User{repo: repo, name: name}

View File

@ -2,31 +2,11 @@ package kiss_test
import ( import (
"h4kor/kiss-social" "h4kor/kiss-social"
"math/rand"
"os" "os"
"path" "path"
"testing" "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) { func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName() repoName := testRepoName()
_, err := kiss.CreateRepository(repoName) _, 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 // Create a new user
repo, _ := kiss.CreateRepository(testRepoName()) repo, _ := kiss.CreateRepository(testRepoName())
user1, _ := repo.CreateUser(randomUserName()) user1, _ := repo.CreateUser(randomUserName())
user2, _ := repo.CreateUser(randomUserName()) user2, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
users, _ := repo.Users() users, _ := repo.Users()
if len(users) == 2 { if len(users) != 2 {
t.Error("No users found") t.Error("Wrong number of users returned, expected 2, got ", len(users))
} }
for _, user := range users { for _, user := range users {
if user.Name() == user1.Name() || user.Name() == user2.Name() { if user.Name() != user1.Name() && user.Name() != user2.Name() {
t.Error("User found") t.Error("User found: " + user.Name())
} }
} }
} }

View File

@ -15,7 +15,7 @@ type User struct {
} }
func (user User) Dir() string { 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 { func (user User) Path() string {