diff options
Diffstat (limited to 'server/main.go')
-rw-r--r-- | server/main.go | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/server/main.go b/server/main.go index 3ad3060..b4fc654 100644 --- a/server/main.go +++ b/server/main.go @@ -1,49 +1,62 @@ package main import ( - "crypto/ed25519" - "crypto/sha256" - "encoding/base64" + "encoding/gob" "flag" + "fmt" "log" "net/http" + "os" "sync" ) -type user struct { - pubkey []byte - servers []string -} - var mu sync.Mutex +var bindAddr string var me string -var myHash string -var myPos int -var hashToDomain map[string]string -var peerHashes []string -var kvstore map[string][]byte +var initialPeer string +var dataDir string func main() { - bindAddr := flag.String("b", ":4200", "bind address") - publicURL := flag.String("u", "http://localhost:4200", "public URL") - peer := flag.String("i", "", "initial peer") + flag.StringVar(&bindAddr, "b", ":4200", "bind address") + flag.StringVar(&me, "u", "http://localhost:4200", "public URL") + flag.StringVar(&initialPeer, "i", "", "initial peer") + flag.StringVar(&dataDir, "d", ".", "data directory") flag.Parse() - log.Printf("Starting %s %s %s", *bindAddr, *publicURL, *peer) + log.Printf("Starting %s %s %s %s", bindAddr, me, initialPeer, dataDir) // Record myself - me = *publicURL myHash = sha256sum(me) myPos = 0 peerHashes = append(peerHashes, sha256sum(me)) hashToDomain = map[string]string{peerHashes[0]: me} - if *peer != "" { - go addPeer(*peer) + if initialPeer != "" { + go addPeer(initialPeer) + } + go cleanPeers() + go cleanKVStore() + + // Load user data from disk + entries, _ := os.ReadDir(dataDir) + for _, entry := range entries { + id := entry.Name() + reader, err := os.Open(dataDir + "/" + id + "/gob") + if err != nil { + continue + } + var user user + dec := gob.NewDecoder(reader) + dec.Decode(&user) + users[id] = user } + 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)) + log.Fatal(http.ListenAndServe(bindAddr, nil)) } |