preview server
sageclove
Sat, 04 Jan 2025 12:22:15 -0700
4 files changed,
52 insertions(+),
3 deletions(-)
M
.vscode/launch.json
→
.vscode/launch.json
@@ -5,13 +5,22 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "configurations": [ { - "name": "Launch Package", + "name": "Build", "type": "go", "request": "launch", "mode": "auto", "program": "${fileDirname}", "console": "integratedTerminal", "args": ["../"] - } + }, + { + "name": "Build + Serve", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}", + "console": "integratedTerminal", + "args": ["../", "-s"] + }, ] }
M
build/build.go
→
build/build.go
@@ -4,7 +4,10 @@ package main
import ( "bufio" + "fmt" "io" + "log" + "net/http" "os" "path/filepath" "strconv"@@ -34,10 +37,24 @@ var rootPath = "."
var now = time.Now() func main() { + serve := false args := os.Args[1:] if len(args) != 0 { rootPath = args[0] + if len(args) == 2 { + flag := args[1] + switch flag { + case "--serve": + case "-s": + serve = true + + default: + fmt.Println(flag + " is not a recognized flag.") + os.Exit(1) + } + } } + fmt.Println("Building...") os.RemoveAll(filepath.Join("out")) os.Mkdir(filepath.Join("out"), 0777)@@ -52,6 +69,24 @@ )
assetsFS := os.DirFS(filepath.Join(rootPath, "assets")) os.CopyFS(filepath.Join("out", "assets"), assetsFS) + + fmt.Println("Build complete.") + if serve { + http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir("out")))) + http.HandleFunc("GET /{page}", pageHandler) + fmt.Println("Serving on http://localhost:8000") + log.Fatal(http.ListenAndServe(":8000", nil)) + } +} + +func pageHandler(responseWriter http.ResponseWriter, request *http.Request) { + pageName := request.PathValue("page") + pageFile, err := os.ReadFile(filepath.Join("out", pageName+".html")) + if err == nil { + responseWriter.Write(pageFile) + return + } + http.NotFound(responseWriter, request) } func buildPageLinkedList() *Page {
M
build/readme.md
→
build/readme.md
@@ -12,4 +12,8 @@ - `assets`: for other assets (e.g. images, files...)
- `site`: for site pages. The structure of this subdirectory determines the structure of the site itself. - `templates`: for reused markup. -If `<ROOT>` is omitted, the current directory will be used as the source directory.+If `<ROOT>` is omitted, the current directory will be used as the source directory. + + +### Flags +- `--serve`, `-s`: Builds site, then serves built site on port 8000. For previewing site, not for use in a live environment.
M
css/style.css
→
css/style.css
@@ -83,6 +83,7 @@ font-size: 1.2rem;
margin-left: 2rem; } .header .links { + user-select: none; width: 100%; } .header ul {