added static files server

This commit is contained in:
Djeeberjr 2021-09-03 23:23:06 +02:00
parent 63b93f5895
commit 9616450cff
4 changed files with 45 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/internal/static

9
internal/debug.go Normal file
View File

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

View File

@ -50,6 +50,9 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
} }
}) })
// Init the embedded static files
initStatic()
return http.ListenAndServe(":8080", nil) return http.ListenAndServe(":8080", nil)
} }

32
internal/staticFiles.go Normal file
View File

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