aboutsummaryrefslogtreecommitdiff
path: root/cmd/server/main.go
blob: 42eb449e55fc93e5c8bc54f24fa40a6e252b020d (plain) (blame)
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
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)
}