-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (47 loc) · 1.16 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
package main
import (
"context"
"deadfly/pkg/api"
"log"
"net/http"
"os"
"sync"
"github.com/caarlos0/env/v6"
)
type config struct {
Production bool `env:"PRODUCTION" envDefault:"false"`
EnableSleepMode bool `env:"ENABLE_SLEEP_MODE" envDefault:"true"`
SleepAfterSeconds int `env:"SLEEP_AFTER_SECONDS" envDefault:"10"`
ProxyURL string `env:"PROXY_URLS" envDefault:"http://localhost:8000"`
ListenAddr string `env:"LISTEN_ADDR" envDefault:"0.0.0.0:4466"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(1)
config := &config{}
if err := env.Parse(config); err != nil {
log.Fatalln("parse env", err)
}
handler := api.NewHandler(config.EnableSleepMode,
config.Production,
config.SleepAfterSeconds,
config.ProxyURL,
cancel)
srv := http.Server{
Addr: config.ListenAddr,
Handler: handler,
}
log.Printf("Server Listening on: http://%s", config.ListenAddr)
go func() {
err := srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalln("listen and serve", err)
}
}()
<-ctx.Done()
wg.Done()
wg.Wait()
os.Exit(0)
}