aboutsummaryrefslogtreecommitdiff
path: root/server/main.go
blob: 3ad3060a63b6162ab6b84bd827b84429dd2539b6 (plain)
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
package main

import (
	"crypto/ed25519"
	"crypto/sha256"
	"encoding/base64"
	"flag"
	"log"
	"net/http"
	"sync"
)

type user struct {
	pubkey []byte
	servers []string
}

var mu sync.Mutex
var me string
var myHash string
var myPos int
var hashToDomain map[string]string
var peerHashes []string
var kvstore map[string][]byte

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.Parse()
	log.Printf("Starting %s %s %s", *bindAddr, *publicURL, *peer)

	// 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)
	}

	http.HandleFunc("/peer", peerHandler)
	http.HandleFunc("/dht", dhtHandler)
	http.HandleFunc("/storage", storageHandler)
	http.HandleFunc("/message", messageHandler)
	log.Fatal(http.ListenAndServe(*bindAddr, nil))
}