diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc17965 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/internal/static \ No newline at end of file diff --git a/internal/debug.go b/internal/debug.go new file mode 100644 index 0000000..1994573 --- /dev/null +++ b/internal/debug.go @@ -0,0 +1,9 @@ +//go:build !prod +// +build !prod + +package s3browser + +// Since we dont have the static directory when developing we replace the function with an empty one +func initStatic() { + // NOOP +} diff --git a/internal/httpServer.go b/internal/httpServer.go index ca5c418..5aa2607 100644 --- a/internal/httpServer.go +++ b/internal/httpServer.go @@ -50,6 +50,9 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error { } }) + // Init the embedded static files + initStatic() + return http.ListenAndServe(":8080", nil) } diff --git a/internal/staticFiles.go b/internal/staticFiles.go new file mode 100644 index 0000000..3c36c17 --- /dev/null +++ b/internal/staticFiles.go @@ -0,0 +1,32 @@ +//go:build prod +// +build prod + +package s3browser + +import ( + "embed" + "io/fs" + "net/http" + "os" +) + +// content holds our static web server content. +//go:embed static +var staticFiles embed.FS + +type spaFileSystem struct { + root http.FileSystem +} + +func (spa *spaFileSystem) Open(name string) (http.File, error) { + f, err := spa.root.Open(name) + if os.IsNotExist(err) { + return spa.root.Open("index.html") + } + return f, err +} + +func initStatic() { + staticFS, _ := fs.Sub(staticFiles, "static") + http.Handle("/", http.FileServer(&spaFileSystem{http.FS(staticFS)})) +}