diff options
Diffstat (limited to 'server/main.go')
-rw-r--r-- | server/main.go | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/server/main.go b/server/main.go index a342ba5..4cf49ed 100644 --- a/server/main.go +++ b/server/main.go @@ -16,6 +16,16 @@ var me string var initialPeer string var dataDir string +type LoggingHandler struct { + Handler http.Handler +} + +// Log all HTTP requests +func (lh LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Printf("Request: %s %s%s", r.Method, me, r.URL.String()) + lh.Handler.ServeHTTP(w, r) +} + func main() { flag.StringVar(&bindAddr, "b", ":4200", "bind address") flag.StringVar(&me, "u", "http://localhost:4200", "public URL") @@ -29,20 +39,11 @@ func main() { myPos = 0 peerHashes = append(peerHashes, sha256sum(me)) hashToDomain = map[string]string{peerHashes[0]: me} - - // Start background functions - if initialPeer != "" { - go addPeer(initialPeer) - } - go cleanPeers() - go cleanKVStore() - go redistributeKeys() + kvstore = make(map[string][]byte) + users = make(map[string]user) // Load user data from disk - err := os.Mkdir(dataDir, 0755) - if err != nil { - log.Fatal(err) - } + os.Mkdir(dataDir, 0755) entries, err := os.ReadDir(dataDir) if err != nil { log.Fatal(err) @@ -51,6 +52,7 @@ func main() { id := entry.Name() reader, err := os.Open(dataDir + "/" + id + "/gob") if err != nil { + log.Fatal(err) continue } var user user @@ -59,13 +61,22 @@ func main() { users[id] = user } + // Start background functions + if initialPeer != "" { + go addPeer(initialPeer) + } + go cleanPeers() + go cleanKVStore() + go redistributeKeys() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello! This is a Kela server.") }) http.HandleFunc("/peer", peerHandler) - http.HandleFunc("/user", userHandler) - http.HandleFunc("/dht", dhtHandler) - http.HandleFunc("/storage", storageHandler) - http.HandleFunc("/message", messageHandler) - log.Fatal(http.ListenAndServe(bindAddr, nil)) + http.HandleFunc("/user/", userHandler) + http.HandleFunc("/dht/", dhtHandler) + http.HandleFunc("/storage/", storageHandler) + http.HandleFunc("/message/", messageHandler) + log.Fatal(http.ListenAndServe(bindAddr, LoggingHandler{Handler: http.DefaultServeMux})) } |