-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (51 loc) · 1.39 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
fswatch "github.com/andreaskoch/go-fswatch"
)
func skipDotFilesAndFolders(name string) bool {
return strings.HasPrefix(path.Base(name), ".")
}
func watch(conf Config) {
watcher := fswatch.NewFolderWatcher(conf.Resources.Posts, true, skipDotFilesAndFolders, 10)
watcher.Start()
for watcher.IsRunning() {
modified := <-watcher.Modified()
if modified {
log.Println("Change detected, rebuilding pages...")
buildWebsitePages(conf)
log.Println("Done rebuilding pages")
}
}
}
func buildWebsitePages(conf Config) {
clearTempDirectory(conf)
saveDetailedPages(conf)
saveLandingPage(conf)
}
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
_ = <-sigs
log.Println("Cleaning up...")
deleteTempDirectory()
os.Exit(0)
}()
conf := getConfig()
buildWebsitePages(conf)
go watch(conf)
http.Handle("/", http.FileServer(http.Dir(path.Join(os.TempDir(), "parellagram"))))
http.HandleFunc("/uptime", Uptime)
http.Handle("/styles/", http.StripPrefix("/styles/", http.FileServer(http.Dir(conf.Resources.Styles))))
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(conf.Resources.Images))))
log.Println("Listening on port :", conf.Website.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", conf.Website.Port), nil))
}