-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
279 lines (240 loc) · 8.72 KB
/
server.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package bloke
import (
"net/http"
"strings"
"log"
"os"
"path"
"io/ioutil"
"text/template"
"github.com/howeyc/fsnotify"
)
/* TODO
- bubbles only display one line!
- better home/blog definition
- pdfs in bubbles?
- clean up js bubbles so they follow user as they scroll
- add "technical explanation" part to bubbles - + meta info?
*/
// bloke should be launched from the sites root
// should be installed in gopath/src/github/ebuchman/bloke...
var GoPath = os.Getenv("GOPATH")
var BlokePath = GoPath + "/src/github.com/ebuchman/bloke" // is there a nicer way to get this?
var NewBubbleString = "This bubble has not been written yet" // this will be changed to refer you to the github repo once it's configured :)
// main site struct
type Globals struct{
Projects [][]string // list of pairs (url/filename, display name)
SubProjects map[string][][]string // map from project-filename to list of pairs (subproject filenames, subproject displayname). these generate the dropdowns
// all proj/subproj references should be with url_name!
Posts map[string]map[string]map[string][]string // year, month, day, title
RecentPosts [][]string // [](title, date_name)
Config ConfigType // config struct loaded from config.json
SiteRoot string // path to the site
webhookSecret []byte // secret key for authenticating github webhook requests
Templates *template.Template
mux *http.ServeMux // when many blokes are run behind one serve, give each a routing mux (instead of standalone server)
UpdateHandler UpdateHandler // called with names of updated bubbles
html bool // whether to serve html pages from _sites/ or to generate on the fly
watch bool // whether to watch dir for changes
}
// for handling bubble updates
// this can be ignored, if the updates are in text files being served
// it can trigger database lookups/modifications
// it can do whatever else you want
// point is, separate bloke site generator from the bupble database
type UpdateHandler interface{
// takes a list of the names of the updated bubbles
HandleUpdate(map[string]int)
}
// not even using this. is bloke is running as a simple site generator, no one cares, this is nil
// if bloke is being hosted, you probably want to implement an UpdateHandler to talk to the db
type baseUpdateHandler struct{
}
func (b baseUpdateHandler) HandleUpdate(updates map[string]int){
log.Println("booya nigga")
log.Println(updates)
}
type Router interface{
ApplyRouting(*http.ServeMux, *Globals)
}
type baseRouter struct{
}
// new ServeMux.
func (g *Globals) NewServeMux(r Router){
g.mux = http.NewServeMux()
if r == nil{
r = baseRouter{}
}
r.ApplyRouting(g.mux, g)
}
// serve over the mux
func (g *Globals) ServeHTTP(w http.ResponseWriter, r *http.Request){
g.mux.ServeHTTP(w, r)
}
// launch a new live bloke
func LiveBloke(SitePath string, no_html bool, update_handler UpdateHandler) Globals{
var g = Globals{}
g.html = !no_html // whether or not to serve html from _sites/
g.LoadConfig(SitePath) // load config
g.AssembleSite() // assemble site composition from dir contents
if g.html{
g.SaveSite()
}
if g.watch{
g.NewWatcher(SitePath) // watch the root directory
}
g.UpdateHandler = update_handler//baseUpdateHandler{}
g.NewServeMux(nil) // creates g.mux and applies standard routing rules
return g
}
// create new globals, copy over (eg. after git pull)
// takes a list of bubble names recently updates
func (g *Globals) Refresh(updates map[string]int){
// if nil, its coming from local watchdir callback, which is experimental
// and kind of shitty
// TODO: close all old g things!
// if a bubble is updated, we need to know!
log.Println(g.Config.SiteName, g.UpdateHandler)
if g.UpdateHandler != nil{
g.UpdateHandler.HandleUpdate(updates)
}
*g = LiveBloke(g.SiteRoot, g.html, g.UpdateHandler)
}
// serve static files (assets: js, css)
func (g *Globals) ServeBlokeFile(w http.ResponseWriter, r *http.Request){
if strings.Contains(r.URL.Path, "."){
subs := strings.Split(r.URL.Path, ".")
ext := subs[len(subs)-1]
if ext == "js" || ext == "css"{
p := path.Join(g.SiteRoot, r.URL.Path[1:])
_, err := os.Stat(p)
if err == nil{
http.ServeFile(w, r, p)
}
}
}
}
// serve static files (imgs, files)
func (g *Globals) ServeFile(w http.ResponseWriter, r *http.Request){
if strings.Contains(r.URL.Path, "."){
subs := strings.Split(r.URL.Path, ".")
ext := subs[len(subs)-1]
if ext == "png" || ext == "jpg" || ext == "pdf" {
p := path.Join(g.SiteRoot, r.URL.Path[1:])
_, err := os.Stat(p)
if err == nil{
http.ServeFile(w, r, p)
}
} else if ext == "js" || ext == "css"{
p := path.Join(g.SiteRoot, r.URL.Path[1:])
_, err := os.Stat(p)
if err == nil{
http.ServeFile(w, r, p)
}
}
}
}
// serve a single html page
func (g *Globals) servePage(w http.ResponseWriter, r *http.Request){
if !strings.Contains(r.URL.Path, "."){
http.ServeFile(w, r, g.SiteRoot+"/sites/main.html") //+r.URL.Path[1:])
//s.handleIndex(w, r)
}else{
http.ServeFile(w, r, g.SiteRoot+"/sites/main.html") //+r.URL.Path[1:])
}
}
// watch directory callback
// kind of messy since it fires soo much
func (g *Globals) WatchDirCallback(watcher *fsnotify.Watcher){
for {
select {
case ev := <-watcher.Event:
log.Println("event:", ev)
if ev != nil{
// only refresh if name is not hidden
split := strings.Split(ev.Name, "/")
name := split[len(split)-1]
if !strings.HasPrefix(name, ".") && !strings.HasSuffix(name, "~"){
g.Refresh(nil)
}
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
defer watcher.Close()
}
// recursive watch all directories
func (g *Globals) WatchDirs(watcher *fsnotify.Watcher, dir string){
err := watcher.Watch(dir)
if err != nil {
log.Println("Could'nt watch dir", dir, err)
}
files := ReadDir(dir)
if err != nil{
log.Println("Couldn't read dir", dir, err)
}
for _, f := range files{
if f.IsDir(){
g.WatchDirs(watcher, path.Join(dir, f.Name()))
}
}
}
// create new watcher for directory
// cleanup/close!
func (g *Globals) NewWatcher(SiteRoot string){
// set up new watcher (should only be used for local changes (otherwise use github))
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
// watch dir callback
go g.WatchDirCallback(watcher)
// recursive watch dirs. when an event fires in any dir, it'll hit the callback
g.WatchDirs(watcher, SiteRoot)
}
// apply a set of routing rules to a mux using a bloke globals struct
func (r baseRouter) ApplyRouting(mux *http.ServeMux, g *Globals){
mux.HandleFunc("/", g.handleIndex) // main page (/, /postname, /pagename)
mux.HandleFunc("/imgs/", g.ServeFile) // static images (png, jpg)
mux.HandleFunc("/files/", g.ServeFile) // static documents (pdfs)
mux.HandleFunc("/assets/", g.ServeBlokeFile) // static js, css files
mux.HandleFunc("/bubbles/", g.ajaxBubbleResponse) // async bubbles
mux.HandleFunc("/pages/", g.ajaxPagesResponse) // async page loads
mux.HandleFunc("/posts/", g.ajaxPagesResponse) // async post loads
mux.HandleFunc("/git/", g.gitResponse) // github webhook
}
func RedirectTLS(w http.ResponseWriter, r *http.Request){
host := r.Host
log.Println("https://"+host)
http.Redirect(w, r, "https://"+host, 301)
}
func RedirectServer(){
mux := http.NewServeMux()
mux.HandleFunc("/", RedirectTLS)
http.ListenAndServe(":80", mux)
}
// start a http or https server listening on addr routing with the mux
func StartServer(addr string, mux *http.ServeMux, tls bool){
log.Println("wtf!")
if tls{
_, err := ioutil.ReadDir("certs")
if err != nil{
log.Fatal("could not find certs dir", err)
}
err = http.ListenAndServeTLS(addr, "certs/ssl.crt", "certs/ssl.key", mux)
if err != nil{
log.Println("err on tls", err)
}
} else{
err := http.ListenAndServe(addr, mux)
if err != nil{
log.Println("err on http server", err)
}
}
}
// standalone server for running your own bloke
func StartBloke(addr, SiteRoot string, tls bool, no_html bool) {
g := LiveBloke(SiteRoot, no_html, nil)
StartServer(addr, g.mux, tls)
}