From b2056fbf48849e321389977e3fdbb0f5841822c4 Mon Sep 17 00:00:00 2001 From: Mitch Riedstra Date: Sat, 14 Dec 2019 09:51:15 -0500 Subject: Tidy up the server command, pull the handlers into the page library. Convert to go modules. Add a build script and version to the command --- cmd/server/main.go | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'cmd') diff --git a/cmd/server/main.go b/cmd/server/main.go index 42eb449..c18aa55 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -6,40 +6,32 @@ import ( "log" "net/http" "os" - "path/filepath" "git.riedstra.us/mitch/go-website/page" ) +var VersionString = "" + +func VersionPrint() { + fmt.Println(VersionString) + os.Exit(0) +} + func main() { fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") directory := fl.String("d", ".", "Directory to serve.") + version := fl.Bool("v", false, "Print the version then exit") _ = fl.Parse(os.Args[1:]) - fmt.Println("vim-go") + if *version { + VersionPrint() + } if err := os.Chdir(*directory); err != nil { log.Fatal(err) } - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - u := r.URL.Path - if u == "/" { - u = "/index" - } - u = filepath.Join(".", u) - log.Println(u) - - p := &page.Page{Name: u} - err := p.Render(w) - if err != nil { - log.Println(err) - http.Error(w, "Internal server error", 500) - return - } - - }) - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + page.SetupHandlers() _ = http.ListenAndServe(*listen, nil) } -- cgit v1.2.3