1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: serve static file from local fs when on dev (fix #176)

This commit is contained in:
Ferdinand Mütsch 2021-04-16 12:24:19 +02:00
parent a9739a6db0
commit b9ea6530f9
4 changed files with 31 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
@ -59,6 +60,7 @@ var emailProviders = []string{
var cfg *Config
var cFlag = flag.String("config", defaultConfigPath, "config file location")
var env string
type appConfig struct {
AggregationTime string `yaml:"aggregation_time" default:"02:15" env:"WAKAPI_AGGREGATION_TIME"`
@ -287,8 +289,14 @@ func readColors() map[string]map[string]string {
// Extracted from Wakatime website with XPath (see below) and did a bit of regex magic after.
// $x('//span[@class="editor-icon tip"]/@data-original-title').map(e => e.nodeValue)
// $x('//span[@class="editor-icon tip"]/div[1]/text()').map(e => e.nodeValue)
raw := data.ColorsFile
if IsDev(env) {
raw, _ = ioutil.ReadFile("data/colors.json")
}
var colors = make(map[string]map[string]string)
if err := json.Unmarshal(data.ColorsFile, &colors); err != nil {
if err := json.Unmarshal(raw, &colors); err != nil {
logbuch.Fatal(err.Error())
}
@ -335,6 +343,7 @@ func Load(version string) *Config {
logbuch.Fatal("failed to read config: %v", err)
}
env = config.Env
config.Version = strings.TrimSpace(version)
config.App.Colors = readColors()
config.Db.Dialect = resolveDbDialect(config.Db.Type)

14
config/fs.go Normal file
View File

@ -0,0 +1,14 @@
package config
import (
"io/fs"
"os"
)
// ChooseFS returns a local (DirFS) file system when on 'dev' environment and the given go-embed file system otherwise
func ChooseFS(localDir string, embeddedFS fs.FS) fs.FS {
if Get().IsDev() {
return os.DirFS(localDir)
}
return embeddedFS
}

View File

@ -205,7 +205,8 @@ func main() {
// Static Routes
// https://github.com/golang/go/issues/43431
static, _ := fs.Sub(staticFiles, "static")
embeddedStatic, _ := fs.Sub(staticFiles, "static")
static := conf.ChooseFS("static", embeddedStatic)
fileServer := http.FileServer(utils.NeuteredFileSystem{Fs: http.FS(static)})
router.PathPrefix("/contribute.json").Handler(fileServer)
router.PathPrefix("/assets").Handler(fileServer)

View File

@ -24,7 +24,6 @@ type action func(w http.ResponseWriter, r *http.Request) (int, string, string)
var templates map[string]*template.Template
func loadTemplates() {
const tplPath = "/views"
tpls := template.New("").Funcs(template.FuncMap{
"json": utils.Json,
"date": utils.FormatDateHuman,
@ -57,7 +56,10 @@ func loadTemplates() {
})
templates = make(map[string]*template.Template)
files, err := fs.ReadDir(views.TemplateFiles, ".")
// Use local file system when in 'dev' environment, go embed file system otherwise
templateFs := config.ChooseFS("views", views.TemplateFiles)
files, err := fs.ReadDir(templateFs, ".")
if err != nil {
panic(err)
}
@ -68,7 +70,7 @@ func loadTemplates() {
continue
}
templateFile, err := views.TemplateFiles.Open(tplName)
templateFile, err := templateFs.Open(tplName)
if err != nil {
panic(err)
}