diff options
-rw-r--r-- | cmd/ponzu/main.go | 42 | ||||
-rw-r--r-- | cmd/ponzu/options.go | 10 | ||||
-rw-r--r-- | system/admin/cache.go | 2 | ||||
-rw-r--r-- | system/admin/handlers.go | 73 | ||||
-rw-r--r-- | system/admin/server.go | 14 |
5 files changed, 77 insertions, 64 deletions
diff --git a/cmd/ponzu/main.go b/cmd/ponzu/main.go index fcb6035..5b5fa56 100644 --- a/cmd/ponzu/main.go +++ b/cmd/ponzu/main.go @@ -125,25 +125,36 @@ func main() { } case "run": - if len(args) < 2 { - flag.PrintDefaults() - os.Exit(1) - } - var addTLS string if tls { addTLS = "--tls" } else { addTLS = "--tls=false" } + + var services string + if len(args) > 1 { + services = args[1] + } else { + services = "admin,api" + } + serve := exec.Command("./ponzu-server", fmt.Sprintf("--port=%d", port), addTLS, "serve", - args[1], + services, ) + serve.Stderr = os.Stderr + serve.Stdout = os.Stdout - err := serve.Run() + err := serve.Start() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + err = serve.Wait() if err != nil { fmt.Println(err) os.Exit(1) @@ -154,7 +165,6 @@ func main() { if len(args) > 1 { services := strings.Split(args[1], ",") - fmt.Println(args, port, tls) for i := range services { if services[i] == "api" { api.Run() @@ -166,25 +176,11 @@ func main() { os.Exit(1) } } - } else { - if len(args) > 1 { - if args[1] == "admin" { - admin.Run() - } - - if args[1] == "api" { - api.Run() - } - } else { - admin.Run() - api.Run() - } - } if tls { fmt.Println("TLS through Let's Encrypt is not implemented yet.") - fmt.Println("Please run 'ponzu serve' without the -tls flag for now.") + fmt.Println("Please run 'ponzu serve' without the --tls flag for now.") os.Exit(1) } diff --git a/cmd/ponzu/options.go b/cmd/ponzu/options.go index 51ecdb3..8f33a78 100644 --- a/cmd/ponzu/options.go +++ b/cmd/ponzu/options.go @@ -306,19 +306,16 @@ func buildPonzuServer(args []string) error { dstFile, err := os.Create(filepath.Join(contentDstPath, srcFileInfo.Name())) if err != nil { - return err } srcFile, err := os.Open(filepath.Join(contentSrcPath, srcFileInfo.Name())) if err != nil { - return err } _, err = io.Copy(dstFile, srcFile) if err != nil { - return err } } @@ -336,7 +333,12 @@ func buildPonzuServer(args []string) error { } // execute go build -o ponzu-cms cmd/ponzu/*.go - build := exec.Command("go", "build", "-o", "ponzu-server", "cmd/ponzu/*.go") + mainPath := filepath.Join(pwd, "cmd", "ponzu", "main.go") + optsPath := filepath.Join(pwd, "cmd", "ponzu", "options.go") + build := exec.Command("go", "build", "-o", "ponzu-server", mainPath, optsPath) + build.Stderr = os.Stderr + build.Stdout = os.Stdout + err = build.Start() if err != nil { return errors.New("Ponzu build step failed. Please try again. " + "\n" + err.Error()) diff --git a/system/admin/cache.go b/system/admin/cache.go index 0de45af..2abdb26 100644 --- a/system/admin/cache.go +++ b/system/admin/cache.go @@ -9,7 +9,7 @@ import ( ) // CacheControl sets the default cache policy on static asset responses -func CacheControl(next http.HandlerFunc) http.HandlerFunc { +func CacheControl(next http.Handler) http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { etag := db.ConfigCache("etag") policy := fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 60*60*24*30) diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 41e1095..a4eda03 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -5,10 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" - "log" "net/http" - "os" - "path/filepath" "strings" "time" @@ -592,34 +589,42 @@ func searchHandler(res http.ResponseWriter, req *http.Request) { res.Write(adminView) } -func staticAssetHandler(res http.ResponseWriter, req *http.Request) { - path := req.URL.Path - pathParts := strings.Split(path, "/")[1:] - pwd, err := os.Getwd() - if err != nil { - log.Fatal("Coudln't get current directory to set static asset source.") - } - - filePathParts := make([]string, len(pathParts)+2, len(pathParts)+2) - filePathParts = append(filePathParts, pwd) - filePathParts = append(filePathParts, "system") - filePathParts = append(filePathParts, pathParts...) - - http.ServeFile(res, req, filepath.Join(filePathParts...)) -} - -func staticUploadHandler(res http.ResponseWriter, req *http.Request) { - path := req.URL.Path - pathParts := strings.Split(path, "/")[2:] - - pwd, err := os.Getwd() - if err != nil { - log.Fatal("Coudln't get current directory to set static asset source.") - } - - filePathParts := make([]string, len(pathParts)+1, len(pathParts)+1) - filePathParts = append(filePathParts, pwd) - filePathParts = append(filePathParts, pathParts...) - - http.ServeFile(res, req, filepath.Join(filePathParts...)) -} +// func staticAssetHandler(res http.ResponseWriter, req *http.Request) { +// path := req.URL.Path +// pathParts := strings.Split(path, "/")[1:] +// pwd, err := os.Getwd() +// if err != nil { +// log.Fatal("Coudln't get current directory to set static asset source.") +// } + +// var filePathParts = []string{} +// filePathParts = append(filePathParts, pwd) +// filePathParts = append(filePathParts, "cmd") +// filePathParts = append(filePathParts, "ponzu") +// filePathParts = append(filePathParts, "vendor") +// filePathParts = append(filePathParts, "github.com") +// filePathParts = append(filePathParts, "bosssauce") +// filePathParts = append(filePathParts, "ponzu") +// filePathParts = append(filePathParts, "system") +// filePathParts = append(filePathParts, pathParts...) + +// fmt.Println(filepath.Join(filePathParts...)) + +// http.ServeFile(res, req, filepath.Join(filePathParts...)) +// } + +// func staticUploadHandler(res http.ResponseWriter, req *http.Request) { +// path := req.URL.Path +// pathParts := strings.Split(path, "/")[2:] + +// pwd, err := os.Getwd() +// if err != nil { +// log.Fatal("Coudln't get current directory to set static asset source.") +// } + +// filePathParts := make([]string, len(pathParts)+1, len(pathParts)+1) +// filePathParts = append(filePathParts, pwd) +// filePathParts = append(filePathParts, pathParts...) + +// http.ServeFile(res, req, filepath.Join(filePathParts...)) +// } diff --git a/system/admin/server.go b/system/admin/server.go index 312a3de..d5fd894 100644 --- a/system/admin/server.go +++ b/system/admin/server.go @@ -1,7 +1,10 @@ package admin import ( + "log" "net/http" + "os" + "path/filepath" "github.com/bosssauce/ponzu/system/admin/user" ) @@ -24,10 +27,17 @@ func Run() { http.HandleFunc("/admin/edit", user.Auth(editHandler)) http.HandleFunc("/admin/edit/upload", user.Auth(editUploadHandler)) - http.HandleFunc("/admin/static/", CacheControl(staticAssetHandler)) + pwd, err := os.Getwd() + if err != nil { + log.Fatal("Couldn't find current directory for file server.") + } + + staticDir := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "bosssauce", "ponzu", "system") + http.Handle("/admin/static/", CacheControl(http.StripPrefix("/admin/", http.FileServer(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. - http.HandleFunc("/api/uploads/", CacheControl(staticUploadHandler)) + uploadsDir := filepath.Join(pwd, "uploads") + http.Handle("/api/uploads/", CacheControl(http.StripPrefix("/api/uploads/", http.FileServer(http.Dir(uploadsDir))))) } |