summary refs log tree commit diff stats
path: root/svc
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 /svc
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
Diffstat (limited to 'svc')
-rw-r--r--svc/conf.go6
-rw-r--r--svc/svc.go11
2 files changed, 14 insertions, 3 deletions
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)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330