diff options
Diffstat (limited to 'server/main.go')
-rw-r--r-- | server/main.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..63aadb5 --- /dev/null +++ b/server/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "net/http" + "flag" + "path/filepath" + log "github.com/sirupsen/logrus" +) + + +func main() { + log.SetFormatter(&log.TextFormatter{ + FullTimestamp: true, + }); + + pathFlag := flag.String( + "path", "data", "the path to load site data from", + ); + addrFlag := flag.String( + "addr", "127.0.0.1:8080", "the address to serve from", + ); + flag.Parse(); + log.Infof("data path: %v", *pathFlag); + log.Infof("serving from %v", *addrFlag); + + mux := http.NewServeMux() + mux.HandleFunc("/ip", func(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "%v", req.RemoteAddr); + }); + mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/" { + path := filepath.Join(*pathFlag, req.URL.Path); + Serve(w, req, path); + } else { + Serve(w, req, filepath.Join(*pathFlag, "index.md")); + } + }); + + server := http.Server{ + Addr: *addrFlag, + Handler: mux, + }; + err := server.ListenAndServe(); + if err != nil { + log.Fatal(err); + } +} |