summaryrefslogtreecommitdiff
path: root/server/main.go
blob: 63aadb5ff8be51fccedc0dfcbd121611f4dcb7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
	}
}