forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudio.go
148 lines (121 loc) · 2.84 KB
/
studio.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package studio
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/gin-gonic/gin"
"github.com/yaoapp/gou"
"github.com/yaoapp/gou/fs"
"github.com/yaoapp/gou/fs/dsl"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share"
)
var shutdownSignal = make(chan bool, 1)
var dfs fs.FileSystem
var scripts = map[string][]byte{}
type cfunc struct {
Method string `json:"method"`
Args []interface{} `json:"args,omitempty"`
}
// Start start the studio api server
func Start(cfg config.Config) (err error) {
// recive interrupt signal
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
errCh := make(chan error, 1)
// Set router
router := gin.New()
setRouter(router)
// Server setting
addr := fmt.Sprintf("%s:%d", cfg.Host, cfg.Studio.Port)
srv := &http.Server{
Addr: addr,
Handler: router,
}
// Listen
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer func() {
log.Info("[Studio] %s Close Serve", addr)
err = srv.Close()
if err != nil {
log.Error("[Studio] Close Serve Error (%v)", err)
}
}()
// start serve
go func() {
log.Info("[Studio] Starting: %s", addr)
if err := srv.Serve(l); err != nil && err != http.ErrServerClosed {
errCh <- err
}
}()
select {
case <-shutdownSignal:
log.Info("[Studio] %s Exit (Manual)", addr)
return err
case <-interrupt:
log.Info("[Studio] %s Exit (Interrupt) ", addr)
return err
case err := <-errCh:
log.Error("[Studio] %s Error (%v)", addr, err)
return err
}
}
// Stop stop the studio api server
func Stop() {
shutdownSignal <- true
}
// Load studio config
func Load(cfg config.Config) error {
err := loadDSL(cfg)
if err != nil {
return err
}
return loadScripts(cfg)
}
func loadDSL(cfg config.Config) error {
root, err := filepath.Abs(cfg.Root)
if err != nil {
return err
}
scriptRoot := filepath.Join(root, "scripts")
dataRoot := filepath.Join(root, "data")
dslDenyList := []string{scriptRoot, dataRoot}
dfs = dsl.New(root).DenyAbs(dslDenyList...)
return nil
}
func loadScripts(cfg config.Config) error {
root, err := filepath.Abs(cfg.Root)
if err != nil {
return err
}
studioRoot := filepath.Join(root, "studio")
return loadScriptFrom(studioRoot)
}
// Load script From dir
func loadScriptFrom(dir string) error {
if share.DirNotExists(dir) {
log.Warn("[Studio] Load %s does not exists", dir)
return nil
}
messages := []string{}
err := share.Walk(dir, ".js", func(root, filename string) {
name := fmt.Sprintf("__yao.studio.%s", share.SpecName(root, filename))
err := gou.Yao.Load(filename, name)
if err != nil {
messages = append(messages, err.Error())
}
})
if len(messages) > 0 {
return fmt.Errorf("[Studio] Load %s", strings.Join(messages, ";"))
}
return err
}