diff --git a/config/config.go b/config/config.go index abd66e4..700883d 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/config/fs.go b/config/fs.go new file mode 100644 index 0000000..aea1c92 --- /dev/null +++ b/config/fs.go @@ -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 +} diff --git a/main.go b/main.go index 2bf3124..095bcc0 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/routes/routes.go b/routes/routes.go index 5c10b3d..12d2f71 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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) }