diff --git a/.gitignore b/.gitignore index 9b880bb..45b6a0c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,6 @@ sample.txt *.toml # Exclude generated file +assets/ shiori* *.db \ No newline at end of file diff --git a/cmd/serve.go b/cmd/serve.go index dd45f52..ab532e8 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,9 +1,11 @@ package cmd import ( + "bytes" "crypto/rand" "encoding/json" "fmt" + "github.com/RadhiFadlillah/shiori/assets" "github.com/RadhiFadlillah/shiori/model" "github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go/request" @@ -12,6 +14,8 @@ import ( "github.com/spf13/cobra" "golang.org/x/crypto/bcrypt" "html/template" + "io" + "mime" "net/http" fp "path/filepath" "strings" @@ -20,6 +24,7 @@ import ( var ( jwtKey []byte + tplCache *template.Template serveCmd = &cobra.Command{ Use: "serve", Short: "Serve web app for managing bookmarks.", @@ -34,6 +39,19 @@ var ( return } + // Prepare template + tplFile, err := assets.ReadFile("content.html") + if err != nil { + cError.Println("Failed generating HTML template") + return + } + + tplCache, err = template.New("content.html").Parse(string(tplFile)) + if err != nil { + cError.Println("Failed generating HTML template") + return + } + // Create router router := httprouter.New() @@ -70,10 +88,26 @@ func init() { } func serveFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - filepath := r.URL.Path - filepath = strings.TrimPrefix(filepath, "/") - filepath = fp.Join("view", filepath) - http.ServeFile(w, r, filepath) + // Read asset path + path := r.URL.Path + if path[0:1] == "/" { + path = path[1:] + } + + // Load asset + asset, err := assets.ReadFile(path) + checkError(err) + + // Set response header content type + ext := fp.Ext(path) + mimeType := mime.TypeByExtension(ext) + if mimeType != "" { + w.Header().Set("Content-Type", mimeType) + } + + // Serve asset + buffer := bytes.NewBuffer(asset) + io.Copy(w, buffer) } func serveIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -84,13 +118,21 @@ func serveIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params return } - filepath := fp.Join("view", "index.html") - http.ServeFile(w, r, filepath) + asset, err := assets.ReadFile("index.html") + checkError(err) + + w.Header().Set("Content-Type", "text/html") + buffer := bytes.NewBuffer(asset) + io.Copy(w, buffer) } func serveLoginPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - filepath := fp.Join("view", "login.html") - http.ServeFile(w, r, filepath) + asset, err := assets.ReadFile("login.html") + checkError(err) + + w.Header().Set("Content-Type", "text/html") + buffer := bytes.NewBuffer(asset) + io.Copy(w, buffer) } func serveBookmarkCache(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { @@ -106,10 +148,7 @@ func serveBookmarkCache(w http.ResponseWriter, r *http.Request, ps httprouter.Pa } // Read template - templates, err := template.New("content.html").ParseFiles("view/content.html") - checkError(err) - - err = templates.ExecuteTemplate(w, "content.html", &bookmarks[0]) + err = tplCache.Execute(w, &bookmarks[0]) checkError(err) } diff --git a/filebox.json b/filebox.json new file mode 100644 index 0000000..bdbb5e5 --- /dev/null +++ b/filebox.json @@ -0,0 +1,17 @@ +{ + "pkg": "assets", + "dest": "./assets/", + "fmt": true, + "compression": { + "compress": true, + "method": "" + }, + "output": "assets.go", + "custom": [{ + "files": ["./view/"], + "base": "view/", + "exclude": [ + "less/*.less" + ] + }] +} \ No newline at end of file diff --git a/main.go b/main.go index 03cd44b..c44a6aa 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +//go:generate fileb0x filebox.json package main import (