diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2019-12-01 14:54:54 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2019-12-01 14:54:54 -0500 |
| commit | c19536be6e4f8733af329204a50ffc254a679ee5 (patch) | |
| tree | 0b464d70bbd1631c42c904ccf275a9aee50480d1 /cmd/server/main.go | |
| download | go-website-c19536be6e4f8733af329204a50ffc254a679ee5.tar.gz go-website-c19536be6e4f8733af329204a50ffc254a679ee5.tar.xz | |
Initial Commit. Small command to convert my old setup and basic server that reads off of the disk for the new setup
Diffstat (limited to 'cmd/server/main.go')
| -rw-r--r-- | cmd/server/main.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..42eb449 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "os" + "path/filepath" + + "git.riedstra.us/mitch/go-website/page" +) + +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.") + _ = fl.Parse(os.Args[1:]) + + fmt.Println("vim-go") + + 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")))) + _ = http.ListenAndServe(*listen, nil) +} |
