-
Notifications
You must be signed in to change notification settings - Fork 21
/
awe-proxy.go
134 lines (114 loc) · 3.56 KB
/
awe-proxy.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
package main
import (
"fmt"
"github.com/MG-RAST/AWE/lib/conf"
"github.com/MG-RAST/AWE/lib/controller"
"github.com/MG-RAST/AWE/lib/core"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/logger/event"
"github.com/MG-RAST/AWE/lib/worker"
"github.com/MG-RAST/golib/goweb"
"os"
)
func launchAPI(control chan int, port int) {
c := controller.NewProxyController()
goweb.ConfigureDefaultFormatters()
r := &goweb.RouteManager{}
r.MapRest("/work", c.Work)
r.MapRest("/client", c.Client)
r.MapFunc("*", controller.ResourceDescription, goweb.GetMethod)
if conf.SSL_ENABLED {
err := goweb.ListenAndServeRoutesTLS(fmt.Sprintf(":%d", conf.P_API_PORT), conf.SSL_CERT_FILE, conf.SSL_KEY_FILE, r)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: api: %v\n", err)
logger.Error("ERROR: api: " + err.Error())
}
} else {
err := goweb.ListenAndServeRoutes(fmt.Sprintf(":%d", conf.P_API_PORT), r)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: api: %v\n", err)
logger.Error("ERROR: api: " + err.Error())
}
}
control <- 1 //we are ending
}
func main() {
err := conf.Init_conf("proxy") // TODO config not adapted for proxy
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: error reading conf file: "+err.Error())
os.Exit(1)
}
//if !conf.INIT_SUCCESS {
// conf.PrintServerUsage()
// os.Exit(1)
//}
fmt.Printf("--------AWE Proxy running--------\n\n")
conf.Print("proxy")
if _, err := os.Stat(conf.DATA_PATH); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(conf.DATA_PATH, 0777); err != nil {
fmt.Fprintf(os.Stderr, "ERROR in creating data_path %s\n", err.Error())
os.Exit(1)
}
}
if _, err := os.Stat(conf.LOGS_PATH); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(conf.LOGS_PATH, 0777); err != nil {
fmt.Fprintf(os.Stderr, "ERROR in creating log_path %s\n", err.Error())
os.Exit(1)
}
}
if _, err := os.Stat(conf.DATA_PATH + "/temp"); err != nil && os.IsNotExist(err) {
if err := os.Mkdir(conf.DATA_PATH+"/temp", 0777); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}
//init proxy mgr
core.InitResMgr("proxy")
//init logger
logger.Initialize("proxy")
//launch server
control := make(chan int)
go core.QMgr.TaskHandle()
go core.QMgr.ClientHandle()
go core.QMgr.ClientChecker()
go launchAPI(control, conf.API_PORT)
var host string
if hostname, err := os.Hostname(); err == nil {
host = fmt.Sprintf("%s:%d", hostname, conf.API_PORT)
}
logger.Event(event.SERVER_START, "host="+host)
if conf.PID_FILE_PATH != "" {
f, err := os.Create(conf.PID_FILE_PATH)
if err != nil {
err_msg := "Could not create pid file: " + conf.PID_FILE_PATH + "\n"
fmt.Fprintf(os.Stderr, err_msg)
logger.Error("ERROR: " + err_msg)
os.Exit(1)
}
defer f.Close()
pid := os.Getpid()
fmt.Fprintln(f, pid)
fmt.Println("##### pidfile #####")
fmt.Printf("pid: %d saved to file: %s\n\n", pid, conf.PID_FILE_PATH)
}
profile, err := worker.ComposeProfile()
if err != nil {
fmt.Fprintf(os.Stderr, "fail to compose profile: %s\n", err.Error())
os.Exit(1)
}
self, err := worker.RegisterWithAuth(conf.SERVER_URL, profile)
if err != nil {
fmt.Fprintf(os.Stderr, "fail to register: %s\n", err.Error())
os.Exit(1)
}
core.SetClientProfile(self)
core.InitProxyWorkChan()
fmt.Printf("Proxy registered, name=%s, id=%s\n", self.Name, self.Id)
logger.Event(event.CLIENT_REGISTRATION, "clientid="+self.Id)
if err := worker.InitWorkers(); err == nil {
worker.StartProxyWorkers()
} else {
fmt.Printf("failed to initialize and start workers:" + err.Error())
}
<-control //block till something dies
}