diff options
author | Miguel Angel Ajo <majopela@redhat.com> | 2019-12-29 18:36:08 +0100 |
---|---|---|
committer | Miguel Angel Ajo <majopela@redhat.com> | 2019-12-29 23:42:46 +0100 |
commit | 90a2ff959a958e8f7654f577e88e942378b0a7c2 (patch) | |
tree | 934b6d72979f0edf26f3c661b6b059b6d1753e37 | |
parent | 45730ba5b4de56fed27f79f8c822bb59d41f7a38 (diff) |
Allow data directories outside pwd
Running ponzu-cms inside a docker container, and maintaining
data files in a volume (or several) was not possible with the
previous directory schema without copying files or binaries
to the volume.
This commit adds several environment variables to allow the
administrator to specify paths for directories while
retaining backwards compatibility.
- PONZU_DATA_DIR : is the directory where all the data and other
directories will be stored, still the other
directories can be controlled by the other env
variables.
- PONZU_TLS_DIR : is the directory to store devcerts and autocerts
- PONZU_ADMINSTATIC_DIR : directory where the admin static files are
served from.
- PONZU_UPLOAD_DIR: upload files directory
- PONZU_SEARCH_DIR: index directory for searches
-rw-r--r-- | system/admin/server.go | 12 | ||||
-rw-r--r-- | system/cfg/env.go | 56 | ||||
-rw-r--r-- | system/search/backup.go | 4 | ||||
-rw-r--r-- | system/search/search.go | 10 | ||||
-rw-r--r-- | system/tls/devcerts.go | 10 | ||||
-rw-r--r-- | system/tls/enable.go | 7 | ||||
-rw-r--r-- | system/tls/enabledev.go | 11 |
7 files changed, 76 insertions, 34 deletions
diff --git a/system/admin/server.go b/system/admin/server.go index 9f28a0d..b912d4d 100644 --- a/system/admin/server.go +++ b/system/admin/server.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" + "github.com/ponzu-cms/ponzu/system/cfg" + "github.com/ponzu-cms/ponzu/system" "github.com/ponzu-cms/ponzu/system/admin/user" "github.com/ponzu-cms/ponzu/system/api" @@ -46,18 +48,14 @@ func Run() { http.HandleFunc("/admin/edit/upload", user.Auth(editUploadHandler)) http.HandleFunc("/admin/edit/upload/delete", user.Auth(deleteUploadHandler)) - pwd, err := os.Getwd() - if err != nil { - log.Fatalln("Couldn't find current directory for file server.") - } + staticDir := cfg.AdminStaticDir() - staticDir := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu", "system") - http.Handle("/admin/static/", db.CacheControl(http.FileServer(restrict(http.Dir(staticDir))))) + http.Handle("/admin/static/", db.CacheControl(http.StripPrefix("/admin/static", http.FileServer(restrict(http.Dir(staticDir)))))) // API path needs to be registered within server package so that it is handled // even if the API server is not running. Otherwise, images/files uploaded // through the editor will not load within the admin system. - uploadsDir := filepath.Join(pwd, "uploads") + uploadsDir := cfg.UploadDir() http.Handle("/api/uploads/", api.Record(api.CORS(db.CacheControl(http.StripPrefix("/api/uploads/", http.FileServer(restrict(http.Dir(uploadsDir)))))))) // Database & uploads backup via HTTP route registered with Basic Auth middleware. diff --git a/system/cfg/env.go b/system/cfg/env.go new file mode 100644 index 0000000..71f48a2 --- /dev/null +++ b/system/cfg/env.go @@ -0,0 +1,56 @@ +package cfg + +import ( + "log" + "os" + "path/filepath" +) + +func getWd() string { + wd, err := os.Getwd() + if err != nil { + log.Fatalln("Couldn't find working directory", err) + } + return wd +} + +func DataDir() string { + dataDir := os.Getenv("PONZU_DATA_DIR") + if dataDir == "" { + return getWd() + } + return dataDir +} + +func TlsDir() string { + tlsDir := os.Getenv("PONZU_TLS_DIR") + if tlsDir == "" { + tlsDir = filepath.Join(getWd(), "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu", "system", "tls") + } + return tlsDir +} + +func AdminStaticDir() string { + staticDir := os.Getenv("PONZU_ADMINSTATIC_DIR") + if staticDir == "" { + + staticDir = filepath.Join(getWd(), "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu", "system", "admin", "static") + } + return staticDir +} + +func UploadDir() string { + uploadDir := os.Getenv("PONZU_UPLOAD_DIR") + if uploadDir == "" { + uploadDir = filepath.Join(DataDir(),"uploads") + } + return uploadDir +} + +func SearchDir() string { + searchDir := os.Getenv("PONZU_SEARCH_DIR") + if searchDir == "" { + searchDir = filepath.Join(DataDir(),"search") + } + return searchDir +} diff --git a/system/search/backup.go b/system/search/backup.go index 4017b15..c054f9d 100644 --- a/system/search/backup.go +++ b/system/search/backup.go @@ -9,6 +9,8 @@ import ( "path/filepath" "time" + "github.com/ponzu-cms/ponzu/system/cfg" + "github.com/ponzu-cms/ponzu/system/backup" ) @@ -26,7 +28,7 @@ func Backup(ctx context.Context, res http.ResponseWriter) error { return err } - err = backup.ArchiveFS(ctx, "search", f) + err = backup.ArchiveFS(ctx, cfg.SearchDir(), f) if err != nil { return err } diff --git a/system/search/search.go b/system/search/search.go index 8cd0fe6..d3bee6d 100644 --- a/system/search/search.go +++ b/system/search/search.go @@ -11,6 +11,8 @@ import ( "path/filepath" "strings" + "github.com/ponzu-cms/ponzu/system/cfg" + "github.com/ponzu-cms/ponzu/system/item" "github.com/blevesearch/bleve" @@ -62,13 +64,7 @@ func MapIndex(typeName string) error { idxName := typeName + ".index" var idx bleve.Index - // check if index exists, use it or create new one - pwd, err := os.Getwd() - if err != nil { - return err - } - - searchPath := filepath.Join(pwd, "search") + searchPath := cfg.SearchDir() err = os.MkdirAll(searchPath, os.ModeDir|os.ModePerm) if err != nil { diff --git a/system/tls/devcerts.go b/system/tls/devcerts.go index 0554aa4..1dde4df 100644 --- a/system/tls/devcerts.go +++ b/system/tls/devcerts.go @@ -26,6 +26,8 @@ import ( "path/filepath" "time" + "github.com/ponzu-cms/ponzu/system/cfg" + "github.com/ponzu-cms/ponzu/system/db" ) @@ -112,12 +114,8 @@ func setupDev() { } // overwrite/create directory for devcerts - pwd, err := os.Getwd() - if err != nil { - log.Fatalln("Couldn't find working directory to locate or save dev certificates:", err) - } - vendorTLSPath := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu", "system", "tls") + vendorTLSPath := cfg.TlsDir() devcertsPath := filepath.Join(vendorTLSPath, "devcerts") // clear all old certs if found @@ -126,7 +124,7 @@ func setupDev() { log.Fatalln("Failed to remove old files from dev certificate directory:", err) } - err = os.Mkdir(devcertsPath, os.ModeDir|os.ModePerm) + err = os.MkdirAll(devcertsPath, os.ModeDir|os.ModePerm) if err != nil { log.Fatalln("Failed to create directory to locate or save dev certificates:", err) } diff --git a/system/tls/enable.go b/system/tls/enable.go index 9fc95f9..12d63bc 100644 --- a/system/tls/enable.go +++ b/system/tls/enable.go @@ -13,18 +13,15 @@ import ( "time" "github.com/ponzu-cms/ponzu/system/db" + "github.com/ponzu-cms/ponzu/system/cfg" "golang.org/x/crypto/acme/autocert" ) // newManager attempts to locate or create the cert cache directory and the // certs for TLS encryption and returns an autocert.Manager func newManager() autocert.Manager { - pwd, err := os.Getwd() - if err != nil { - log.Fatalln("Couldn't find working directory to locate or save certificates.") - } - cache := autocert.DirCache(filepath.Join(pwd, "system", "tls", "certs")) + cache := autocert.DirCache(filepath.Join(cfg.TlsDir(), "certs")) if _, err := os.Stat(string(cache)); os.IsNotExist(err) { err := os.MkdirAll(string(cache), os.ModePerm|os.ModeDir) if err != nil { diff --git a/system/tls/enabledev.go b/system/tls/enabledev.go index 3550fc0..03bce42 100644 --- a/system/tls/enabledev.go +++ b/system/tls/enabledev.go @@ -3,8 +3,9 @@ package tls import ( "log" "net/http" - "os" "path/filepath" + + "github.com/ponzu-cms/ponzu/system/cfg" ) // EnableDev generates self-signed SSL certificates to use HTTPS & HTTP/2 while @@ -15,13 +16,7 @@ import ( func EnableDev() { setupDev() - pwd, err := os.Getwd() - if err != nil { - log.Fatalln("Couldn't find working directory to activate dev certificates:", err) - } - - vendorPath := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu", "system", "tls") - + vendorPath := cfg.TlsDir() cert := filepath.Join(vendorPath, "devcerts", "cert.pem") key := filepath.Join(vendorPath, "devcerts", "key.pem") |