-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
61 lines (49 loc) · 1.18 KB
/
app.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
package main
import (
"net/http"
"os"
"strconv"
"github.com/gorilla/websocket"
"github.com/dush-t/goloca/db"
"github.com/dush-t/goloca/pool"
)
// Config stores global variables that the app needs. These
// variables will be passed to functions using dependency
// injection
type Config struct {
Store db.Store
Pool pool.Pool
SocketUpgrader websocket.Upgrader
}
func buildDBInsertAction(conf Config) pool.WorkerAction {
return func(job pool.Job) error {
err := conf.Store.Insert(job.DataPoint)
if err != nil {
return err
}
job.StatusChan <- true
return nil
}
}
// InitializeApp will create a database connection and start the threadpool
func InitializeApp() Config {
// Load environment variables
LoadEnv("config.env")
var conf Config
// Connect to datastore.
store := db.CreateStore()
conf.Store = store
// Create worker pool
var p pool.Pool
numWorkers, _ := strconv.Atoi(os.Getenv("NUM_POOL_WORKERS"))
p.Start(numWorkers, buildDBInsertAction(conf))
conf.Pool = p
// Setup websocket upgrader
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
conf.SocketUpgrader = upgrader
return conf
}