Fixed static file embedding
This commit is contained in:
@@ -42,10 +42,10 @@ func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
|
|||||||
|
|
||||||
// Handles requests for static files, without allowing access to the
|
// Handles requests for static files, without allowing access to the
|
||||||
// directory viewer and returning 404 if an exact file is not found
|
// directory viewer and returning 404 if an exact file is not found
|
||||||
func HandleStatic() http.Handler {
|
func HandleStatic(staticFS *http.FileSystem) http.Handler {
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
nfs := justFilesFilesystem{http.Dir("static")}
|
nfs := justFilesFilesystem{*staticFS}
|
||||||
fs := http.FileServer(nfs)
|
fs := http.FileServer(nfs)
|
||||||
fs.ServeHTTP(w, r)
|
fs.ServeHTTP(w, r)
|
||||||
},
|
},
|
||||||
|
|||||||
31
main.go
31
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -22,6 +23,26 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed static/*
|
||||||
|
var embeddedStatic embed.FS
|
||||||
|
|
||||||
|
// Gets the static files
|
||||||
|
func getStaticFiles() (http.FileSystem, error) {
|
||||||
|
if _, err := os.Stat("static"); err == nil {
|
||||||
|
// Use actual filesystem in development
|
||||||
|
fmt.Println("Using filesystem for static files")
|
||||||
|
return http.Dir("static"), nil
|
||||||
|
} else {
|
||||||
|
// Use embedded filesystem in production
|
||||||
|
fmt.Println("Using embedded static files")
|
||||||
|
subFS, err := fs.Sub(embeddedStatic, "static")
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "fs.Sub")
|
||||||
|
}
|
||||||
|
return http.FS(subFS), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initializes and runs the server
|
// Initializes and runs the server
|
||||||
func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
||||||
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
||||||
@@ -62,7 +83,12 @@ func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
srv := server.NewServer(config, logger, conn)
|
staticFS, err := getStaticFiles()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "getStaticFiles")
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := server.NewServer(config, logger, conn, &staticFS)
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: net.JoinHostPort(config.Host, config.Port),
|
Addr: net.JoinHostPort(config.Host, config.Port),
|
||||||
Handler: srv,
|
Handler: srv,
|
||||||
@@ -101,9 +127,6 @@ func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed static/*
|
|
||||||
var static embed.FS
|
|
||||||
|
|
||||||
// Start of runtime. Parse commandline arguments & flags, Initializes context
|
// Start of runtime. Parse commandline arguments & flags, Initializes context
|
||||||
// and starts the server
|
// and starts the server
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var excludedFiles = map[string]bool{
|
|
||||||
"/static/css/output.css": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks is path requested if for an excluded file and returns the file
|
|
||||||
// instead of passing the request onto the next middleware
|
|
||||||
func ExcludedFiles(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if excludedFiles[r.URL.Path] {
|
|
||||||
filePath := strings.TrimPrefix(r.URL.Path, "/")
|
|
||||||
http.ServeFile(w, r, filePath)
|
|
||||||
} else {
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package middleware
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Favicon(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path == "/favicon.ico" {
|
|
||||||
http.ServeFile(w, r, "static/favicon.ico")
|
|
||||||
} else {
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -18,12 +18,13 @@ func addRoutes(
|
|||||||
logger *zerolog.Logger,
|
logger *zerolog.Logger,
|
||||||
config *config.Config,
|
config *config.Config,
|
||||||
conn *sql.DB,
|
conn *sql.DB,
|
||||||
|
staticFS *http.FileSystem,
|
||||||
) {
|
) {
|
||||||
// Health check
|
// Health check
|
||||||
mux.HandleFunc("GET /healthz", func(http.ResponseWriter, *http.Request) {})
|
mux.HandleFunc("GET /healthz", func(http.ResponseWriter, *http.Request) {})
|
||||||
|
|
||||||
// Static files
|
// Static files
|
||||||
mux.Handle("GET /static/", http.StripPrefix("/static/", handlers.HandleStatic()))
|
mux.Handle("GET /static/", http.StripPrefix("/static/", handlers.HandleStatic(staticFS)))
|
||||||
|
|
||||||
// Index page and unhandled catchall (404)
|
// Index page and unhandled catchall (404)
|
||||||
mux.Handle("GET /", handlers.HandleRoot())
|
mux.Handle("GET /", handlers.HandleRoot())
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ func NewServer(
|
|||||||
config *config.Config,
|
config *config.Config,
|
||||||
logger *zerolog.Logger,
|
logger *zerolog.Logger,
|
||||||
conn *sql.DB,
|
conn *sql.DB,
|
||||||
|
staticFS *http.FileSystem,
|
||||||
) http.Handler {
|
) http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
addRoutes(
|
addRoutes(
|
||||||
@@ -22,6 +23,7 @@ func NewServer(
|
|||||||
logger,
|
logger,
|
||||||
config,
|
config,
|
||||||
conn,
|
conn,
|
||||||
|
staticFS,
|
||||||
)
|
)
|
||||||
var handler http.Handler = mux
|
var handler http.Handler = mux
|
||||||
// Add middleware here, must be added in reverse order of execution
|
// Add middleware here, must be added in reverse order of execution
|
||||||
@@ -29,10 +31,6 @@ func NewServer(
|
|||||||
handler = middleware.Logging(logger, handler)
|
handler = middleware.Logging(logger, handler)
|
||||||
handler = middleware.Authentication(logger, config, conn, handler)
|
handler = middleware.Authentication(logger, config, conn, handler)
|
||||||
|
|
||||||
// Serve the favicon and exluded files before any middleware is added
|
|
||||||
handler = middleware.ExcludedFiles(handler)
|
|
||||||
handler = middleware.Favicon(handler)
|
|
||||||
|
|
||||||
// Gzip
|
// Gzip
|
||||||
handler = middleware.Gzip(handler, config.GZIP)
|
handler = middleware.Gzip(handler, config.GZIP)
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ templ Global() {
|
|||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||||
<title>Project Reshoot</title>
|
<title>Project Reshoot</title>
|
||||||
|
<link rel="icon" type="image/x-icon" href="/static/favicon.ico"/>
|
||||||
<link href="/static/css/output.css" rel="stylesheet"/>
|
<link href="/static/css/output.css" rel="stylesheet"/>
|
||||||
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
|
<script src="https://unpkg.com/htmx.org@2.0.4" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
|
||||||
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/persist@3.x.x/dist/cdn.min.js"></script>
|
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/persist@3.x.x/dist/cdn.min.js"></script>
|
||||||
|
|||||||
Reference in New Issue
Block a user