-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
223 lines (196 loc) · 5.44 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
223
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path"
"path/filepath"
"github.com/gin-gonic/gin"
)
// type Config struct {
// Path string `json:"path"`
// Source string `json:"source"`
// Template string `json:"template"`
// Arguments []string `json:"arguments"`
// }
var (
port uint64
root string
mode string
// configFilePath string
// configMap map[string]Config
router *gin.Engine
)
func init() {
// configMap = make(map[string]Config)
gin.SetMode(gin.ReleaseMode)
router = gin.Default()
}
func main() {
flag.StringVar(&root, "root", "", "Absolute path for root directory")
flag.Uint64Var(&port, "port", 80, "Port, default is 80")
flag.StringVar(&mode, "mode", "", "Mode: fs, spa, upload. Default fs mode")
// flag.StringVar(&configFilePath, "config", "", "Config file path")
flag.Parse()
if len(root) < 1 {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
root = dir
}
if port > 1<<16-1 {
log.Fatal("Port number too large")
}
if len(mode) < 1 {
_, err := os.Open(filepath.Join(root, "index.html"))
if err != nil {
mode = "fs"
} else {
mode = "spa"
}
}
// if len(configFilePath) > 0 {
// raw, err := ioutil.ReadFile(configFilePath)
// if err != nil {
// fmt.Println("Config file not found. No configuration is loaded.")
// }
// var configArr []Config
// if err := json.Unmarshal(raw, &configArr); err != nil {
// fmt.Println("Cannot parse config file. No configuration is loaded.")
// }
// for _, config := range configArr {
// configPath := config.Path
// configMap[configPath] = config
// router.POST(configPath, handlerCreator(configPath))
// }
// }
switch mode {
case "spa":
router.GET("/*page", func(c *gin.Context) {
urlPath := c.Request.URL.Path
fullPath := filepath.FromSlash(path.Join(root, urlPath))
rel, _ := filepath.Rel(root, fullPath)
if len(rel) > 0 && rel != "." {
fileInfo, err := os.Stat(fullPath)
if !os.IsNotExist(err) && fileInfo.Mode().IsRegular() {
c.File(fullPath)
return
}
}
c.File(path.Join(root, "index.html"))
})
case "upload":
const tpl = `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple file upload</title>
<link rel="stylesheet" href="https://unpkg.com/purecss">
</head>
<body>
<div class="pure-u-1">
<form class="pure-form pure-form-aligned" action="/upload" method="post" enctype="multipart/form-data">
<fieldset>
<div class="pure-control-group">
<label for="name">Files</label>
<input type="file" name="files" multiple>
</div>
<div class="pure-control-group">
<label for="name">Image</label>
<input type="file" name="files" multiple accept="image/*" capture>
</div>
<div class="pure-control-group">
<label for="name">Video</label>
<input type="file" name="files" multiple accept="video/*" capture>
</div>
<div class="pure-control-group">
<label for="name">Audio</label>
<input type="file" name="files" multiple accept="audio/*" capture>
</div>
<div class="pure-controls">
<input class="pure-button pure-button-primary" type="submit" value="Submit">
</div>
</fieldset>
</form>
</div>
</body>
</html>`
router.SetHTMLTemplate(template.Must(template.New("index").Parse(tpl)))
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index", gin.H{})
})
router.POST("/upload", func(c *gin.Context) {
// Multipart form
form, _ := c.MultipartForm()
files := form.File["files"]
for _, file := range files {
filename := filepath.Base(file.Filename)
log.Println(filename)
if err := c.SaveUploadedFile(file, filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
}
c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
})
case "fs":
router.StaticFS("/", gin.Dir(root, true))
default:
log.Fatalf("%s mode is not supported\n", mode)
}
log.Println(fmt.Sprintf("Listening on %d, serving %s, in %s mode", port, root, mode))
err := router.Run(fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
}
}
// func handlerCreator(key string) func(c *gin.Context) {
// return func(c *gin.Context) {
// value, ok := configMap[key]
// if !ok {
// c.AbortWithStatus(404)
// } else {
// source := value.Source
// template := value.Template
// arguments := value.Arguments
// doc, err := jsonquery.LoadURL(source)
// if err != nil {
// c.AbortWithStatus(404)
// }
// if len(template) > 1 {
// argumentsLen := len(arguments)
// if argumentsLen > 0 {
// tmpl, err := textTemplate.New("template").Parse(template)
// if err != nil {
// c.AbortWithStatus(404)
// return
// }
// buf := new(bytes.Buffer)
// queries := make([]string, argumentsLen)
// for i, argument := range arguments {
// nodeNameNode := jsonquery.FindOne(doc, argument)
// if nodeNameNode != nil {
// queries[i] = nodeNameNode.InnerText()
// } else {
// queries[i] = ""
// }
// }
// err = tmpl.Execute(buf, queries)
// if err != nil {
// c.AbortWithStatus(404)
// return
// }
// c.JSON(200, buf.String())
// }
// } else {
// c.JSON(200, source)
// }
// }
// }
// }