-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (62 loc) · 1.69 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
75
76
package main
import (
"fmt"
"log"
"net/http"
"./conf"
_ "./models"
_ "github.com/lib/pq"
"database/sql"
"./handlers/events"
"./handlers/users"
"./utils"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
// checkError check errors
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
// load config
config, err := conf.NewConfig("config.yaml").Load()
checkError(err)
sqlBind := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
config.DB.UserName,
config.DB.UserPassword,
config.DB.Database,
)
// http server address and port
hostBind := fmt.Sprintf("%s:%s",
"0.0.0.0",
"3000",
)
utils.DBCon, err = sql.Open("postgres", sqlBind)
utils.RedisCon = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
uh := users.NewUserHandler()
eh := events.NewEventHandler()
r := mux.NewRouter()
// users
r.HandleFunc("/auth", uh.Auth).Methods("POST")
r.HandleFunc("/profile", uh.GetUser).Methods("GET")
// events
r.HandleFunc("/new_event", eh.NewEvent).Methods("POST")
r.HandleFunc("/my_events", eh.MyEvents).Methods("GET")
r.HandleFunc("/check_event", eh.CheckEvent).Methods("POST")
r.HandleFunc("/events", eh.AllEvents).Methods("GET")
r.HandleFunc("/event", eh.GetEvent).Methods("GET")
r.HandleFunc("/point", eh.GetPoint).Methods("GET")
r.HandleFunc("/join_event", eh.JoinEvent).Methods("POST")
r.HandleFunc("/enter_token", eh.EnterToken).Methods("POST")
r.HandleFunc("/answer_question", eh.AnswerQuestion).Methods("POST")
r.Use(utils.AuthenticationMiddleware)
// start server
log.Println("Listening on", hostBind)
http.ListenAndServe(hostBind, r)
}