diff options
Diffstat (limited to 'dynamic.go')
-rw-r--r-- | dynamic.go | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/dynamic.go b/dynamic.go deleted file mode 100644 index 892ba71..0000000 --- a/dynamic.go +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of FoldaWeb <https://github.com/Pacien/FoldaWeb> | ||
4 | |||
5 | FoldaWeb 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 | FoldaWeb 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 FoldaWeb. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "fmt" | ||
24 | "github.com/drbawb/mustache" | ||
25 | "net/http" | ||
26 | "path" | ||
27 | "strings" | ||
28 | ) | ||
29 | |||
30 | func handle(w http.ResponseWriter, r *http.Request) { | ||
31 | // serve static files | ||
32 | if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path), settings.exts)) { | ||
33 | http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path)) | ||
34 | return | ||
35 | } | ||
36 | |||
37 | // redirect to add the trailing slash if missing | ||
38 | if !strings.HasSuffix(r.URL.Path, "/") { | ||
39 | http.Redirect(w, r, r.URL.Path+"/", http.StatusFound) | ||
40 | return | ||
41 | } | ||
42 | |||
43 | // get the list of dirs to parse | ||
44 | request := strings.TrimSuffix(r.URL.Path, "/") | ||
45 | dirs := strings.Split(request, "/") | ||
46 | for i, dir := range dirs { | ||
47 | if i != 0 { | ||
48 | dirs[i] = path.Join(dirs[i-1], dir) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | // parse these dirs | ||
53 | elements := make(map[string][]byte) | ||
54 | for i := len(dirs) - 1; i >= 0; i-- { | ||
55 | parsed := false | ||
56 | elements, parsed = parse(path.Join(*settings.sourceDir, dirs[i]), elements, settings.exts, false) | ||
57 | if (i == len(dirs)-1) && !parsed { | ||
58 | http.Error(w, "404 page not found", http.StatusNotFound) | ||
59 | return | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // render the page | ||
64 | template := merge(elements) | ||
65 | page := mustache.Render(string(template), makeContext(r.URL.Path, *settings.sourceDir, settings.exts)) | ||
66 | |||
67 | // serve the page | ||
68 | _, err := w.Write([]byte(page)) | ||
69 | if err != nil { | ||
70 | fmt.Println(err) | ||
71 | return | ||
72 | } | ||
73 | } | ||
74 | |||
75 | func dynamic(port string) { | ||
76 | fmt.Println("Listening on: localhost:" + port) | ||
77 | http.HandleFunc("/", handle) | ||
78 | err := http.ListenAndServe(":"+port, nil) | ||
79 | if err != nil { | ||
80 | fmt.Println(err) | ||
81 | } | ||
82 | } | ||