all repos — vite @ 116b5b2774eec9a0a03b448a9ca69778e6e2309c

a fast (this time, actually) and minimal static site generator

Add new `vite serve` command
Anirudh Oppiliappan x@icyphox.sh
Thu, 24 Mar 2022 17:49:02 +0530
commit

116b5b2774eec9a0a03b448a9ca69778e6e2309c

parent

a15978fe24a2e105d347adbc857a0af0c143bdeb

3 files changed, 49 insertions(+), 13 deletions(-)

jump to
A commands/serve.go

@@ -0,0 +1,25 @@

+package commands + +import ( + "fmt" + "log" + "net/http" +) + +func requestLog(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + log.Printf("%s\t%s", r.Method, r.URL.Path) + }) +} + +func Serve(addr string) error { + fs := http.FileServer(http.Dir("./build")) + mux := http.NewServeMux() + mux.Handle("/", fs) + fmt.Printf("vite: serving on %s\n", addr) + if err := http.ListenAndServe(addr, requestLog(mux)); err != nil { + return err + } + return nil +}
M main.gomain.go

@@ -15,10 +15,11 @@

A simple and minimal static site generator. options: - init PATH create vite project at PATH - build builds the current project - new PATH create a new markdown post - ` + init PATH create vite project at PATH + build builds the current project + new PATH create a new markdown post + serve [HOST:PORT] serves the 'build' directory +` if len(args) <= 1 { fmt.Println(helpStr)

@@ -32,14 +33,12 @@ fmt.Println(helpStr)

return } initPath := args[2] - err := commands.Init(initPath) - if err != nil { + if err := commands.Init(initPath); err != nil { fmt.Fprintf(os.Stderr, "error: init: %+v\n", err) } case "build": - err := commands.Build() - if err != nil { + if err := commands.Build(); err != nil { fmt.Fprintf(os.Stderr, "error: build: %+v\n", err) }

@@ -49,10 +48,21 @@ fmt.Println(helpStr)

return } newPath := args[2] - err := commands.New(newPath) - if err != nil { + if err := commands.New(newPath); err != nil { fmt.Fprintf(os.Stderr, "error: new: %+v\n", err) } + case "serve": + var addr string + if len(args) == 3 { + addr = args[2] + } else { + addr = ":9191" + } + if err := commands.Serve(addr); err != nil { + fmt.Fprintf(os.Stderr, "error: serve: %+v\n", err) + } + default: + fmt.Println(helpStr) } }
M readmereadme

@@ -15,9 +15,10 @@

A simple and minimal static site generator. options: - init PATH create vite project at PATH - build builds the current project - new PATH create a new markdown post + init PATH create vite project at PATH + build builds the current project + new PATH create a new markdown post + serve [HOST:PORT] serves the 'build' directory CONFIG