/
server.go
100 lines (82 loc) · 1.77 KB
/
server.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
package server
import (
"io/ioutil"
"github.com/gin-gonic/gin"
"github.com/shima-park/lotus/common/log"
"github.com/shima-park/lotus/common/plugin"
"github.com/shima-park/lotus/pipeline"
"gopkg.in/yaml.v2"
)
type Server struct {
options Options
metadata Metadata
engine *gin.Engine
pipelineManager pipeline.PipelinerManager
}
func New(opts ...Option) (*Server, error) {
c := &Server{
options: defaultOptions,
engine: gin.Default(),
pipelineManager: pipeline.NewPipelinerManager(),
}
for _, opt := range opts {
opt(&c.options)
}
return c, c.init()
}
func (c *Server) init() error {
var err error
c.metadata, err = NewMetadata(c.options.MetadataPath)
if err != nil {
return err
}
for _, path := range c.metadata.ListPaths(FileTypePlugin) {
err := plugin.LoadPlugins(path)
if err != nil {
return err
}
}
for _, path := range c.metadata.ListPaths(FileTypePipelineConfig) {
err := loadPipelineFromFile(path, c.pipelineManager)
if err != nil {
return err
}
}
return nil
}
func loadPipelineFromFile(path string, pm pipeline.PipelinerManager) error {
log.Info("loading pipeline from: %s", path)
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var conf pipeline.Config
err = yaml.Unmarshal(content, &conf)
if err != nil {
return err
}
_, err = pm.AddPipeline(conf)
if err != nil {
return err
}
return nil
}
func (c *Server) Serve() error {
for _, p := range c.pipelineManager.List() {
if p.GetConfig().Bootstrap {
if err := p.Start(); err != nil {
return err
}
}
}
if c.options.HTTPAddr != "" {
c.setRouter()
return c.engine.Run(c.options.HTTPAddr)
}
return nil
}
func (c *Server) Stop() {
for _, p := range c.pipelineManager.List() {
p.Stop()
}
}