diff options
author | Steve Manuel <nilslice@gmail.com> | 2020-01-01 17:14:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-01 17:14:32 -0700 |
commit | 9bc41b703131aa42ca64265ab6fdeadc37841e66 (patch) | |
tree | 138e6b7d6f8bd5c879eff61ce85df95506546003 /system | |
parent | 45730ba5b4de56fed27f79f8c822bb59d41f7a38 (diff) | |
parent | 6ee81caea4649d3fc3bb6b5bde0628cb91c8e6f5 (diff) |
Merge pull request #333 from mangelajo/env-vars-for-dirs
Allow data directories outside pwd
Diffstat (limited to 'system')
-rw-r--r-- | system/admin/server.go | 12 | ||||
-rw-r--r-- | system/admin/upload/upload.go | 11 | ||||
-rw-r--r-- | system/api/analytics/init.go | 5 | ||||
-rw-r--r-- | system/cfg/env.go | 56 | ||||
-rw-r--r-- | system/db/init.go | 6 | ||||
-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 |
10 files changed, 88 insertions, 44 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/admin/upload/upload.go b/system/admin/upload/upload.go index fd29796..a5a2d1c 100644 --- a/system/admin/upload/upload.go +++ b/system/admin/upload/upload.go @@ -14,6 +14,8 @@ import ( "strconv" "time" + "github.com/ponzu-cms/ponzu/system/cfg" + "github.com/ponzu-cms/ponzu/system/db" "github.com/ponzu-cms/ponzu/system/item" ) @@ -41,12 +43,6 @@ func StoreFiles(req *http.Request) (map[string]string, error) { req.Form.Set("timestamp", ts) // get or create upload directory to save files from request - pwd, err := os.Getwd() - if err != nil { - err := fmt.Errorf("Failed to locate current directory: %s", err) - return nil, err - } - i, err := strconv.ParseInt(ts, 10, 64) if err != nil { return nil, err @@ -56,8 +52,7 @@ func StoreFiles(req *http.Request) (map[string]string, error) { urlPathPrefix := "api" uploadDirName := "uploads" - - uploadDir := filepath.Join(pwd, uploadDirName, fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%02d", tm.Month())) + uploadDir := filepath.Join(cfg.UploadDir(), fmt.Sprintf("%d", tm.Year()), fmt.Sprintf("%02d", tm.Month())) err = os.MkdirAll(uploadDir, os.ModeDir|os.ModePerm) if err != nil { return nil, err diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index caa187a..0e9e90e 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -7,11 +7,13 @@ import ( "encoding/json" "log" "net/http" + "path/filepath" "runtime" "strings" "time" "github.com/boltdb/bolt" + "github.com/ponzu-cms/ponzu/system/cfg" ) type apiRequest struct { @@ -72,7 +74,8 @@ func Close() { // sets up the queue/batching channel func Init() { var err error - store, err = bolt.Open("analytics.db", 0666, nil) + analyticsDb := filepath.Join(cfg.DataDir(),"analytics.db") + store, err = bolt.Open(analyticsDb, 0666, nil) if err != nil { log.Fatalln(err) } 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/db/init.go b/system/db/init.go index 31e9f3f..f08e55f 100644 --- a/system/db/init.go +++ b/system/db/init.go @@ -5,6 +5,9 @@ package db import ( "log" + "path/filepath" + + "github.com/ponzu-cms/ponzu/system/cfg" "github.com/ponzu-cms/ponzu/system/item" "github.com/ponzu-cms/ponzu/system/search" @@ -46,7 +49,8 @@ func Init() { } var err error - store, err = bolt.Open("system.db", 0666, nil) + systemDb := filepath.Join(cfg.DataDir(),"system.db") + store, err = bolt.Open(systemDb, 0666, nil) if err != nil { log.Fatalln(err) } 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") |