-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (78 loc) · 1.9 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
"github.com/wengchaoxi/auth-proxy/pkg/token"
"github.com/wengchaoxi/auth-proxy/service"
)
var (
HOST = getEnv("HOST", "0.0.0.0")
PORT = getEnv("PORT", "18000")
TARGET_URL = getEnv("TARGET_URL", "https://github.com/wengchaoxi/auth-proxy")
ACCESS_KEY = getEnv("AUTH_ACCESS_KEY", "whoami")
AUTH_EXPIRATION = getEnv("AUTH_EXPIRATION", "24h")
authExpiration = func() time.Duration {
if d, err := time.ParseDuration(AUTH_EXPIRATION); err == nil {
return d
}
return 24 * time.Hour
}()
)
func getEnv(key, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}
func generateSecretKey(x string) string {
size := len(x)
if size > 0 {
for {
if len(x) >= 32 {
return x[:32]
}
x += x
}
}
return "passphrasewhichneedstobe32bytes!"
}
func main() {
app := service.New(&service.ServiceOptions{
TargetURL: TARGET_URL,
AccessKey: ACCESS_KEY,
AuthExpiration: authExpiration,
TokenManager: token.NewTokenManager(generateSecretKey(ACCESS_KEY)),
})
engine := gin.Default()
app.Init(engine)
var err error
var wg sync.WaitGroup
addr := HOST + ":" + PORT
srv := &http.Server{Addr: addr, Handler: engine}
wg.Add(1)
go func() {
defer wg.Done()
if err = srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Panicf("server err: %v\n", err)
os.Exit(-1)
}
}()
log.Printf("Running on http://%s, Proxy to %s\n", addr, TARGET_URL)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("server shutdown err: %v\n", err)
}
wg.Wait()
}