summaryrefslogtreecommitdiff
path: root/site.go
blob: c7d3c192b4116512cdda7c2349da94c80cb76a09 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"os"
	"io/ioutil"
	"net/http"
	"path"
	"path/filepath"
	"strings"
)

const staticPrefix = "/static/"

type NavTree struct {
	name string
	path string
	children []NavTree
	hasIndex bool
	selected bool
}

func stripPath(caminho string) string {
	caminho = path.Clean(caminho)
	return "/" + path.Join(strings.Split(caminho, "/")[1:]...)
}

func rootTree(root string, goal string) (NavTree, string) {
	nav := NavTree{}
	nav.name = path.Base(root)
	nav.path = root
	nav.selected = false
	
	index := ""

	if !strings.HasPrefix(goal, root) {
		return nav, index
	}
	
	nav.selected = true

	filepath.Walk(root, func(current string, info os.FileInfo, err error) error {
		if info.Name() == ".index" {
			index = current
		}
		
		if info.IsDir() && info.Name()[0] == '.' {
			return filepath.SkipDir
		}
		
		if current == root || info.Name()[0] == '.'  {
			return nil
		}
		
		rec, idx := rootTree(current, goal)
		if idx != "" {
			index = idx
		}
		
		nav.children = append(nav.children, rec)

		if info.IsDir() {
			return filepath.SkipDir
		} else {
			return nil
		}
	})

	return nav, index
}

func RenderList(children []NavTree) string {
	deleg := ""
	ul := "<ul>"
	for _, s := range children {
		u, d := s.Render()
		ul += u
		deleg += d
	}
	ul += "</ul>"
	ul += deleg
	return ul
}

func (v *NavTree) Render() (string, string) {
	li := "<li><a href=\"" + stripPath(v.path) + "\">"
	if v.selected {
		li += "<span class=\"nav-intended\">"
	} else {
		li += "<span class=\"nav-not-intended\">"
	}
	li += v.name
	li += "</span></a></li>"
	
	deleg := ""

	if len(v.children) > 0 {
		deleg += RenderList(v.children)
	}
	
	return li, deleg
}

func build(request *http.Request) (string, int) {
	code := 200
	root := request.Host
	postLoc := path.Join(root, request.URL.Path)
	
	if strings.HasPrefix(request.URL.Path, staticPrefix) {
		staticLoc := path.Join(root, request.URL.Path[len(staticPrefix):])
		static, err0 := ioutil.ReadFile(staticLoc)
		if err0 != nil {
			return "Path not  found", 404
		}
		
		return string(static), 200
	}
	
	tr, idx := rootTree(root, postLoc)
	template, err0 := ioutil.ReadFile(idx)
	if err0 != nil {
		return "No template in tree", 500
	}

	final := string(template)
	
	post, err1 := ioutil.ReadFile(postLoc)
	if err1 != nil {
		viewLoc := path.Join(root, request.URL.Path, ".view")
		
		view, err1_0 := ioutil.ReadFile(viewLoc)
		if err1_0 != nil {
			return "Path not found", 404
		}
		post = view
	}

	final = strings.Replace(string(template), "{{ post }}", string(post), -1)
	final = strings.Replace(string(final), "{{ nav }}", RenderList(tr.children), -1)
	
	return final, code
}

func handler(w http.ResponseWriter, r *http.Request) {
	content, code := build(r)

	w.WriteHeader(code)
	w.Write([]byte(content))
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}