-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
47 lines (40 loc) · 1.21 KB
/
init.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
package nuMicro
import (
"fmt"
"log"
"github.com/Just4Ease/nuMicro/broker"
)
type mountServices func(serviceName string)
/**
* Entry point into nuMicro
* The ```Init``` method takes a service name and the handler for all subscription.
*
* param: string serviceName
* param: mountServices eventsHandler
* param: mountServices actionHandler
*/
func Init(serviceName string, eventsHandler mountServices, actionHandler mountServices, opts ...broker.Option) {
if opts != nil {
if err := broker.Init(opts...); err != nil {
log.Fatal(err, " Please check broker options... Failed to start service.")
}
}
if err := broker.Connect(); err != nil {
log.Fatal(err, " Failed to start broker. Mayday!, Mayday! Call the NATS officer, ensure all is well!")
}
forever := make(chan bool)
if eventsHandler != nil {
go eventsHandler(serviceName)
}
if eventsHandler == nil {
fmt.Println("Event handler has not been set, no events will be listened for.")
}
if actionHandler != nil {
go actionHandler(serviceName)
}
if actionHandler == nil {
fmt.Println("Action handler has not been set, no requests will be responded to.")
}
log.Println(fmt.Sprintf("Running nuMicro as : %s", serviceName))
<-forever
}