writing initial static files

This commit is contained in:
Niko Abeler 2022-07-24 15:34:52 +02:00
parent b139b15ca6
commit 8a2496f6b7
8 changed files with 61 additions and 5 deletions

View File

@ -0,0 +1 @@
package static

View File

@ -0,0 +1 @@
package static_test

5
embed/initial/static/pico.min.css vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@ func (post Post) Dir() string {
return path.Join(post.user.Dir(), "public", post.id) return path.Join(post.user.Dir(), "public", post.id)
} }
func (post Post) Path() string { func (post Post) UrlPath() string {
return post.user.Path() + "/" + post.id return post.user.Path() + "/" + post.id
} }

View File

@ -56,7 +56,7 @@ func RenderIndexPage(user User) (string, error) {
postHtml := "" postHtml := ""
for _, postId := range posts { for _, postId := range posts {
post, _ := user.GetPost(postId) post, _ := user.GetPost(postId)
postHtml += "<h2><a href=\"" + post.Path() + "\">" + post.Title() + "</a></h2>\n" postHtml += "<h2><a href=\"" + post.UrlPath() + "\">" + post.Title() + "</a></h2>\n"
} }
data := PageContent{ data := PageContent{

View File

@ -1,14 +1,19 @@
package kiss package kiss
import ( import (
"embed"
_ "embed" _ "embed"
"fmt" "fmt"
"os" "os"
"path" "path"
) )
//go:embed embed/base.html //go:embed embed/initial/base.html
var base_template string var base_template string
//go:embed embed/initial/static/*
var static_files embed.FS
var VERSION = "0.0.1" var VERSION = "0.0.1"
type Repository struct { type Repository struct {
@ -22,8 +27,19 @@ func CreateRepository(name string) (Repository, error) {
return Repository{}, fmt.Errorf("Repository already exists") return Repository{}, fmt.Errorf("Repository already exists")
} }
os.Mkdir(name, 0755) os.Mkdir(newRepo.Dir(), 0755)
os.Mkdir(path.Join(name, "users"), 0755) os.Mkdir(newRepo.UsersDir(), 0755)
os.Mkdir(newRepo.StaticDir(), 0755)
// copy all files from static_files embed.FS to StaticDir
staticFiles, _ := static_files.ReadDir("embed/initial/static")
for _, file := range staticFiles {
if file.IsDir() {
continue
}
src_data, _ := static_files.ReadFile(file.Name())
os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644)
}
return newRepo, nil return newRepo, nil
} }
@ -42,6 +58,14 @@ func (repo Repository) Dir() string {
return repo.name return repo.name
} }
func (repo Repository) StaticDir() string {
return path.Join(repo.Dir(), "static")
}
func (repo Repository) UsersDir() string {
return path.Join(repo.Dir(), "users")
}
func (repo Repository) Users() ([]User, error) { func (repo Repository) Users() ([]User, error) {
userNames := listDir(path.Join(repo.Dir(), "users")) userNames := listDir(path.Join(repo.Dir(), "users"))
users := make([]User, len(userNames)) users := make([]User, len(userNames))

View File

@ -152,3 +152,28 @@ func TestCannotGetNonexistingUser(t *testing.T) {
t.Error("No error returned when getting non-existing user") t.Error("No error returned when getting non-existing user")
} }
} }
func TestGetStaticDirOfRepo(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
// Get the user
staticDir := repo.StaticDir()
if staticDir == "" {
t.Error("Static dir not returned")
}
}
func TestNewRepoGetsStaticFiles(t *testing.T) {
// Create a new user
repo, _ := kiss.CreateRepository(testRepoName())
if _, err := os.Stat(repo.StaticDir()); err != nil {
t.Error("Static directory not found")
}
dir, _ := os.Open(repo.StaticDir())
defer dir.Close()
files, _ := dir.Readdirnames(-1)
if len(files) == 0 {
t.Error("No static files found")
}
}