-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
43 lines (36 loc) · 1.04 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
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
func Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remoteAddr := r.RemoteAddr
if len(remoteAddr) == 0 {
remoteAddr = r.Header.Get("x-forwarded-for")
}
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
var db SparkleDatabase
func main() {
r := mux.NewRouter()
r.HandleFunc("/", defaultHandler).Methods("GET")
r.HandleFunc("/sparkles", addSparkle).Methods("POST")
r.HandleFunc("/sparkles", getSparkles).Methods("GET")
r.HandleFunc("/top/giver", topGiven).Methods("GET")
r.HandleFunc("/top/receiver", topReceived).Methods("GET")
r.HandleFunc("/sparkles/{recipient}", getSparklesForRecipient).Methods("GET")
// Load the database from file
db = LoadDB()
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
http.Handle("/", r)
log.Printf("[+] Starting the sparkles app on port %s", port)
http.ListenAndServe(":"+port, Log(http.DefaultServeMux))
}