-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
178 lines (147 loc) · 3.38 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
. "github.com/gencebay/httplive/lib"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/urfave/cli"
)
func main() {
var ports string
var dbpath string
app := cli.NewApp()
app.Name = "httplive"
app.Usage = "HTTP Request & Response Service, Mock HTTP"
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ports, p",
Value: "5003",
Usage: "Hosting ports can be array with semicolon <5003,5004> to host multiple endpoint. First one is DefaultPort.",
Destination: &ports,
},
cli.StringFlag{
Name: "dbpath, d",
Value: "",
Usage: "Fullpath of the httplive.db with forward slash.",
Destination: &dbpath,
},
}
app.Action = func(c *cli.Context) error {
host(ports, dbpath)
return nil
}
app.Run(os.Args)
}
func createDb(dbPath string, dbPathPresent bool) error {
var err error
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
var filename string = filepath.Base(dbPath)
if filename == DefaultDbName && !dbPathPresent {
_, err := os.Create(dbPath)
if err != nil {
log.Fatal(err)
}
} else {
if err != nil {
log.Fatal(err)
}
}
}
Environments.DatabaseFullPath = dbPath
CreateDbBucket()
return err
}
func host(ports string, dbPath string) {
portsArr := strings.Split(ports, ",")
port := portsArr[0]
length := len(portsArr)
hasMultiplePort := false
if length > 1 {
hasMultiplePort = true
}
workdir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
Environments.WorkingDirectory = filepath.ToSlash(workdir)
Environments.DefaultPort = port
Environments.HasMultiplePort = hasMultiplePort
dbPathPresent := false
if dbPath == "" {
dbPath = path.Join(workdir, DefaultDbName)
} else {
dbPathPresent = true
}
createDb(dbPath, dbPathPresent)
InitDbValues()
r := gin.Default()
r.Use(StaticFileMiddleware())
r.GET("/ws", func(c *gin.Context) {
wshandler(c.Writer, c.Request)
})
r.Use(CORSMiddleware())
r.Use(ConfigJsMiddleware())
webcli := r.Group("/webcli")
{
ctrl := new(WebCliController)
webcli.GET("/api/backup", ctrl.Backup)
webcli.GET("/api/downloadfile", ctrl.DownloadFile)
webcli.GET("/api/tree", ctrl.Tree)
webcli.GET("/api/endpoint", ctrl.Endpoint)
webcli.GET("/api/deleteendpoint", ctrl.DeleteEndpoint)
webcli.POST("/api/save", ctrl.Save)
webcli.POST("/api/saveendpoint", ctrl.SaveEndpoint)
}
r.Use(APIMiddleware())
r.NoRoute(func(c *gin.Context) {
Broadcast(c)
c.Status(404)
c.File("./public/404.html")
})
if hasMultiplePort {
for i := 1; i < length; i++ {
go func(port string) {
r.Run(":" + port)
}(portsArr[i])
}
}
fmt.Printf("Httplive started with port: %s", port)
r.Run(":" + port)
}
var wsupgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func wshandler(w http.ResponseWriter, r *http.Request) {
connID := r.URL.Query().Get("connectionId")
if connID != "" {
conn := Clients[connID]
if conn != nil {
return
}
}
conn, err := wsupgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Printf("Failed to set websocket upgrade: %+v", err)
return
}
Clients[connID] = conn
for {
t, msg, err := conn.ReadMessage()
if err != nil {
delete(Clients, connID)
break
}
conn.WriteMessage(t, msg)
}
}