-
Notifications
You must be signed in to change notification settings - Fork 251
/
helper.go
332 lines (282 loc) · 8.5 KB
/
helper.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2022 AndeyaLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package faygo
import (
"fmt"
"io/ioutil"
"mime"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/andeya/ini"
"github.com/andeya/goutil"
"github.com/andeya/goutil/errors"
)
// JoinStatic adds the static directory prefix to the file name.
func JoinStatic(shortFilename string) string {
return path.Join(StaticDir(), shortFilename)
}
// SyncINI quickly create your own configuration files.
// Struct tags reference `https://github.com/go-ini/ini`
func SyncINI(structPtr interface{}, f func(onecUpdateFunc func() error) error, filename ...string) error {
t := reflect.TypeOf(structPtr)
if t.Kind() != reflect.Ptr {
return errors.New("SyncINI's param must be struct pointer type.")
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return errors.New("SyncINI's param must be struct pointer type.")
}
var fname string
if len(filename) > 0 {
fname = filename[0]
} else {
fname = strings.TrimSuffix(t.Name(), "Config")
fname = strings.TrimSuffix(fname, "INI")
fname = goutil.SnakeString(fname) + ".ini"
fname = filepath.Join(configDir, fname)
}
return ini.SyncINI(structPtr, f, fname)
}
// RemoveUseless when there's not frame instance, remove files: config, log, static and upload .
func RemoveUseless() {
if len(AllFrames()) > 0 {
return
}
var files []string
filepath.Walk(configDir, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
files = append(files, retpath)
return err
})
confile := filepath.Join(configDir, globalConfigFile)
if len(files) == 1 || len(files) == 2 && files[1] == confile {
os.Remove(confile)
os.Remove(configDir)
os.Remove(LogDir())
os.Remove(StaticDir())
os.Remove(UploadDir())
}
}
/**
* WrapDoc add a document notes to handler
*/
type docWrap struct {
Handler
doc Doc
}
var _ APIDoc = new(docWrap)
func (w *docWrap) Doc() Doc {
return w.doc
}
// WrapDoc adds a note to the handler func.
func WrapDoc(fn HandlerFunc, note string, ret interface{}, params ...ParamInfo) Handler {
return &docWrap{
Handler: fn,
doc: Doc{
Note: note,
Return: ret,
MoreParams: params,
},
}
}
/**
* common utils
*/
// ContentTypeByExtension gets the content type from ext string.
// MIME type is given in mime package.
// It returns `application/octet-stream` incase MIME type is not
// found.
func ContentTypeByExtension(ext string) string {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
ctype := mime.TypeByExtension(ext)
if ctype != "" {
return ctype
}
return MIMEOctetStream
}
// WritePid write pid to the specified file.
func WritePid(pidFilename string) error {
abs, err := filepath.Abs(pidFilename)
if err != nil {
return err
}
dir := filepath.Dir(abs)
os.MkdirAll(dir, 0777)
pid := os.Getpid()
return ioutil.WriteFile(abs, []byte(fmt.Sprintf("%d\n", pid)), 0666)
}
// CleanToURL is the URL version of path.Clean, it returns a canonical URL path
// for p, eliminating . and .. elements.
//
// The following rules are applied iteratively until no further processing can
// be done:
// 1. Replace multiple slashes with a single slash.
// 2. Eliminate each . path name element (the current directory).
// 3. Eliminate each inner .. path name element (the parent directory)
// along with the non-.. element that precedes it.
// 4. Eliminate .. elements that begin a rooted path:
// that is, replace "/.." by "/" at the beginning of a path.
//
// If the result of this process is an empty string, "/" is returned
func CleanToURL(p string) string {
// Turn empty string into "/"
if p == "" {
return "/"
}
n := len(p)
var buf []byte
// Invariants:
// reading from path; r is index of next byte to process.
// writing to buf; w is index of next byte to write.
// path must start with '/'
r := 1
w := 1
if p[0] != '/' {
r = 0
buf = make([]byte, n+1)
buf[0] = '/'
}
trailing := n > 2 && p[n-1] == '/'
// A bit more clunky without a 'lazybuf' like the path package, but the loop
// gets completely inlined (bufApp). So in contrast to the path package this
// loop has no expensive function calls (except 1x make)
for r < n {
switch {
case p[r] == '/':
// empty path element, trailing slash is added after the end
r++
case p[r] == '.' && r+1 == n:
trailing = true
r++
case p[r] == '.' && p[r+1] == '/':
// . element
r++
case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
// .. element: remove to last /
r += 2
if w > 1 {
// can backtrack
w--
if buf == nil {
for w > 1 && p[w] != '/' {
w--
}
} else {
for w > 1 && buf[w] != '/' {
w--
}
}
}
default:
// real path element.
// add slash if needed
if w > 1 {
bufApp(&buf, p, w, '/')
w++
}
// copy element
for r < n && p[r] != '/' {
bufApp(&buf, p, w, p[r])
w++
r++
}
}
}
// re-append trailing slash
if trailing && w > 1 {
bufApp(&buf, p, w, '/')
w++
}
if buf == nil {
return p[:w]
}
return BytesToString(buf[:w])
}
// internal helper to lazily create a buffer if necessary
func bufApp(buf *[]byte, s string, w int, c byte) {
if *buf == nil {
if s[w] == c {
return
}
*buf = make([]byte, len(s))
copy(*buf, s[:w])
}
(*buf)[w] = c
}
// SelfPath gets compiled executable file absolute path.
// func SelfPath() string
var SelfPath = goutil.SelfPath
// SelfDir gets compiled executable file directory
// func SelfDir() string
var SelfDir = goutil.SelfDir
// RelPath gets relative path.
// func RelPath() string
var RelPath = goutil.RelPath
// SelfChdir switch the working path to my own path.
// func SelfChdir()
var SelfChdir = goutil.SelfChdir
// FileExists reports whether the named file or directory exists.
// func FileExists(name string) bool
var FileExists = goutil.FileExists
// SearchFile Search a file in paths.
// this is often used in search config file in /etc ~/
// func SearchFile(filename string, paths ...string) (fullpath string, err error)
var SearchFile = goutil.SearchFile
// GrepFile like command grep -E
// for example: GrepFile(`^hello`, "hello.txt")
// \n is striped while read
// func GrepFile(patten string, filename string) (lines []string, err error)
var GrepFile = goutil.GrepFile
// WalkDirs traverses the directory, return to the relative path.
// You can specify the suffix.
// func WalkDirs(targpath string, suffixes ...string) (dirlist []string)
var WalkDirs = goutil.WalkDirs
// SnakeString converts the accepted string to a snake string (XxYy to xx_yy)
// func SnakeString(s string) string
var SnakeString = goutil.SnakeString
// CamelString converts the accepted string to a camel string (xx_yy to XxYy)
// func CamelString(s string) string
var CamelString = goutil.CamelString
// ObjectName gets the type name of the object
// func ObjectName(i interface{}) string
var ObjectName = goutil.ObjectName
// RandomString returns a URL-safe, base64 encoded securely generated
// random string. It will panic if the system's secure random number generator
// fails to function correctly.
// The length n must be an integer multiple of 4, otherwise the last character will be padded with `=`.
// func RandomString(n int) string
var RandomString = goutil.URLRandomString
// BytesToString convert []byte type to string type.
// func BytesToString(b []byte) string
var BytesToString = goutil.BytesToString
// StringToBytes convert string type to []byte type.
// NOTE: panic if modify the member value of the []byte.
// func StringToBytes(s string) []byte
var StringToBytes = goutil.StringToBytes
// JsQueryEscape escapes the string in javascript standard so it can be safely placed
// inside a URL query.
// func JsQueryEscape(s string) string
var JsQueryEscape = goutil.JsQueryEscape
// JsQueryUnescape does the inverse transformation of JsQueryEscape, converting
// %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if
// any % is not followed by two hexadecimal digits.
// func JsQueryUnescape(s string) (string, error)
var JsQueryUnescape = goutil.JsQueryUnescape