diff --git a/cmd/kiss-web/main.go b/cmd/kiss-web/main.go
index da06764..3ca6c6a 100644
--- a/cmd/kiss-web/main.go
+++ b/cmd/kiss-web/main.go
@@ -50,19 +50,16 @@ func handler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
func indexHandler(repo kiss.Repository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
- users, err := repo.Users()
+ html, err := kiss.RenderUserList(repo)
+
if err != nil {
- println("Error getting users: ", err.Error())
- w.Write([]byte("Error getting users"))
+ println("Error rendering index: ", err.Error())
+ w.WriteHeader(http.StatusInternalServerError)
+ w.Write([]byte("Internal server error"))
return
}
- w.Write([]byte("Index"))
- w.Write([]byte("
"))
- for _, user := range users {
- w.Write([]byte("- "))
- w.Write([]byte(user.Name()))
- w.Write([]byte("
"))
- }
+ println("Rendering index")
+ w.Write([]byte(html))
}
}
diff --git a/embed/initial/repo_base.html b/embed/initial/repo_base.html
new file mode 100644
index 0000000..4afaa2b
--- /dev/null
+++ b/embed/initial/repo_base.html
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+ {{ .Title }}
+
+
+
+
+ {{ .Content }}
+
+
+
\ No newline at end of file
diff --git a/embed/user-list.html b/embed/user-list.html
index fe33eac..f0de97a 100644
--- a/embed/user-list.html
+++ b/embed/user-list.html
@@ -1,8 +1,8 @@
-{{range .Users}}
+{{range .}}
diff --git a/kiss_test.go b/kiss_test.go
index d1f0b15..9818169 100644
--- a/kiss_test.go
+++ b/kiss_test.go
@@ -30,6 +30,11 @@ func getTestUser() kiss.User {
return user
}
+func getTestRepo() kiss.Repository {
+ repo, _ := kiss.CreateRepository(testRepoName())
+ return repo
+}
+
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
diff --git a/renderer.go b/renderer.go
index f93bd18..712b941 100644
--- a/renderer.go
+++ b/renderer.go
@@ -66,16 +66,40 @@ func RenderIndexPage(user User) (string, error) {
var html bytes.Buffer
t, err := template.New("index").Parse(baseTemplate)
+ if err != nil {
+ return "", err
+ }
t.Execute(&html, data)
- return html.String(), err
+ return html.String(), nil
}
-// func RenderUserList(user User) (string, error) {
-// base_template, _ := user.Template()
-// users, _ := user.repo.Users()
-// template.New("user_list").Parse()
-// return strings.Replace(template, "{{content}}", userHtml, -1), nil
-// }
+func RenderUserList(repo Repository) (string, error) {
+ baseTemplate, _ := repo.Template()
+ users, _ := repo.Users()
+ t, err := template.New("user_list").Parse(userListTemplateStr)
+ if err != nil {
+ return "", err
+ }
+
+ var userHtml bytes.Buffer
+ t.Execute(&userHtml, users)
+
+ data := PageContent{
+ Title: "Index",
+ Content: template.HTML(userHtml.String()),
+ }
+
+ var html bytes.Buffer
+ t, err = template.New("index").Parse(baseTemplate)
+ if err != nil {
+ return "", err
+ }
+
+ t.Execute(&html, data)
+
+ return html.String(), nil
+
+}
diff --git a/renderer_test.go b/renderer_test.go
index f3ef05a..172a28b 100644
--- a/renderer_test.go
+++ b/renderer_test.go
@@ -2,6 +2,8 @@ package kiss_test
import (
"h4kor/kiss-social"
+ "os"
+ "path"
"strings"
"testing"
)
@@ -49,3 +51,34 @@ func TestCanRenderIndexPage(t *testing.T) {
t.Error("Post title not rendered. Got: " + result)
}
}
+
+func TestRenderIndexPageWithBrokenBaseTemplate(t *testing.T) {
+ user := getTestUser()
+ user.CreateNewPost("testpost1")
+ user.CreateNewPost("testpost2")
+
+ os.WriteFile(path.Join(user.Dir(), "meta/base.html"), []byte("{{content}}"), 0644)
+
+ _, err := kiss.RenderIndexPage(user)
+ if err == nil {
+ t.Error("Expected error rendering index page, got nil")
+ }
+}
+
+func TestRenderUserList(t *testing.T) {
+ repo := getTestRepo()
+ repo.CreateUser("user1")
+ repo.CreateUser("user2")
+
+ result, err := kiss.RenderUserList(repo)
+ if err != nil {
+ t.Error("Error rendering user list: " + err.Error())
+ }
+
+ if !strings.Contains(result, "user1") {
+ t.Error("Post title not rendered. Got: " + result)
+ }
+ if !strings.Contains(result, "user2") {
+ t.Error("Post title not rendered. Got: " + result)
+ }
+}
diff --git a/repository.go b/repository.go
index 673370b..0bb7a09 100644
--- a/repository.go
+++ b/repository.go
@@ -4,6 +4,7 @@ import (
"embed"
_ "embed"
"fmt"
+ "io/ioutil"
"os"
"path"
)
@@ -11,7 +12,7 @@ import (
//go:embed embed/initial/base.html
var base_template string
-//go:embed embed/initial/static/*
+//go:embed embed/*
var static_files embed.FS
var VERSION = "0.0.1"
@@ -40,6 +41,10 @@ func CreateRepository(name string) (Repository, error) {
src_data, _ := static_files.ReadFile(file.Name())
os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644)
}
+
+ // copy repo_base.html to base.html
+ src_data, _ := static_files.ReadFile("embed/initial/repo_base.html")
+ os.WriteFile(newRepo.Dir()+"/base.html", src_data, 0644)
return newRepo, nil
}
@@ -66,6 +71,16 @@ func (repo Repository) UsersDir() string {
return path.Join(repo.Dir(), "users")
}
+func (repo Repository) Template() (string, error) {
+ // load base.html
+ path := path.Join(repo.Dir(), "base.html")
+ base_html, err := ioutil.ReadFile(path)
+ if err != nil {
+ return "", err
+ }
+ return string(base_html), nil
+}
+
func (repo Repository) Users() ([]User, error) {
userNames := listDir(repo.UsersDir())
users := make([]User, len(userNames))
diff --git a/repository_test.go b/repository_test.go
index 8c5acff..c408c23 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -157,3 +157,24 @@ func TestNewRepoGetsStaticFiles(t *testing.T) {
t.Error("No static files found")
}
}
+
+func TestNewRepoGetsBaseHtml(t *testing.T) {
+ // Create a new user
+ repo, _ := kiss.CreateRepository(testRepoName())
+ if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil {
+ t.Error("Base html file not found")
+ }
+}
+
+func TestCanGetRepoTemplate(t *testing.T) {
+ // Create a new user
+ repo, _ := kiss.CreateRepository(testRepoName())
+ // Get the user
+ template, err := repo.Template()
+ if err != nil {
+ t.Error("Error getting template: ", err.Error())
+ }
+ if template == "" {
+ t.Error("Template not returned")
+ }
+}