-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add spa package * chore:fix typo * fix: remove spa type * fix: remove panic * fix: merge handlers
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package spa | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"net/http" | ||
) | ||
|
||
// router is the http filesystem which only serves files | ||
// and prevent the directory traversal. | ||
type router struct { | ||
index string | ||
fs http.FileSystem | ||
} | ||
|
||
// Open inspects the URL path to locate a file within the static dir. | ||
// If a file is found, it will be served. If not, the file located at | ||
// the index path on the SPA handler will be served. | ||
func (r *router) Open(name string) (http.File, error) { | ||
file, err := r.fs.Open(name) | ||
|
||
if err == nil { | ||
return file, nil | ||
} | ||
// Serve index if file does not exist. | ||
if errors.Is(err, fs.ErrNotExist) { | ||
file, err := r.fs.Open(r.index) | ||
return file, err | ||
} | ||
|
||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package spa | ||
|
||
import ( | ||
"embed" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"net/http" | ||
|
||
"github.com/NYTimes/gziphandler" | ||
) | ||
|
||
// Handler return a file server http handler for single page | ||
// application. This handler can be mounted on your mux server. | ||
// | ||
// If gzip is set true, handler gzip the response body, for clients | ||
// which support it. Usually it also can be left to proxies like Nginx, | ||
// this method is useful when that's undesirable. | ||
func Handler(build embed.FS, dir string, index string, gzip bool) (http.Handler, error) { | ||
fsys, err := fs.Sub(build, dir) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't create sub filesystem: %w", err) | ||
} | ||
|
||
if _, err = fsys.Open(index); err != nil { | ||
if errors.Is(err, fs.ErrNotExist) { | ||
return nil, fmt.Errorf("ui is enabled but no index.html found: %w", err) | ||
} else { | ||
return nil, fmt.Errorf("ui assets error: %w", err) | ||
} | ||
} | ||
router := &router{index: index, fs: http.FS(fsys)} | ||
|
||
hlr := http.FileServer(router) | ||
|
||
if !gzip { | ||
return hlr, nil | ||
} | ||
return gziphandler.GzipHandler(hlr), nil | ||
} |