-
Notifications
You must be signed in to change notification settings - Fork 7
/
gos.gxml
297 lines (260 loc) · 7.93 KB
/
gos.gxml
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?xml version="1.0" encoding="UTF-8"?>
<gos>
<!-- xml docs : http://golangserver.com/docs/markup.html -->
<deploy>webapp</deploy>
<port>9001</port>
<package>if-package-is-library</package>
<not_found>/your-404-page</not_found>
<error>/your-500-page</error>
<output>application.go</output>
<domain></domain><!-- Cookie domain -->
<var type="*MegaConfig">Config</var>
<var type="TrLock">GL</var>
<var type="bool">isInContainer</var>
<import src="github.com/cheikhshift/momentum/gos.gxml"/>
<import src="strings"/>
<import src="math/rand"/>
<import src="sync"/>
<import src="github.com/cheikhshift/gos/core"/>
<import src="flag"/>
<import src="os"/>
<main>
nobrowser := flag.Bool("nobrowser", false, "Launch without openning browser")
worker := flag.Bool("worker", false, "Launch megalith instance as worker")
dispaddr := flag.String("dispatcher", DefaultAddress, "Host name of dispatcher instance. Add port number as needed. ie hostname:9000")
workaddr := flag.String("hostname", DefaultAddress, "Host name of worker instance. Add port number as needed. ie hostname:9000")
portNumber := flag.String("port", DefaultPort, "The port number megalith will to listen on")
fws := flag.String("workspace", "megaWorkSpace", "Set instance directory")
container := flag.Bool("container", false, "Get Dispatcher and hostname addresses from env. variables.")
flag.Parse()
WorkerMode = *worker
if *container == false {
DispatcherAddressPort = *dispaddr
WorkerAddressPort = *workaddr
megaWorkspace = *fws
if *portNumber != DefaultPort {
os.Setenv(PORT, *portNumber)
}
} else {
DispatcherAddressPort = os.ExpandEnv("$DISPATCHER_ADDR")
WorkerAddressPort = os.ExpandEnv("$WORKER_ADDR")
}
isInContainer = *container
ChdirHome()
GL = TrLock{Lock: new (sync.RWMutex)}
if !WorkerMode {
InitConfigLoad()
if !*nobrowser {
LaunchBrowser()
}
if WorkerAddressPort != DefaultAddress {
RegisterWorker(WorkerAddressPort)
}
ticker := time.NewTicker(Checkinterval)
go MegaTimer(ticker)
} else {
SelfAnnounce(DispatcherAddressPort)
Config = &MegaConfig{}
}
</main>
<key>a very very very very secret key</key>
<header>
<!-- Moved data types to mega_types.go -->
</header>
<methods>
<func name="Mega" var="" return="(result *MegaConfig)">
if WorkerAddressPort != DefaultAddress {
LoadConfig(&Config)
}
ClearHistory()
ShouldLock()
defer ShouldUnlock()
return Config
</func>
<func name="AddServer" var="" return="(result []Server)">
ShouldLock()
randint := rand.Intn(200) + 50 + len(Config.Servers)
genimage := fmt.Sprintf("https://picsum.photos/%v/%v",randint, randint)
ns := Server{ID : core.NewLen(20), Nickname:"New server",Image : genimage}
Config.Servers = append(Config.Servers, ns)
if DispatcherAddressPort != DefaultAddress {
ioutil.WriteFile(fmt.Sprintf(urlformat, GenLogName(ns.ID), LockExt), OK ,0700)
}
ShouldUnlock()
SaveConfig(&Config);
return Config.Servers
</func>
<func name="DServer" var="req Server" return="(result []Server)">
result = []Server{}
ShouldLock()
for _,target := range Config.Servers {
if target.ID != req.ID {
result = append(result, target)
}
}
DeleteLog(req.ID)
Config.Servers = result
ShouldUnlock()
SaveConfig(&Config);
return
</func>
<func name="UServer" var="req Server" return="(result bool)">
ShouldLock()
for index,target := range Config.Servers {
if target.ID == req.ID {
Config.Servers[index] = req
}
}
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<!-- contact funcs -->
<func name="AddContact" var="" return="(result []Contact)">
ShouldLock()
nc := Contact{ID : core.NewLen(20), Nickname:"New contact"}
Config.Contacts = append(Config.Contacts, nc)
ShouldUnlock()
SaveConfig(&Config);
return Config.Contacts
</func>
<func name="GetLog" var="req Server" return="(result RequestLog)">
ShouldLock()
LoadLog(req.ID, &result)
ShouldUnlock()
return
</func>
<func name="DContact" var="req Contact" return="(result []Contact)">
result = []Contact{}
ShouldLock()
for _,target := range Config.Contacts {
if target.ID != req.ID {
result = append(result, target)
}
}
Config.Contacts = result
ShouldUnlock()
SaveConfig(&Config);
return
</func>
<func name="UContact" var="req Contact" return="(result bool)">
ShouldLock()
for index,target := range Config.Contacts {
if target.ID == req.ID {
Config.Contacts[index] = req
}
}
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<!-- setting funcs -->
<func name="UMail" var="req MailSettings" return="(result bool)">
ShouldLock()
Config.Mail = req
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<func name="UTw" var="req TwilioInfo" return="(result bool)">
ShouldLock()
Config.SMS = req
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<func name="USetting" var="req Settings" return="(result bool)">
ShouldLock()
Config.Misc = req
Config.LastReset = time.Now().Unix()
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<!-- Worker/Dispatcher functions -->
<func name="ProcessServer" var="req string" return="(result bool)">
LoadConfig(&Config)
server,index := FindServer(req)
Process(server,index)
return true
</func>
<func name="UpdateServer" var="req Server" return="(result bool)">
ShouldLock()
_,index := FindServer(req.ID)
Config.Servers[index].Uptime = req.Uptime
ShouldUnlock()
SaveConfig(&Config)
return true
</func>
<func name="RegisterServer" var="req string" return="(result bool)">
RegisterWorker(req)
return true
</func>
<func name="UpdateKubernetes" var="req k8sConfig" return="(result bool)">
ShouldLock()
Config.KubeSettings = req
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<func name="AddPod" var="req PodConfig" return="( watching []PodConfig)">
ShouldLock()
Config.KubeSettings.Monitoring = append(Config.KubeSettings.Monitoring, req)
watching = Config.KubeSettings.Monitoring
ShouldUnlock()
SaveConfig(&Config);
return
</func>
<func name="UpdatePod" var="req PodConfig" return="(result bool)">
ShouldLock()
for index,target := range Config.KubeSettings.Monitoring {
if target.Name == req.Name {
Config.KubeSettings.Monitoring[index] = req
}
}
ShouldUnlock()
SaveConfig(&Config);
return true
</func>
<func name="GetPods" var="" return="(result []Pod)">
ClearHistory()
list,_ := RequestPods()
return list.Items
</func>
</methods>
<templates>
</templates>
<endpoints>
<end path="/" type="f" >
if strings.Contains(r.URL.Path, ".map") || strings.Contains(r.URL.Path, "web/{{ server.Image }}") {
return true
}
</end>
<!-- Deploy endpoints as functions by updating
this configuration file's deploy tag from 'webapp' to 'faas'.
Find more information about gos FaaS deployments here :
http://gophersauce.com/docs/markup.html -->
<end path="/update/server" type="POST" >
decoder := json.NewDecoder(r.Body)
var tmvv PayloadOfRequest
err := decoder.Decode(&tmvv)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("{\"error\":\"%s\"}", err.Error())))
return true
}
_ = NetProcessServer(tmvv.req)
w.Header().Set("Content-Type", "text/plain")
w.Write(OK)
</end>
<end path="/mega" type="POST" >
Cfg := &MegaConfig{}
LoadConfig(&Config)
w.Header().Set("Content-Type", "application/json")
retjson := []byte(mResponse(Cfg))
w.Write(retjson)
retjson = nil
</end>
<!-- End Faas functions -->
</endpoints>
</gos>