-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.83 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
package main
import (
"flag"
"os"
"syscall"
"os/signal"
"net/http"
"log"
"context"
"time"
"tam/account"
"io/ioutil"
)
var (
accountJsonFilePath string
accountKey string
listen string
accounts *account.Accounts
accountTimeout time.Duration
)
func init() {
log.SetFlags(0)
log.SetOutput(new(logWriter))
flag.StringVar(&accountJsonFilePath, "accounts", "accounts.json", "The account json file path")
flag.StringVar(&accountKey, "key", "username", "The key attribute name of accounts structure")
flag.StringVar(&listen, "listen", "localhost:6666", "The host and port that listen to")
flag.DurationVar(&accountTimeout, "timeout", 300*time.Second, "account timeout in time.Duration format, e.g. 300s or 500ms")
flag.Parse()
log.SetFlags(0)
log.Printf("accounts: %s\n", accountJsonFilePath)
log.Printf("key: %s\n", accountKey)
log.Printf("timeout: %s\n", accountTimeout)
loadAccounts()
}
func loadAccounts() {
data, err := ioutil.ReadFile(accountJsonFilePath)
if err != nil {
log.Fatal(err)
}
accounts, err = account.BuildAccounts(accountKey, accountTimeout, data)
if err != nil {
log.Fatal(err)
}
}
func main() {
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
gracefulPeriod := 10 * time.Second
server := &http.Server{
Addr: listen,
Handler: mux(),
}
e := make(chan error)
go func() {
log.Printf("start server at %s...\n", listen)
e <- server.ListenAndServe()
}()
select {
case err := <-e:
log.Fatalf("[-] [-] [INIT] [-] [-] [-] [-] [-] [-] [%v]\n", err)
case <-stop:
}
log.Printf("[-] [%s] [SHUTTING_DOWN] [-] [-] [-] [-] [-] [-] [-]\n", gracefulPeriod)
ctx, cancel := context.WithTimeout(context.Background(), gracefulPeriod)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("[-] [-] [SHUTDOWN_FAILURE] [-] [-] [-] [-] [-] [-] [%v]\n", err)
}
}