This repository has been archived by the owner on Oct 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisk.go
122 lines (106 loc) · 2.84 KB
/
whisk.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
package whisk
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
)
const (
dataDir = "data"
logFilePath = dataDir + "/whisk.log"
cleanInterval = time.Minute * 15
port = "443"
)
var confirmPage *template.Template // confirmation page
// Launch starts server, initializes log, register, etc.
func Launch() {
initDataDir()
initLog()
initHome()
initPages()
initRegister()
startSweep()
startServer()
}
// Cleanup sweeps/writes the page register
func Cleanup() {
sweepRegister()
writeRegister()
}
// initDataDir creates data directory if it doesn't exist
func initDataDir() {
err := os.MkdirAll(dataDir, 0755)
if err != nil {
log.Fatalf("Error creating data directory: %s\n", err)
}
}
// initLog creates log file if it doesn't exist, sets output
func initLog() {
f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error initializing log file: %s\n", err)
}
log.SetOutput(f)
}
// initHome initializes templates used on homepage
func initHome() {
var err error
confirmPage, err = template.ParseFiles("resources/confirm-template.html")
if err != nil {
log.Fatalf("Error initializing template: %s\n", err)
}
}
// startSweep will sweep/write the register on given interval
func startSweep() {
ticker := time.NewTicker(cleanInterval)
go func() {
for range ticker.C {
Cleanup()
}
}()
}
// startServer sets up http routes, listens/serves
func startServer() {
http.HandleFunc("/public/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// HTML forms use '\r\n' instead of '\n'
r.ParseForm()
body := strings.Replace(r.Form["body"][0], "\r\n", "\n", -1)
// make new page, redirect to confirmation
newPage, err := createPage([]byte(body))
if err != nil {
http.ServeFile(w, r, "resources/error.html")
return
}
confirmPage.Execute(w, newPage)
} else {
http.ServeFile(w, r, "resources/404.html")
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
address := r.URL.Path[1:]
if len(address) == 0 {
http.ServeFile(w, r, "resources/home.html")
return
}
if len(address) != idLength || !pageInRegister(address) {
http.ServeFile(w, r, "resources/404.html")
return
}
http.ServeFile(w, r, pagesDir+address+".html")
})
log.Printf("Starting server on port %s...\n", port)
fmt.Printf("Starting server on port %s...\n", port)
go http.ListenAndServe(":80", http.HandlerFunc(redirect)) // redirect http to https
log.Fatal(http.ListenAndServeTLS(":"+port, "whisk.ws.crt", "whisk.ws.key", nil))
}
// redirect to https
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://whisk.ws"+r.RequestURI, http.StatusMovedPermanently)
}