-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (106 loc) · 2.9 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
package main
import (
"encoding/json"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/basicauth"
"github.com/urfave/cli/v2"
)
func runServer(c *cli.Context) error {
gp, gpErr := newGeoParser(c.String("mmdb-city-path"), c.String("mmdb-asn-path"))
if gpErr != nil {
return gpErr
}
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Prefork: false,
UnescapePath: true,
CaseSensitive: true,
StrictRouting: true,
BodyLimit: c.Int("body-limit-size"),
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
ctx.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
ctx.Status(code).SendString(err.Error())
return nil
},
})
if c.String("basic-auth-username") != "" && c.String("basic-auth-password") != "" {
app.Use(basicauth.New(basicauth.Config{
Users: map[string]string{
c.String("basic-auth-username"): c.String("basic-auth-password"),
},
}))
}
app.Get("/info/:ip", func(c *fiber.Ctx) error {
gr := gp.newResultFromIP(c.Params("ip"))
jB, _ := json.Marshal(gr)
c.Type("application/json", "utf8")
return c.Send(jB)
})
return app.Listen(c.String("listen"))
}
func main() {
app := cli.NewApp()
app.Usage = "IP information rest api"
app.EnableBashCompletion = true
app.Commands = []*cli.Command{
{
Name: "run",
Usage: "Run server",
Action: runServer,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Usage: "Application listen http ip:port address",
Value: "0.0.0.0:4000",
Required: false,
EnvVars: []string{"ASM_IP_INFO_LISTEN_ADDRESS"},
},
&cli.IntFlag{
Name: "body-limit-size",
Usage: "Request limit size",
Value: 1 * 1024 * 1024,
Required: false,
EnvVars: []string{"ASM_IP_INFO_BODY_LIMIT_SIZE"},
},
&cli.StringFlag{
Name: "basic-auth-username",
Usage: "Basic authentication username",
Value: "",
Required: false,
EnvVars: []string{"ASM_IP_INFO_BASIC_AUTH_USERNAME"},
},
&cli.StringFlag{
Name: "basic-auth-password",
Usage: "Basic authentication password",
Value: "",
Required: false,
EnvVars: []string{"ASM_IP_INFO_BASIC_AUTH_PASSWORD"},
},
&cli.StringFlag{
Name: "mmdb-city-path",
Usage: "MMDB city database path",
Value: "/tmp/GeoLite2-City.mmdb",
Required: false,
EnvVars: []string{"ASM_IP_INFO_COLLECTOR_MMDB_CITY_PATH"},
},
&cli.StringFlag{
Name: "mmdb-asn-path",
Usage: "MMDB asn database path",
Value: "/tmp/GeoLite2-ASN.mmdb",
Required: false,
EnvVars: []string{"ASM_IP_INFO_COLLECTOR_MMDB_ASN_PATH"},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}