-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (57 loc) · 1.32 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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
"golang.org/x/sync/errgroup"
)
// Options of the tool
type Options struct {
ListenAddress string
Folder string
}
func main() {
options := ParseOptions()
app := fiber.New()
app.Use(logger.New())
registerRoutes(app, options)
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
return app.Listen(options.ListenAddress)
})
// Wait for shutdown signal
g.Go(func() error {
<-ctx.Done()
return app.Shutdown()
})
if err := g.Wait(); err != nil {
fmt.Println("error:", err)
}
}
func ParseOptions() *Options {
options := &Options{}
flag.StringVar(&options.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port")
currentPath := "."
if p, err := os.Getwd(); err == nil {
currentPath = p
}
flag.StringVar(&options.Folder, "path", currentPath, "Folder")
flag.Parse()
return options
}
func registerRoutes(app *fiber.App, options *Options) {
// Root route
app.Static("/", options.Folder, fiber.Static{Browse: true})
app.Get("/ping", func(c fiber.Ctx) error {
return c.SendString("pong")
})
app.Get("/health", func(c fiber.Ctx) error {
return c.SendString("OK")
})
app.Get("/version", func(c fiber.Ctx) error {
return c.SendString("v0.0.1")
})
}