-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
74 lines (65 loc) · 2.21 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"errors"
"fmt"
"log"
"net/http"
)
const (
host = "localhost"
port = "8080"
adminUser = "admin"
adminPass = "admin"
// NikkiHaley candidate
NikkiHaley = 1
// KamalaHarris candidate
KamalaHarris = 2
)
var immudbClient = ImmudbClient{}
func main() {
fmt.Println(
" _ __ _\n" +
" (_)___ ___ ____ ___ __ ___ ______ / /_(_)___ ____ _\n" +
" / / __ `__ \\/ __ `__ \\/ / / / | / / __ \\/ __/ / __ \\/ __ `/\n" +
" / / / / / / / / / / / / /_/ /| |/ / /_/ / /_/ / / / / /_/ /\n" +
"/_/_/ /_/ /_/_/ /_/ /_/\\__,_/ |___/\\____/\\__/_/_/ /_/\\__, /\n" +
"e l e c t i o n s a n y o n e c a n v e r i f y \\____/\n")
// fmt.Print("e l e c t i o n s a n y o n e c a n v e r i f y\n\n\n")
// init immudb client
immudbClient.Init(&ImmudbConfig{
Address: "localhost:3322",
DB: "defaultdb",
User: "immudb",
Password: "immudb",
LocalStateDir: "",
})
if err := immudbClient.Connect(); err != nil {
log.Fatalf("error connecting to immudb: %v", err)
}
defer immudbClient.Disconnect()
// create immuvoting admin user
hashedPassword, err := HashAndSaltPassword("admin")
if err != nil {
log.Fatalf("error hashing and salting password: %v", err)
}
if err := immudbClient.Set(
[]byte("immuvoting:user:admin"), []byte(hashedPassword)); err != nil &&
!errors.Is(err, ErrAlreadyExists) {
log.Fatalf("error creating admin user: %v", err)
}
// setup HTTP handlers
http.HandleFunc("/register-voter", cors(registerVoterHandler))
http.HandleFunc("/vote", cors(voteHandler))
http.HandleFunc("/voter-status", cors(getVoterStatusHandler))
http.HandleFunc("/ballot", cors(getBallotHandler))
http.HandleFunc("/random-ballot", cors(getRandomBallotHandler))
http.HandleFunc("/state", cors(getStateHandler))
http.HandleFunc("/verifiable-tx", cors(getVerifiableTransactionHandler))
http.HandleFunc("/stats", cors(getStatsHandler))
// NOTE: to add a handler which requires auth, wrap the handler with corsAndBasicAuth(...)
fmt.Println("listening on port", port)
// start server
if err = http.ListenAndServe(host+":"+port, nil); err != nil {
log.Fatalf("error starting HTTP server: %v", err)
}
}