serving static files
This commit is contained in:
parent
8a2496f6b7
commit
d961fb65e8
|
@ -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)
|
||||
|
|
|
@ -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())),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ .Title }}</title>
|
||||
<link rel="stylesheet" href="/static/pico.min.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
|
24
kiss_test.go
24
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())
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue