-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
73 lines (59 loc) · 1.26 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
package main
import (
"bytes"
"flag"
"image"
"image/color"
"image/gif"
"github.com/gin-gonic/gin"
"1px/log"
)
var (
logFile = flag.String("log", "./1px.log", "log file path")
listenHost = flag.String("host", "0.0.0.0", "listen host")
listenPort = flag.String("port", "9982", "listen port")
help = flag.Bool("h", false, "print help info")
)
func main() {
flag.Parse()
if *help {
flag.Usage()
return
}
setupLogger()
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
img, err := gen1pxGif()
if err != nil {
panic("fail to gen 1px gif " + err.Error())
}
r.Any("/1px/gif", func(ctx *gin.Context) {
ctx.Data(200, "image/gif", img)
})
err = r.Run(*listenHost + ":" + *listenPort)
if err != nil {
panic("fail to run 1px service " + err.Error())
}
}
func gen1pxGif() ([]byte, error) {
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
img.Set(0, 0, color.RGBA{R: 255, A: 255})
var buf bytes.Buffer
err := gif.Encode(&buf, img, nil)
if err != nil {
return nil ,err
}
return buf.Bytes(), nil
}
func setupLogger() {
opt := log.Options{
Stdout: false,
ConsoleMode: false,
Filename: *logFile,
MaxSize: 500, // 500MB
MaxAge: 30, // 30 days
MaxBackups: 20, // 20 logs
Level: log.InfoLevel,
}
log.SetOptions(opt)
}