-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
79 lines (65 loc) · 1.62 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com./joho/godotenv"
"github.com./logrusorgru/aurora"
"github.com./valyala/fasthttp"
)
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err.Error())
}
err = godotenv.Load(cwd + "/.env")
if err != nil {
log.Fatal(err.Error())
}
options, err := getOptions()
if err != nil {
log.Fatal(err.Error())
}
server := getServer(options)
fmt.Print("\n")
fmt.Println(aurora.Black(" Local Cors Proxy Go ").Bold().Underline().BgWhite())
fmt.Print("\n")
fmt.Printf(aurora.Sprintf(aurora.Blue("Proxy url: %v\n"), aurora.Green(options.cleanURL)))
fmt.Printf(aurora.Sprintf(aurora.Blue("Proxy url section: %v\n"), aurora.Green(options.cleanURLSection)))
fmt.Printf(aurora.Sprintf(aurora.Blue("Proxy port: %v\n"), aurora.Green(options.port)))
fmt.Print("\n")
fmt.Printf(
aurora.Sprintf(
aurora.Cyan("To start using the proxy simply replace the proxied section of your url with: %s"),
aurora.Sprintf(
aurora.Yellow("http://%v/%v").Bold(),
options.addr,
options.cleanURLSection,
),
),
)
fmt.Print("\n")
waitingForGracefulShutdown(server)
}
func waitingForGracefulShutdown(server *fasthttp.Server) {
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
interruptReason := <-interruptChan
fmt.Print("\n")
fmt.Println(
aurora.Sprintf(
aurora.Red("Shutting down cause %v..\n"),
interruptReason,
),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
go func() {
server.Shutdown()
defer cancel()
}()
<-ctx.Done()
}