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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
)
type Post struct {
Time int
Meta []byte
Thumbnail []byte
Content []byte
}
type Posts struct {
Contents []*Post
}
func loadPosts(location string, postCount int) (p *Posts, err error) {
if postCount == 0 || postCount < -1 {
return nil, os.ErrInvalid
}
var tmpPosts []*Post
if postCount == 1 {
tmpPosts = append(tmpPosts, readFile(location+".html"))
} else {
var depthCount = 0
// TODO - Use less computationally heavy opperation for scalability
err = filepath.Walk(location, func(path string, _ os.FileInfo, _ error) error {
var directory, err = regexp.MatchString("^data/(projects|blog)$", path)
if err != nil {
return err
}
if directory {
depthCount++
} else if postCount == -1 || depthCount < postCount+1 {
tmp := readFile(path)
tmpPosts = append(tmpPosts, tmp)
depthCount++
} else {
depthCount++
}
return nil
})
if err != nil {
return nil, err
}
sort.Slice(tmpPosts, func(i, j int) bool {
return tmpPosts[i].Time > tmpPosts[j].Time
})
}
return &Posts{Contents: tmpPosts}, nil
}
func readFile(location string) (p *Post) {
file, err := os.Open(location)
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(file)
var section = 0
var tmp *Post = new(Post)
LineLoop:
for scanner.Scan() {
var line = scanner.Text()
var lineByte = []byte(line + "\n")
if line == "{{br}}" { // this is not happy with any space or tab characters that follow,
section++ // so make sure to check if there's whitespace after a {{br}} if
goto LineLoop // there are any errors. I'll fix this later...TM
}
switch section {
case 0:
time, err := strconv.Atoi(strings.TrimSuffix(string(lineByte), "\n"))
if err != nil {
log.Fatal(err)
} else {
tmp.Time = time
}
case 1:
tmp.Meta = append(tmp.Meta, lineByte...)
case 2:
tmp.Thumbnail = append(tmp.Thumbnail, lineByte...)
case 3:
tmp.Content = append(tmp.Content, lineByte...)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
file.Close()
return tmp
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Posts) {
if tmpl == "post" {
t, err := template.ParseFiles("templates/master.html", "templates/postHelp.tmpl")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, p.Contents[0])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
t, err := template.ParseFiles("templates/master.html", "templates/"+tmpl+".html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")
if path[1] != "" {
http.NotFound(w, r)
} else {
p, err := loadPosts("data/projects", 3)
if err != nil {
http.NotFound(w, r)
}
renderTemplate(w, "index", p)
}
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "about", nil)
}
func agHandler(w http.ResponseWriter, r *http.Request) {
p, err := loadPosts("data"+strings.TrimSuffix(r.URL.Path, "/"), -1)
if err != nil {
http.NotFound(w, r)
}
renderTemplate(w, strings.TrimPrefix(strings.TrimSuffix(r.URL.Path, "/"), "/"), p)
}
func postHandler(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")
if path[2] == "" {
agHandler(w, r)
} else {
p, err := loadPosts("data/"+path[1]+"/"+path[2], 1)
if p == nil || err != nil {
http.NotFound(w, r)
} else {
renderTemplate(w, "post", p)
}
}
}
func fileHandler(w http.ResponseWriter, r *http.Request) {
body, _ := os.ReadFile("data/www/" + r.URL.Path)
w.Header().Set("Content-Type", "text/css; charset=utf-8")
fmt.Fprintf(w, "%s", body)
}
func makeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fn(w, r)
}
}
func httpsRedirect(w http.ResponseWriter, req *http.Request) {
// remove/add not default ports from req.Host
target := "https://" + req.Host + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}
log.Printf("redirect to: %s", target)
http.Redirect(w, req, target,
// see comments below and consider the codes 308, 302, or 301
http.StatusMovedPermanently)
}
func main() {
/* hostValid, _ := regexp.MatchString("^alexscerba.com$", r.URL.Path)
if !hostValid {
req, _ := http.NewRequest(r.Method, "alexscerba.com"+r.URL.Path, r.Body)
redirect(w, req)
} */
fs := http.FileServer(http.Dir("./data/static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/projects/", postHandler)
http.HandleFunc("/blog/", postHandler)
http.HandleFunc("/about/", aboutHandler)
http.HandleFunc("/", rootHandler)
go http.ListenAndServe(":80", http.HandlerFunc(httpsRedirect))
log.Fatal(http.ListenAndServeTLS(":443", "/etc/letsencrypt/live/alexscerba.com/fullchain.pem", "/etc/letsencrypt/live/alexscerba.com/privkey.pem", nil))
//log.Fatal(http.ListenAndServe(":4000", nil)) // for local dev because I'm lazy
}
|