-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (177 loc) · 4.11 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/urfave/cli/v2"
)
var sv *fiber.App
func main() {
port, list, cert, key := "", "", "", ""
index, spa, api, tls := false, false, false, false
wg := &sync.WaitGroup{}
app := &cli.App{
Name: "static-server",
Usage: "Start http server with static files!",
ArgsUsage: "Path of static files (default: \".\")",
Version: "v1.0.3",
Commands: nil,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Value: "8000",
Destination: &port,
Aliases: []string{"p"},
Usage: "listening port of http server",
},
&cli.BoolFlag{
Name: "index",
Destination: &index,
Aliases: []string{"i"},
Usage: "allow index all directory",
},
&cli.BoolFlag{
Name: "spa",
Destination: &spa,
Aliases: []string{"s"},
Usage: "support SPA mode, this option will be ignored when allowing index",
},
&cli.BoolFlag{
Name: "api",
Destination: &api,
Aliases: []string{"a"},
Usage: "enable proxy for /api, combine with --proxy to config",
},
&cli.StringFlag{
Name: "proxy",
Value: "http://localhost:3000",
Destination: &list,
Aliases: []string{"x"},
Usage: "list of proxy for /api, separated by comma",
},
&cli.BoolFlag{
Name: "tls",
Destination: &tls,
Aliases: []string{"t"},
Usage: "enable tls mode, combine with --cert and --key",
},
&cli.StringFlag{
Name: "cert",
Value: "cert.pem",
Destination: &cert,
Aliases: []string{"c"},
Usage: "path to tls cert",
},
&cli.StringFlag{
Name: "key",
Value: "key.pem",
Destination: &key,
Aliases: []string{"k"},
Usage: "path to private key",
},
},
Action: func(c *cli.Context) error {
wg.Add(1)
colorReset := "\033[0m"
colorRed := "\033[31m"
colorBlue := "\033[36m"
proto := "http"
if tls {
proto = "https"
}
fmt.Println("Server is running at:", colorBlue+proto+"://localhost:"+port+colorReset)
dir := c.Args().Get(0)
if err := startServer(dir, ":"+port, index, spa, api, list, tls, cert, key); err != nil {
fmt.Println(colorRed + err.Error() + colorReset)
return err
}
return nil
},
}
go func() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
if err := stopServer(ctx); err != nil {
if err.Error() == "timeout" {
fmt.Println("Server stopped")
} else {
fmt.Println(err.Error())
}
}
cancel()
wg.Done()
}()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
wg.Wait()
}
func startServer(dir, port string, index bool, spa bool, api bool, list string, tlsMode bool, cert, key string) error {
path, err := os.Getwd()
if err != nil {
return err
}
if dir != "." && dir != "" {
path = dir
}
sv = fiber.New(fiber.Config{
Prefork: false,
DisableStartupMessage: true,
ReduceMemoryUsage: true,
})
if api {
sv.Group("/api", proxy.Balancer(proxy.Config{
Servers: strings.Split(list, ","),
}))
}
sv.Static("/", path, fiber.Static{
Browse: index,
})
if !index && spa {
sv.Get("*", func(ctx *fiber.Ctx) error {
if strings.Contains(string(ctx.Request().URI().Path()), ".") {
return ctx.SendStatus(404)
}
f, e := os.Open(path + "/index.html")
if e != nil {
return ctx.SendStatus(404)
}
defer f.Close()
ctx.Set("Content-Type", "text/html")
return ctx.SendStream(f)
})
}
if tlsMode {
return sv.ListenTLS(port, cert, key)
}
return sv.Listen(port)
}
func stopServer(ctx context.Context) error {
stop := make(chan struct{}, 1)
go func(c chan struct{}) {
if sv != nil {
_ = sv.Shutdown()
}
c <- struct{}{}
}(stop)
fmt.Println("\nStopping http server...")
for {
select {
case <-ctx.Done():
return errors.New("timeout")
case <-stop:
return nil
}
}
}