Bundle the view in executable

This commit is contained in:
Radhi Fadlillah 2018-02-23 16:51:06 +07:00
parent 529b089fd9
commit 786cae511a
4 changed files with 70 additions and 12 deletions

1
.gitignore vendored
View File

@ -6,5 +6,6 @@ sample.txt
*.toml *.toml
# Exclude generated file # Exclude generated file
assets/
shiori* shiori*
*.db *.db

View File

@ -1,9 +1,11 @@
package cmd package cmd
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/RadhiFadlillah/shiori/assets"
"github.com/RadhiFadlillah/shiori/model" "github.com/RadhiFadlillah/shiori/model"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/dgrijalva/jwt-go/request" "github.com/dgrijalva/jwt-go/request"
@ -12,6 +14,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"html/template" "html/template"
"io"
"mime"
"net/http" "net/http"
fp "path/filepath" fp "path/filepath"
"strings" "strings"
@ -20,6 +24,7 @@ import (
var ( var (
jwtKey []byte jwtKey []byte
tplCache *template.Template
serveCmd = &cobra.Command{ serveCmd = &cobra.Command{
Use: "serve", Use: "serve",
Short: "Serve web app for managing bookmarks.", Short: "Serve web app for managing bookmarks.",
@ -34,6 +39,19 @@ var (
return 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 // Create router
router := httprouter.New() router := httprouter.New()
@ -70,10 +88,26 @@ func init() {
} }
func serveFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func serveFiles(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filepath := r.URL.Path // Read asset path
filepath = strings.TrimPrefix(filepath, "/") path := r.URL.Path
filepath = fp.Join("view", filepath) if path[0:1] == "/" {
http.ServeFile(w, r, filepath) 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) { 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 return
} }
filepath := fp.Join("view", "index.html") asset, err := assets.ReadFile("index.html")
http.ServeFile(w, r, filepath) 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) { func serveLoginPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filepath := fp.Join("view", "login.html") asset, err := assets.ReadFile("login.html")
http.ServeFile(w, r, filepath) 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) { 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 // Read template
templates, err := template.New("content.html").ParseFiles("view/content.html") err = tplCache.Execute(w, &bookmarks[0])
checkError(err)
err = templates.ExecuteTemplate(w, "content.html", &bookmarks[0])
checkError(err) checkError(err)
} }

17
filebox.json Normal file
View File

@ -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"
]
}]
}

View File

@ -1,3 +1,4 @@
//go:generate fileb0x filebox.json
package main package main
import ( import (