forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
130 lines (112 loc) · 3.04 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
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
// Copyright (C) 2019-2020 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/algorand/websocket"
"github.com/gorilla/mux"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 20480,
WriteBufferSize: 20480,
CheckOrigin: func(r *http.Request) bool {
if len(r.Header.Get("Origin")) == 0 {
return true
}
if strings.HasPrefix(r.Header.Get("Origin"), "devtools://") {
return true
}
if strings.HasPrefix(r.Header.Get("Origin"), "http://localhost") {
return true
}
return false
},
}
// DebugServer is Debugger + HTTP/WS handlers for frontends
type DebugServer struct {
debugger *Debugger
frontend DebugAdapter
router *mux.Router
server *http.Server
remote *RemoteHookAdapter
params *DebugParams
}
// DebugParams is a container for debug parameters
type DebugParams struct {
ProgramNames []string
ProgramBlobs [][]byte
Proto string
TxnBlob []byte
GroupIndex int
BalanceBlob []byte
DdrBlob []byte
Round uint64
LatestTimestamp int64
RunMode string
DisableSourceMap bool
AppID uint64
Painless bool
}
// FrontendFactory interface for attaching debug frontends
type FrontendFactory interface {
Make(router *mux.Router, appAddress string) (da DebugAdapter)
}
func makeDebugServer(port int, ff FrontendFactory, dp *DebugParams) DebugServer {
debugger := MakeDebugger()
router := mux.NewRouter()
appAddress := fmt.Sprintf("localhost:%d", port)
da := ff.Make(router, appAddress)
debugger.AddAdapter(da)
server := &http.Server{
Handler: router,
Addr: appAddress,
WriteTimeout: time.Duration(0),
ReadTimeout: time.Duration(0),
}
return DebugServer{
debugger: debugger,
frontend: da,
router: router,
server: server,
params: dp,
}
}
func (ds *DebugServer) startRemote() {
remote := MakeRemoteHook(ds.debugger)
remote.Setup(ds.router)
ds.remote = remote
log.Printf("starting server on %s", ds.server.Addr)
ds.server.ListenAndServe()
}
func (ds *DebugServer) startDebug() (err error) {
local := MakeLocalRunner(ds.debugger)
if err = local.Setup(ds.params); err != nil {
return
}
go ds.server.ListenAndServe()
defer ds.server.Shutdown(context.Background())
err = local.RunAll()
if err != nil {
return
}
ds.frontend.WaitForCompletion()
return
}