-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (58 loc) · 1.61 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
package main
import (
"context"
"log"
"net/url"
"time"
"github.com/dosquad/mock-oauth-test-server/internal/mainconfig"
"github.com/dosquad/mock-oauth-test-server/mockghauth"
"github.com/na4ma4/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
)
var rootCmd = &cobra.Command{
Use: "mock-oauth-server",
RunE: mainCommand,
}
func init() {
cobra.OnInitialize(mainconfig.ConfigInit)
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug output")
_ = viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
_ = viper.BindEnv("debug", "DEBUG")
_ = viper.BindEnv("load.code-file")
_ = viper.BindEnv("load.code-file", "LOAD_CODE_FILE")
_ = viper.BindEnv("load.clients-file", "LOAD_CLIENTS_FILE")
_ = viper.BindEnv("load.tokens-file", "LOAD_TOKENS_FILE")
}
func main() {
_ = rootCmd.Execute()
}
func mainCommand(_ *cobra.Command, _ []string) error {
cfg := config.NewViperConfigFromViper(viper.GetViper(), "mock-server")
logger, _ := cfg.ZapConfig().Build()
defer logger.Sync()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
baseURL, baseURLErr := url.Parse("http://" + cfg.GetString("server.bind"))
if baseURLErr != nil {
return baseURLErr
}
svr := mockghauth.NewServer(baseURL, cfg)
svr.AddClient("github-client-id", "github-client-secret")
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
ticker := time.NewTicker(time.Minute)
for {
ts := <-ticker.C
svr.Reaper(ts)
}
})
eg.Go(func() error {
return svr.Run(ctx)
})
if err := eg.Wait(); err != nil {
log.Printf("error: %s", err)
}
return nil
}