diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | build.sh | 11 | ||||
| -rw-r--r-- | cmd/server/main.go | 32 | ||||
| -rw-r--r-- | go.mod | 9 | ||||
| -rw-r--r-- | go.sum | 8 | ||||
| -rw-r--r-- | page/http.go | 34 |
6 files changed, 75 insertions, 20 deletions
@@ -1 +1,2 @@ *site* +server diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..e8630f3 --- /dev/null +++ b/build.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -x +set -e + +version="$(git log --format="%h %d" -1)" + +if ! git diff-index --quiet HEAD ; then + version="dirty: $version" +fi + +go build -ldflags="-X 'main.VersionString=$version'" ./cmd/server 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) } @@ -0,0 +1,9 @@ +module git.riedstra.us/mitch/go-website + +go 1.13 + +require ( + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + gopkg.in/russross/blackfriday.v2 v2.0.0 + gopkg.in/yaml.v2 v2.2.7 +) @@ -0,0 +1,8 @@ +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA= +gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI= +gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/page/http.go b/page/http.go new file mode 100644 index 0000000..acf9147 --- /dev/null +++ b/page/http.go @@ -0,0 +1,34 @@ +package page + +import ( + "log" + "net/http" + "path/filepath" +) + +func SetupHandlers() { + http.Handle("/static/", StaticHandler()) + http.HandleFunc("/", PageHandler) +} + +func PageHandler(w http.ResponseWriter, r *http.Request) { + u := r.URL.Path + if u == "/" { + u = "/index" + } + u = filepath.Join(".", u) + log.Println(u) + + p := &Page{Name: u} + err := p.Render(w) + if err != nil { + log.Println(err) + http.Error(w, "Internal server error", 500) + return + } + +} + +func StaticHandler() (h http.Handler) { + return http.StripPrefix("/static/", http.FileServer(http.Dir("static"))) +} |
