-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
168 lines (146 loc) · 3.89 KB
/
main.go
File metadata and controls
168 lines (146 loc) · 3.89 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
rprop "github.com/bestbug456/gorpropplus"
gorest "github.com/fredmaggiowski/gorest"
"github.com/rs/cors"
"gopkg.in/mgo.v2"
)
var heroID map[string]int
func init() {
heroID = make(map[string]int)
for i := 0; i < len(heros); i++ {
heroID[heros[i]] = i
}
}
func main() {
address := os.Getenv("address")
username := os.Getenv("username")
password := os.Getenv("password")
option := os.Getenv("option")
ssl := os.Getenv("ssl")
allowedorigins := os.Getenv("allowed")
var allowedoriginsSlice []string
if allowedorigins != "" {
allowedoriginsSlice = strings.Split(allowedorigins, ",")
}
var s *mgo.Session
var err error
log.Printf("Accessing to db\n")
if ssl == "false" {
log.Printf("Accessing to db via mgo.Dial\n")
s, err = mgo.Dial(fmt.Sprintf("mongodb://%s:%s@%s/%s", username, password, address, option))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
} else {
log.Printf("Accessing to db via ssl\n")
s, err = dialUsingSSL(address, option, username, password)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
}
defer s.Close()
// Create a new handler
handler := gorest.NewHandler()
// Define and setup your custom structures.
var nnRes NNResource
var statRes StatisticsResource
go updateDatabaseInfosPeriodically(s, &nnRes, &statRes)
var herosRes HeroResource
// Register the routes.
handler.SetRoutes([]*gorest.Route{
gorest.NewRoute(&nnRes, "/nn/predict"),
gorest.NewRoute(&herosRes, "/heros"),
gorest.NewRoute(&statRes, "/stats"),
})
c := cors.New(cors.Options{
AllowedOrigins: allowedoriginsSlice,
AllowCredentials: true,
// Enable Debugging for testing, consider disabling in production
Debug: true,
})
// Insert the middleware
corsmiddleware := c.Handler(handler.GetMuxRouter(nil))
// Get the handler for your HTTP(S) server.
log.Printf("serving on 0.0.0.0:8080\n")
http.ListenAndServe("0.0.0.0:8080", corsmiddleware)
}
func getActualNewNeuralNetwork(s *mgo.Session) (*rprop.NeuralNetwork, error) {
var NN rprop.NeuralNetwork
err := s.DB("neuralnetwork").C("weights").Find(nil).One(&NN)
if err != nil {
return nil, err
}
return &NN, nil
}
func getStatistics(s *mgo.Session) (*rprop.ValidationResult, error) {
var stats rprop.ValidationResult
err := s.DB("neuralnetwork").C("score").Find(nil).One(&stats)
if err != nil {
return nil, err
}
return &stats, nil
}
func updateDatabaseInfosPeriodically(s *mgo.Session, nnRes *NNResource, statRes *StatisticsResource) {
for {
NN, err := getActualNewNeuralNetwork(s)
if err != nil {
log.Printf("%s\n", err.Error())
os.Exit(1)
}
NN.ActivationFunction = rprop.Logistic
NN.DerivateActivation = rprop.DerivateLogistic
NN.ErrorFunction = rprop.SSE
NN.DerivateError = rprop.DerivateSSE
nnRes.nn = NN
stats, err := getStatistics(s)
if err != nil {
log.Printf("%s\n", err.Error())
os.Exit(1)
}
statRes.Stats = stats
time.Sleep(5 * time.Minute)
}
}
func dialUsingSSL(addresses string, dboption string, username string, password string) (*mgo.Session, error) {
listaddresses := make([]string, 0)
for _, str := range strings.Split(addresses, ",") {
if str != "" {
listaddresses = append(listaddresses, str)
}
}
dboptions := strings.Split(dboption, "=")
if len(dboption) < 2 {
return nil, fmt.Errorf("can not found authSource keyword in order to permit SSL connection, aborting")
}
tlsConfig := &tls.Config{}
dialInfo := &mgo.DialInfo{
Addrs: listaddresses,
Database: dboptions[1],
Username: username,
Password: password,
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
return conn, err
}
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
return nil, err
}
session.EnsureSafe(&mgo.Safe{
W: 1,
FSync: false,
})
return session, nil
}