chore: Refactor site to improve testing (#2014)

It was difficult to develop this package due to the
embed build tag being mandatory on the tests. The logic
to test doesn't require any embedded files.
This commit is contained in:
Kyle Carberry 2022-06-02 23:27:21 -05:00 committed by GitHub
parent 89dde21837
commit 61aacff444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 40 deletions

View File

@ -334,7 +334,7 @@ func New(options *Options) *API {
r.Get("/state", api.workspaceBuildState)
})
})
r.NotFound(site.DefaultHandler().ServeHTTP)
r.NotFound(site.Handler(site.FS()).ServeHTTP)
return api
}

View File

@ -1,12 +0,0 @@
//go:build !embed
// +build !embed
package site
import (
"net/http"
)
func DefaultHandler() http.Handler {
return http.NotFoundHandler()
}

View File

@ -1,11 +1,7 @@
//go:build embed
// +build embed
package site
import (
"bytes"
"embed"
"fmt"
"io"
"io/fs"
@ -21,26 +17,6 @@ import (
"golang.org/x/xerrors"
)
// The `embed` package ignores recursively including directories
// that prefix with `_`. Wildcarding nested is janky, but seems to
// work quite well for edge-cases.
//go:embed out
//go:embed out/bin/*
var site embed.FS
func DefaultHandler() http.Handler {
// the out directory is where webpack builds are created. It is in the same
// directory as this file (package site).
siteFS, err := fs.Sub(site, "out")
if err != nil {
// This can't happen... Go would throw a compilation error.
panic(err)
}
return Handler(siteFS)
}
// Handler returns an HTTP handler for serving the static site.
func Handler(fileSystem fs.FS) http.Handler {
// html files are handled by a text/template. Non-html files

24
site/site_embed.go Normal file
View File

@ -0,0 +1,24 @@
//go:build embed
// +build embed
package site
import (
"embed"
"io/fs"
)
//go:embed out
//go:embed out/bin/*
var site embed.FS
func FS() fs.FS {
// the out directory is where webpack builds are created. It is in the same
// directory as this file (package site).
out, err := fs.Sub(site, "out")
if err != nil {
// This can't happen... Go would throw a compilation error.
panic(err)
}
return out
}

15
site/site_slim.go Normal file
View File

@ -0,0 +1,15 @@
//go:build !embed
// +build !embed
package site
import (
"embed"
"io/fs"
)
var slim embed.FS
func FS() fs.FS {
return slim
}

View File

@ -1,6 +1,3 @@
//go:build embed
// +build embed
package site_test
import (