forked from dcposch/scramble
/
server.go
47 lines (40 loc) · 1.25 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
package main
import (
"fmt"
"log"
"net/http"
"runtime/debug"
)
func main() {
// Rest API
http.HandleFunc("/user/", userHandler)
http.HandleFunc("/user/me/contacts", contactsHandler)
http.HandleFunc("/user/me/key", privateKeyHandler)
http.HandleFunc("/publickeys/", publicKeysHandler)
http.HandleFunc("/email/", emailHandler)
http.HandleFunc("/box/", inboxHandler)
http.HandleFunc("/nginx_proxy", nginxProxyHandler)
// Resources
http.HandleFunc("/", staticHandler)
// SMTP Server
go StartSMTPServer()
address := fmt.Sprintf("127.0.0.1:%d", GetConfig().HttpPort)
log.Printf("Listening on http://%s\n", address)
http.ListenAndServe(address, recoverAndLog(http.DefaultServeMux))
}
func recoverAndLog(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
// Send a 500 error if a panic happens during a handler.
// Without this, Chrome & Firefox were retrying aborted ajax requests,
// at least to my localhost.
defer func() {
if e := recover(); e != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal Server Error"))
log.Printf("%s: %s", e, debug.Stack())
}
}()
handler.ServeHTTP(w, r)
})
}