Serve static files
This commit is contained in:
parent
3b3f384024
commit
1973fd685d
4 changed files with 47 additions and 24 deletions
46
src/main.go
46
src/main.go
|
|
@ -9,17 +9,21 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Setup routes
|
// Setup routes
|
||||||
r := chi.NewRouter()
|
router := chi.NewRouter()
|
||||||
r.Route("/", func(r chi.Router) {
|
router.Route("/", func(r chi.Router) {
|
||||||
r.Get("/", media.Index)
|
router.Get("/", media.Index)
|
||||||
r.Get("/fetch", media.FetchMedia)
|
router.Get("/fetch", media.FetchMedia)
|
||||||
r.Get("/download", media.ServeMedia)
|
router.Get("/download", media.ServeMedia)
|
||||||
})
|
})
|
||||||
|
fileServer(router, "/static", "static/")
|
||||||
|
|
||||||
// Print out all routes
|
// Print out all routes
|
||||||
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
||||||
|
|
@ -27,13 +31,13 @@ func main() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Panic if there is an error
|
// 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())
|
log.Panic().Msgf("%s\n", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
valv := valve.New()
|
valv := valve.New()
|
||||||
baseCtx := valv.Context()
|
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
|
// Create a shutdown hook for graceful shutdowns
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
|
|
@ -62,10 +66,36 @@ func main() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Start the listener
|
|
||||||
err := srv.ListenAndServe()
|
err := srv.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info().Msg(err.Error())
|
log.Info().Msg(err.Error())
|
||||||
}
|
}
|
||||||
log.Info().Msgf("Shutdown complete")
|
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
7
static/css/style.css
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
body {
|
||||||
|
background-color: #43464a;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container {
|
||||||
|
height: 100vh;
|
||||||
|
}
|
||||||
|
|
@ -5,14 +5,7 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<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"
|
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"
|
||||||
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
|
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
|
||||||
<style>
|
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||||
body {
|
|
||||||
background-color: #43464a;
|
|
||||||
}
|
|
||||||
.container {
|
|
||||||
height: 100vh;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container d-flex flex-column text-light text-center">
|
<div class="container d-flex flex-column text-light text-center">
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,7 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<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"
|
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"
|
||||||
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
|
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
|
||||||
<style>
|
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||||
body {
|
|
||||||
background-color: #43464a;
|
|
||||||
}
|
|
||||||
.container {
|
|
||||||
height: 100vh;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container d-flex flex-column text-light text-center">
|
<div class="container d-flex flex-column text-light text-center">
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue