added static files server
This commit is contained in:
parent
63b93f5895
commit
9616450cff
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/internal/static
|
9
internal/debug.go
Normal file
9
internal/debug.go
Normal 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
|
||||
}
|
@ -50,6 +50,9 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
|
||||
}
|
||||
})
|
||||
|
||||
// Init the embedded static files
|
||||
initStatic()
|
||||
|
||||
return http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
|
32
internal/staticFiles.go
Normal file
32
internal/staticFiles.go
Normal 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)}))
|
||||
}
|
Loading…
Reference in New Issue
Block a user