This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpd.go
55 lines (47 loc) · 1.53 KB
/
httpd.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
package main
import (
"net/http"
"github.com/gorilla/mux"
"log"
"fmt"
"code.google.com/p/go-imap/go1/imap"
"time"
)
var c *imap.Client
func requestLogger(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)
handler.ServeHTTP(w, r)
})
}
func inboxHandler(w http.ResponseWriter, r *http.Request) {
selectMailbox(c, "INBOX", true)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
fmt.Fprint(w, string(listRecent(c, 20)))
}
func allMailHandler(w http.ResponseWriter, r *http.Request) {
selectMailbox(c, "[Gmail]/All Mail", true)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
fmt.Fprint(w, string(listRecent(c, 20)))
}
func messageHandler(w http.ResponseWriter, r *http.Request) {
messageID := mux.Vars(r)["messageID"]
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-cache")
fmt.Fprint(w, string(fetchMessage(c, messageID)))
}
func httpMain(debug bool) {
c = initClient(debug)
if (c == nil) { return }
selectMailbox(c, "INBOX", true)
defer c.Logout(30 * time.Minute)
r := mux.NewRouter()
r.HandleFunc("/Inbox.json", inboxHandler)
r.HandleFunc("/AllMail.json", allMailHandler)
r.HandleFunc("/Messages/{messageID}", messageHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("www")))
http.Handle("/", r)
http.ListenAndServe(":8080", requestLogger(http.DefaultServeMux))
}