-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (133 loc) · 3.4 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
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
package main
import (
"html/template"
"log"
"net/http"
"os"
"regexp"
"strings"
)
// *****************
// Date Structures *
// *****************
type Post struct {
Title string
Slug string
Date string
Tags string
Content template.HTML
}
type Posts struct {
PostsList []Post
}
// *********
// Globals *
// *********
var PostsToList Posts
var templates = template.Must(
template.ParseFiles(
"public/index.html",
"public/template.html",
),
)
var validPath = regexp.MustCompile("^/(view)/([a-zA-Z0-9-.\u0621-\u064A]+)$")
// ****************
// Util Functions *
// ****************
func check(e error) {
if e != nil {
panic(e)
}
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func unslugify(title *string) {
t := strings.Split(*title, "-")
*title = strings.Join(t, " ")
}
// ********************
// Handlers Functions *
// ********************
// this will show all posts at /
func listHandler(w http.ResponseWriter, r *http.Request) {
postsDirectoryList, err := os.ReadDir("posts/")
check(err)
if len(PostsToList.PostsList) == 0 {
for i := len(postsDirectoryList) - 1; i >= 0; i-- {
p := postsDirectoryList[i]
title := strings.Replace(p.Name(), ".html", "", 1)
unslugify(&title)
PostsToList.PostsList = append(PostsToList.PostsList, Post{Title: title, Slug: p.Name()})
}
}
templates.ExecuteTemplate(w, "index.html", PostsToList)
}
// this will show post at /view/<slug>
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
path := "./posts/" + title
post_body, err := os.ReadFile(path)
check(err)
post_content := template.HTML(post_body)
post := Post{
Content: post_content,
}
templates.ExecuteTemplate(w, "template.html", post)
}
// this will show /cv
func viewCVHandler(w http.ResponseWriter, r *http.Request) {
path := "./public/cv.html"
cv_body, err := os.ReadFile(path)
check(err)
cv_content := template.HTML(cv_body)
cv := Post{
Content: cv_content,
}
templates.ExecuteTemplate(w, "template.html", cv)
}
// this will show /projects
func viewProjectsHandler(w http.ResponseWriter, r *http.Request) {
path := "./public/projects.html"
cv_body, err := os.ReadFile(path)
check(err)
cv_content := template.HTML(cv_body)
cv := Post{
Content: cv_content,
}
templates.ExecuteTemplate(w, "template.html", cv)
}
// this will show /robots.txt
func viewRobotsHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/robots.txt")
}
// this will show /feed.xml
func viewFeedHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/feed.xml")
}
// ***************
// Main Function *
// ***************
func main() {
http.HandleFunc("/", listHandler)
http.HandleFunc("/posts", listHandler)
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/cv", viewCVHandler)
http.HandleFunc("/projects", viewProjectsHandler)
http.HandleFunc("/feed.xml", viewFeedHandler)
http.HandleFunc("/robots.txt", viewRobotsHandler)
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
port := os.Getenv("PORT")
if port == "" {
port = "4444"
}
log.Println("listen on", ":"+port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}