-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
50 lines (43 loc) · 1.28 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
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
// Log what's up
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", remoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
var db SparkleDatabase
func main() {
r := mux.NewRouter()
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")
adminMode := os.Getenv("SPARKLE_ADMIN_MODE")
if adminMode == "TRUE" {
r.HandleFunc("/migrate/{from}/{to}", migrateSparkles)
}
r.HandleFunc("/graph", getSparkleGraph).Methods("GET")
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
// 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))
log.Printf("... started!")
}