-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (71 loc) · 2.06 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
package main
import (
"flag"
"fmt"
"go-tools-api/m/controllers"
"io"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
func main() {
host := flag.String("host", "localhost", "liscen host")
port := flag.Int("port", 8085, "liscen port")
imageDir := flag.String("image", "./images", "image folder(jpg type)")
jsonValidityDays := flag.Int("jsonValidityDays", 31, "json validity days")
jsonMaxMemorySize := flag.Int64("jsonMaxMemorySize", 32, "json max memory size")
help := flag.Bool("h", false, "show help")
flag.Parse()
if *help {
flag.Usage()
return
}
fmt.Printf("Host: %s\n", *host)
fmt.Printf("Port: %d\n", *port)
fmt.Printf("Image Directory: %s\n", *imageDir)
router := gin.Default()
// Logging to a file.
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f)
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// By default gin.DefaultWriter = os.Stdout
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// your custom format
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
router.Use(gin.Recovery())
var files []string
err := filepath.Walk(*imageDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil || len(files) < 2 {
log.Fatal(err)
}
controllers.ImageList = files
controllers.MaxMemory = *jsonMaxMemorySize << 20
controllers.JsonValidityDays = *jsonValidityDays
// Routes
router.GET("/rest/api/v1/ip", controllers.IP)
router.GET("/rest/api/v1/image", controllers.Image)
router.GET("/rest/api/v1/anyjson", controllers.AnyJsonWithGet)
router.POST("/rest/api/v1/anyjson", controllers.AnyJsonWithPost)
// Start server
router.Run(*host + ":" + strconv.Itoa(*port))
// r.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", host, port)))
}