-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
107 lines (86 loc) · 2.03 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
package main
import (
"encoding/json"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/urfave/cli/v2"
)
func runServer(c *cli.Context) error {
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
},
})
app.Post("/info", func(c *fiber.Ctx) error {
var processInfo urlInfoProcess
if err := c.BodyParser(&processInfo); err != nil {
return err
}
cacheID := processInfo.cache()
cacheFile := cachePath(cacheID, "json")
if fileExist(cacheFile) {
return c.SendFile(cacheFile)
}
u, uE := newURL(processInfo.URL)
if uE != nil {
return uE
}
u.process(processInfo)
b, bErr := json.Marshal(u)
if bErr != nil {
return bErr
}
writeErr := os.WriteFile(cacheFile, b, 0666)
if writeErr != nil {
return writeErr
}
c.Type("application/json", "utf8")
return c.Send(b)
})
return app.Listen(c.String("listen"))
}
func main() {
app := cli.NewApp()
app.Usage = "URL Information"
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_URL_INFO_LISTEN_ADDRESS"},
},
&cli.IntFlag{
Name: "body-limit-size",
Usage: "Request limit size",
Value: 2 * 1024 * 1024,
Required: false,
EnvVars: []string{"ASM_URL_INFO_BODY_LIMIT_SIZE"},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}