about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-04-12 14:45:43 -0400
committerBen Morrison <ben@gbmor.dev>2020-04-12 15:46:23 -0400
commit0daa948c9c6b507f1f087d57941ef35c26c97ae0 (patch)
treeef671bf2b358d568472faecb778c9499c50df024
parentf2bf68aedafb7b1df3c8d853ed7c9f7d35029de3 (diff)
downloadgetwtxt-0daa948c9c6b507f1f087d57941ef35c26c97ae0.tar.gz
Allowing static files to be served directly from
a directory specified in the config.

Added a handler to svc.go:Start() to serve /static/
Added default value in conf.go for "StaticFilesDirectory" in the
config file.
Read the value of StaticFilesDirectory in config to the config
global in conf.go
-rw-r--r--getwtxt.yml4
-rw-r--r--svc/conf.go6
-rw-r--r--svc/svc.go11
3 files changed, 18 insertions, 3 deletions
diff --git a/getwtxt.yml b/getwtxt.yml
index 8078245..8945993 100644
--- a/getwtxt.yml
+++ b/getwtxt.yml
@@ -45,6 +45,10 @@ DatabasePath: "getwtxt.db"
 #     tmpl/index.html
 AssetsDirectory: "assets"
 
+# The path to the static files directory.
+# Will be served as /static
+StaticFilesDirectory: "/usr/local/getwtxt/static"
+
 # If true, getwtxt will send all log messages, including
 # requests, to stdout. It will ignore any set log file.
 # Useful for debugging, but you probably want to keep
diff --git a/svc/conf.go b/svc/conf.go
index ae52219..8968ed4 100644
--- a/svc/conf.go
+++ b/svc/conf.go
@@ -41,7 +41,8 @@ type Configuration struct {
 	ReqLog        string        `yaml:"RequestLog"`
 	DBType        string        `yaml:"DatabaseType"`
 	DBPath        string        `yaml:"DatabasePath"`
-	AssetsDir     string        `yaml:"-"`
+	AssetsDir     string        `yaml:"AssetsDirectory"`
+	StaticDir     string        `yaml:"StaticFilesDirectory"`
 	StdoutLogging bool          `yaml:"StdoutLogging"`
 	CacheInterval time.Duration `yaml:"StatusFetchInterval"`
 	DBInterval    time.Duration `yaml:"DatabasePushInterval"`
@@ -120,6 +121,7 @@ func setConfigDefaults() {
 	viper.SetDefault("RequestLog", "logs/request.log")
 	viper.SetDefault("DatabasePath", "getwtxt.db")
 	viper.SetDefault("AssetsDirectory", "assets")
+	viper.SetDefault("StaticFilesDirectory", "static")
 	viper.SetDefault("DatabaseType", "leveldb")
 	viper.SetDefault("StdoutLogging", false)
 	viper.SetDefault("ReCacheInterval", "1h")
@@ -167,6 +169,7 @@ func bindConfig() {
 	confObj.DBType = strings.ToLower(viper.GetString("DatabaseType"))
 	confObj.DBPath = viper.GetString("DatabasePath")
 	confObj.AssetsDir = viper.GetString("AssetsDirectory")
+	confObj.StaticDir = viper.GetString("StaticFilesDirectory")
 	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
 	confObj.CacheInterval = viper.GetDuration("StatusFetchInterval")
 	confObj.DBInterval = viper.GetDuration("DatabasePushInterval")
@@ -207,4 +210,5 @@ func announceConfig() {
 	log.Printf("Using %v database: %v\n", confObj.DBType, confObj.DBPath)
 	log.Printf("Database push interval: %v\n", confObj.DBInterval)
 	log.Printf("User status fetch interval: %v\n", confObj.CacheInterval)
+	log.Printf("Static files directory: %v", confObj.StaticDir)
 }
diff --git a/svc/svc.go b/svc/svc.go
index 4e98c94..8c1c63a 100644
--- a/svc/svc.go
+++ b/svc/svc.go
@@ -39,6 +39,13 @@ func Start() {
 	// handlers/paths
 	index := mux.NewRouter().StrictSlash(true)
 	setIndexRouting(index)
+
+	// Serve the raw contents of this directory so users can
+	// place miscellaneous files in it to use with the template.
+	index.PathPrefix("/static").
+		Methods("GET", "HEAD").
+		Handler(http.StripPrefix("/static", http.FileServer(http.Dir(confObj.StaticDir))))
+
 	api := index.PathPrefix("/api").Subrouter()
 	setEndpointRouting(api)
 
@@ -84,8 +91,8 @@ func setIndexRouting(index *mux.Router) {
 }
 
 func setEndpointRouting(api *mux.Router) {
-	// twtxt will add support for other formats later.
-	// Maybe json? Making this future-proof.
+	// May add support for other formats later.
+	// Making this future-proof.
 	api.Path("/{format:(?:plain)}").
 		Methods("GET", "HEAD").
 		HandlerFunc(apiFormatHandler)