-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
140 lines (126 loc) · 2.95 KB
/
main.go
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
package main
import (
"errors"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
type Templater struct {
srcDir string
}
func NewTemplater(srcDir string) Templater {
return Templater{
srcDir: srcDir,
}
}
func (t Templater) getAllTemplates() ([]*template.Template, error) {
ret := make([]*template.Template, 0)
entries, err := os.ReadDir(t.srcDir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
if entry.Name() == "base.html" {
continue
}
t, err := template.ParseFiles(filepath.Join(t.srcDir, "base.html"), filepath.Join(t.srcDir, entry.Name()))
if err != nil {
return nil, err
}
for _, tmplName := range t.Templates() {
if tmplName.Name() != entry.Name() {
continue
}
// Print the name of the template
if !strings.HasSuffix(tmplName.Name(), ".html") {
continue
}
ret = append(ret, tmplName)
}
}
return ret, nil
}
func (t Templater) Write(dir string) error {
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
allTemplates, err := t.getAllTemplates()
if err != nil {
return err
}
for _, tmpl := range allTemplates {
outputFile, err := os.Create(filepath.Join(dir, tmpl.Name()))
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()
// Render the template (without any data, or you can pass data as needed)
err = tmpl.Execute(outputFile, nil)
if err != nil {
return fmt.Errorf("error rendering template: %v", err)
}
}
return nil
}
func (t Templater) getTemplateFromURL(url string) (*template.Template, error) {
if !strings.HasSuffix(url, ".html") {
return nil, errors.New("must end with .html")
}
if strings.Count(url, "/") != 1 {
return nil, errors.New("does not look like path")
}
fullPath := filepath.Join(t.srcDir, url)
_, err := os.Stat(fullPath)
if err != nil {
return nil, err
}
tmpl, err := template.ParseFiles(filepath.Join(t.srcDir, "base.html"), fullPath)
if err != nil {
return nil, err
}
for _, tmplName := range tmpl.Templates() {
if tmplName.Name() != filepath.Base(fullPath) {
continue
}
return tmplName, nil
}
return nil, errors.New("problem getting template")
}
func (t Templater) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tmpl, err := t.getTemplateFromURL(r.URL.Path)
if err != nil {
log.Printf("404: %s: %v", r.URL.Path, err)
http.NotFound(w, r)
return
}
err = tmpl.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), 500)
}
}
func main() {
t := NewTemplater("src/templates")
if len(os.Args) > 1 {
if os.Args[1] == "--server" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/index.html", http.StatusMovedPermanently)
})
http.Handle("/{page}", t)
log.Print("Listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
log.Fatal("Usage: [--server]")
}
err := t.Write("docs")
if err != nil {
log.Fatal(err)
}
}