-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (124 loc) · 3.48 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"github.com/jinzhu/gorm"
"github.com/orsenkucher/nothing/encio"
"github.com/orsenkucher/nothing/server"
)
// Main loads db and handles functions realized in dir server
// in file
// in encio provided code that checks password written after flag -s
// functions in file server/handlers.go
//
// DataBase includes tables Questions an Users
// We save UsersAnswers in DataBase
// fields you can see in file server/types.go
//
// There is many to many connection between Users and Questions
// realised by table UsersAnswers
// Each function in server has proper name so mostly no need
// to explain each of them
// MMR for Users and answars is rank
// all calculations for MMR provided in file server/mmr.go
var pass string
func init() {
const usage = "Provide encio password"
flag.StringVar(&pass, "encio", "", usage)
flag.StringVar(&pass, "s", "", usage+" (shorthand)")
}
func main() {
var q = flag.Bool("q", false, "update cfg.enc and quit")
if flag.Parse(); pass == "" {
if pass = os.Getenv("encio"); pass == "" {
log.Fatalln("No password provided")
}
}
key := encio.NewEncIO(pass)
cfg, err := key.GetConfig("secure/cfg.json")
if err != nil {
log.Fatalln(err)
}
if *q {
bytes, err := key.ReadFile("secure/key.jks")
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile("secure/key.secret.jks", bytes, 0644)
if err != nil {
log.Fatalln(err)
}
log.Println("`key.enc.jks` is used like `key.jks`")
bytes, err = key.ReadFile("secure/key.properties")
if err != nil {
log.Fatalln(err)
}
err = ioutil.WriteFile("secure/key.secret.properties", bytes, 0644)
if err != nil {
log.Fatalln(err)
}
log.Println("`key.enc.properties` is used like `key.secret.properties`")
log.Println("put into `<app dir>/android/key.properties`")
log.Println("Updated .enc files, quitting now")
return
}
db := NewDB(cfg)
defer db.Close()
// s := server.StartUp(db)
// fmt.Scanln()
// d := dataChecker.DataChecker{Server: s}
// d.ShowUesrsAns("ef360dec-2fa0-4e42-a1d7-be1c74c80822")
// d.ShowQuestions()
StartServer(db)
log.Print("Server Exited Properly")
}
func StartServer(db *gorm.DB) {
s := server.StartUp(db)
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
http.HandleFunc("/getQues", s.GetQuesHandler)
http.HandleFunc("/adRegister", s.AdRegisterHandler)
http.HandleFunc("/adReport", s.AdReportHandler)
http.HandleFunc("/likeReport", s.LikeReportHandler)
hsrv := &http.Server{
Addr: ":9099",
Handler: nil, // use default mux
}
go func() {
if err := hsrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
log.Print("Server Started")
<-done
log.Print("Server Stopped")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := hsrv.Shutdown(ctx); err != nil {
log.Fatalf("Server Shutdown Failed:%+v", err)
}
}
func NewDB(cfg encio.Config) *gorm.DB {
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
cfg["dbhost"].(string),
int32(cfg["dbport"].(float64)),
cfg["dbuser"].(string),
cfg["dbpass"].(string),
cfg["dbname"].(string),
)
db, err := gorm.Open(cfg["driver"].(string), dsn)
if err != nil {
log.Panicf("failed to connect database: %+v\n", err)
}
return db
}