Add new `vite serve` command
Anirudh Oppiliappan x@icyphox.sh
Thu, 24 Mar 2022 17:49:02 +0530
3 files changed,
49 insertions(+),
13 deletions(-)
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.go
→
main.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
readme
→
readme
@@ -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