This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
222 lines (193 loc) · 4.73 KB
/
main.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
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
// use martini as web framework
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/render"
"github.com/codeskyblue/go-websocket"
"github.com/codeskyblue/gobuild/database"
"github.com/codeskyblue/gobuild/utils"
"github.com/codeskyblue/goyaml"
"github.com/qiniu/api.v6/conf"
"github.com/qiniu/log"
)
var (
mu = &sync.Mutex{}
broadcasts = make(map[string]*utils.WriteBroadcaster)
totalUser = 0
//log = klog.DevLog
//OS = []string{"windows", "linux", "darwin"}
Arch = []string{"386", "amd64"}
)
type StreamOutput struct {
BufferStr string
Reader io.ReadCloser
job *Builder
}
func (p *StreamOutput) Close() {
p.Reader.Close()
}
func GetWriteBroadcaster(project string) (wc *utils.WriteBroadcaster, newer bool) {
mu.Lock()
defer mu.Unlock()
if wc = broadcasts[project]; wc == nil {
wc = utils.NewWriteBroadcaster()
broadcasts[project] = wc
newer = true
}
return
}
func NewStreamOutput(project, branch, goos, goarch string) *StreamOutput {
fullname := strings.Join([]string{project, branch, goos, goarch}, "-")
wb, newer := GetWriteBroadcaster(fullname)
if newer {
go func() {
// start compiling job
_, err := NewBuilder(project, branch, goos, goarch, wb).Auto()
if err != nil {
log.Error(err)
}
mu.Lock()
defer mu.Unlock()
delete(broadcasts, fullname)
log.Info("delete broadcasts", project, broadcasts)
}()
}
bufbytes, rd := wb.NewReader("")
reader := utils.NewBufReader(rd)
return &StreamOutput{
BufferStr: string(bufbytes),
Reader: reader,
}
}
type SendMsg struct {
Error error `json:"error"`
Type string `json:"type"` // FIXME: how to omited
Data string `json:"data"`
}
type RecvMsg struct {
Project string `json:"project"`
Branch string `json:"branch"`
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
}
// output websocket
func WsBuildServer(ws *websocket.Conn) {
defer ws.Close()
var err error
recvMsg := new(RecvMsg)
sendMsg := new(SendMsg)
err = websocket.JSON.Receive(ws, &recvMsg)
if err != nil {
sendMsg.Error = err
websocket.JSON.Send(ws, sendMsg)
utils.Debugf("read json error: %v", err)
return
}
name := ws.RemoteAddr().String()
log.Debug(name)
sout := NewStreamOutput(recvMsg.Project, recvMsg.Branch, recvMsg.GOOS, recvMsg.GOARCH)
defer sout.Close()
sendMsg.Data = sout.BufferStr
err = websocket.JSON.Send(ws, sendMsg)
if err != nil {
utils.Debugf("send first sendMsg error: %v", err)
return
}
// send the rest outputs
buf := make([]byte, 100)
for {
n, err := sout.Reader.Read(buf)
if n > 0 {
sendMsg.Data = string(buf[:n])
deadline := time.Now().Add(time.Second * 1)
ws.SetWriteDeadline(deadline)
if er := websocket.JSON.Send(ws, sendMsg); er != nil {
log.Debug("write failed timeout, user logout")
return
}
}
if err != nil {
return
}
}
}
type Configuration struct {
Hostname string `yaml:"hostname"`
ListenAddr string `yaml:"listen"`
GOROOT string `yaml:"goroot"`
Driver string `yaml:"driver"`
DataSource string `yaml:"data_source"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
}
type config struct {
Development Configuration `yaml:"development"`
Production Configuration `yaml:"production"`
}
var (
m = martini.Classic()
environment = flag.String("e", "development", "select environment <development|production>")
secure = flag.Bool("secure", false, "use secure connection")
opts *Configuration
)
func init() {
cfg := new(config)
in, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Fatal(err)
}
err = goyaml.Unmarshal(in, cfg)
if err != nil {
log.Fatal(err)
}
flag.Parse()
log.SetOutputLevel(log.Ldebug)
if *environment == "development" {
opts = &cfg.Development
} else {
opts = &cfg.Production
}
fmt.Println("== environment:", *environment, "==")
utils.Dump(opts)
conf.ACCESS_KEY = opts.AccessKey
conf.SECRET_KEY = opts.SecretKey
// render html templates from templates directory
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
InitRouter()
}
func HelloServer(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, world!\n")
}
func main() {
var err error
err = database.InitDB(opts.Driver, opts.DataSource)
if err != nil {
log.Fatal(err)
}
log.Info("gobuild service stated ...")
http.Handle("/", m)
http.Handle("/websocket/", websocket.Handler(WsBuildServer))
http.HandleFunc("/hello", HelloServer)
if *secure {
go func() {
er := http.ListenAndServeTLS(":443", "bin/ssl.crt", "bin/ssl.key", nil)
if er != nil {
log.Error(er)
}
}()
}
err = http.ListenAndServe(opts.ListenAddr, nil)
if err != nil {
log.Fatal(err)
}
}