forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
51 lines (44 loc) · 1.04 KB
/
service.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
package gou
import (
"io"
"os"
"strings"
"github.com/yaoapp/gou/helper"
"github.com/yaoapp/gou/service"
"github.com/yaoapp/kun/exception"
)
// Services services loaded (Alpha)
var Services = map[string]*service.Service{}
// LoadService load socket server/client
func LoadService(source string, name string) (*service.Service, error) {
var input io.Reader = nil
if strings.HasPrefix(source, "file://") {
filename := strings.TrimPrefix(source, "file://")
file, err := os.Open(filename)
if err != nil {
exception.Err(err, 400).Throw()
}
defer file.Close()
input = file
} else {
input = strings.NewReader(source)
}
config, err := helper.ReadFile(input)
if err != nil {
return nil, err
}
service, err := service.Load(name, config)
if err != nil {
return nil, err
}
Services[name] = service
return service, nil
}
// SelectService Get socket by name
func SelectService(name string) *service.Service {
service, has := Services[name]
if !has {
exception.New("Service:%s does not load", 500, name).Throw()
}
return service
}