From 1973fd685d73770b463c2fc28a727f21fbcbf006 Mon Sep 17 00:00:00 2001 From: Ronnie Roller Date: Sat, 8 Feb 2020 11:22:14 -0800 Subject: [PATCH] Serve static files --- src/main.go | 46 +++++++++++++++++++++++++++++------ static/css/style.css | 7 ++++++ templates/media/index.html | 9 +------ templates/media/response.html | 9 +------ 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 static/css/style.css diff --git a/src/main.go b/src/main.go index b90be96..6fc25b3 100644 --- a/src/main.go +++ b/src/main.go @@ -9,17 +9,21 @@ import ( "net/http" "os" "os/signal" + "path" + "path/filepath" + "strings" "time" ) func main() { // Setup routes - r := chi.NewRouter() - r.Route("/", func(r chi.Router) { - r.Get("/", media.Index) - r.Get("/fetch", media.FetchMedia) - r.Get("/download", media.ServeMedia) + router := chi.NewRouter() + router.Route("/", func(r chi.Router) { + router.Get("/", media.Index) + router.Get("/fetch", media.FetchMedia) + router.Get("/download", media.ServeMedia) }) + fileServer(router, "/static", "static/") // Print out all routes walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error { @@ -27,13 +31,13 @@ func main() { return nil } // Panic if there is an error - if err := chi.Walk(r, walkFunc); err != nil { + if err := chi.Walk(router, walkFunc); err != nil { log.Panic().Msgf("%s\n", err.Error()) } valv := valve.New() baseCtx := valv.Context() - srv := http.Server{Addr: ":3000", Handler: chi.ServerBaseContext(baseCtx, r)} + srv := http.Server{Addr: ":3000", Handler: chi.ServerBaseContext(baseCtx, router)} // Create a shutdown hook for graceful shutdowns c := make(chan os.Signal, 1) @@ -62,10 +66,36 @@ func main() { } }() - // Start the listener err := srv.ListenAndServe() if err != nil { log.Info().Msg(err.Error()) } log.Info().Msgf("Shutdown complete") } + +func fileServer(r chi.Router, public string, static string) { + if strings.ContainsAny(public, "{}*") { + panic("FileServer does not permit URL parameters.") + } + + root, _ := filepath.Abs(static) + if _, err := os.Stat(root); os.IsNotExist(err) { + panic("Static Documents Directory Not Found") + } + + fs := http.StripPrefix(public, http.FileServer(http.Dir(root))) + + if public != "/" && public[len(public)-1] != '/' { + r.Get(public, http.RedirectHandler(public+"/", 301).ServeHTTP) + public += "/" + } + + r.Get(public+"*", func(w http.ResponseWriter, r *http.Request) { + file := strings.Replace(r.RequestURI, public, "/", 1) + if _, err := os.Stat(root + file); os.IsNotExist(err) { + http.ServeFile(w, r, path.Join(root, "index.html")) + return + } + fs.ServeHTTP(w, r) + }) +} diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..7050fe3 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,7 @@ +body { + background-color: #43464a; +} + +.container { + height: 100vh; +} \ No newline at end of file diff --git a/templates/media/index.html b/templates/media/index.html index 533bfea..4f8e586 100644 --- a/templates/media/index.html +++ b/templates/media/index.html @@ -5,14 +5,7 @@ - +
diff --git a/templates/media/response.html b/templates/media/response.html index a8c2aa0..cd363a1 100644 --- a/templates/media/response.html +++ b/templates/media/response.html @@ -5,14 +5,7 @@ - +