Serve static files

This commit is contained in:
Ronnie Roller 2020-02-08 11:22:14 -08:00
parent 3b3f384024
commit 1973fd685d
4 changed files with 47 additions and 24 deletions

View file

@ -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)
})
}

7
static/css/style.css Normal file
View file

@ -0,0 +1,7 @@
body {
background-color: #43464a;
}
.container {
height: 100vh;
}

View file

@ -5,14 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
body {
background-color: #43464a;
}
.container {
height: 100vh;
}
</style>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
</head>
<body>
<div class="container d-flex flex-column text-light text-center">

View file

@ -5,14 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
body {
background-color: #43464a;
}
.container {
height: 100vh;
}
</style>
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
</head>
<body>
<div class="container d-flex flex-column text-light text-center">