aboutsummaryrefslogtreecommitdiff
path: root/dynamic.go
diff options
context:
space:
mode:
Diffstat (limited to 'dynamic.go')
-rw-r--r--dynamic.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/dynamic.go b/dynamic.go
new file mode 100644
index 0000000..0307003
--- /dev/null
+++ b/dynamic.go
@@ -0,0 +1,69 @@
1/*
2
3 This file is part of CompileTree (https://github.com/Pacien/CompileTree)
4
5 CompileTree is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 CompileTree is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with CompileTree. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20package main
21
22import (
23 "fmt"
24 "github.com/hoisie/mustache"
25 "net/http"
26 "path"
27 "strings"
28)
29
30func handle(w http.ResponseWriter, r *http.Request) {
31 // serve static files
32 if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path))) {
33 http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path))
34 return
35 }
36
37 // get the list of dirs to parse
38 request := strings.Trim(r.URL.Path, "/")
39 dirs := strings.Split(request, "/")
40 if request != "" {
41 dirs = append(dirs, "")
42 }
43
44 // parse these dirs
45 elements := make(map[string][]byte)
46 for _, dir := range dirs {
47 parse(path.Join(*settings.sourceDir, dir), elements, false)
48 }
49
50 // render the page
51 template := merge(elements)
52 page := mustache.Render(string(template), nil /* TODO: generate contextual variables */)
53
54 // serve the page
55 _, err := w.Write([]byte(page))
56 if err != nil {
57 fmt.Println(err)
58 return
59 }
60}
61
62func dynamic(port string) {
63 fmt.Println("Listening on: localhost:" + port)
64 http.HandleFunc("/", handle)
65 err := http.ListenAndServe(":"+port, nil)
66 if err != nil {
67 fmt.Println(err)
68 }
69}