-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic setup for serving a MUD via HTTP
- Loading branch information
Showing
4 changed files
with
111 additions
and
5 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,43 @@ | ||
// web/build.go | ||
package web | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
"net/http" | ||
"os" | ||
"path" | ||
) | ||
|
||
//go:embed build/* | ||
var Assets embed.FS | ||
|
||
// fsFunc is short-hand for constructing a http.FileSystem | ||
// implementation | ||
type fsFunc func(name string) (fs.File, error) | ||
|
||
func (f fsFunc) Open(name string) (fs.File, error) { | ||
return f(name) | ||
} | ||
|
||
// AssetHandler returns an http.Handler that will serve files from | ||
// the Assets embed.FS. When locating a file, it will strip the given | ||
// prefix from the request and prepend the root to the filesystem | ||
// lookup: typical prefix might be /web/, and root would be build. | ||
func AssetHandler(prefix, root string) http.Handler { | ||
handler := fsFunc(func(name string) (fs.File, error) { | ||
assetPath := path.Join(root, name) | ||
|
||
// If we can't find the asset, return the default index.html content | ||
f, err := Assets.Open(assetPath) | ||
if os.IsNotExist(err) { | ||
return Assets.Open("build/index.html") | ||
} | ||
|
||
// Otherwise assume this is a legitimate request routed | ||
// correctly | ||
return f, err | ||
}) | ||
|
||
return http.StripPrefix(prefix, http.FileServer(http.FS(handler))) | ||
} |
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,9 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
Testing ... | ||
</body> | ||
</html> |