-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (85 loc) · 2.5 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
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"code.gitea.io/sdk/gitea"
"dhswt.de/drone-gitea-extensions/plugin_config"
"dhswt.de/drone-gitea-extensions/plugin_converter"
"dhswt.de/drone-gitea-extensions/plugin_env"
"dhswt.de/drone-gitea-extensions/plugin_registry"
"dhswt.de/drone-gitea-extensions/plugin_secret"
"dhswt.de/drone-gitea-extensions/shared"
"fmt"
"github.com/drone/drone-go/plugin/config"
"github.com/drone/drone-go/plugin/converter"
"github.com/drone/drone-go/plugin/environ"
"github.com/drone/drone-go/plugin/registry"
"github.com/drone/drone-go/plugin/secret"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
"net/http"
)
func main() {
cfg := new(shared.AppConfig)
err := envconfig.Process("", cfg)
if err != nil {
logrus.Fatal(err)
}
if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if cfg.Secret == "" {
logrus.Fatalln("missing secret key")
}
if cfg.Bind == "" {
cfg.Bind = ":3000"
}
client := createGiteaClient(cfg)
tokenCache := shared.NewTokenCache(client, cfg)
tokenCache.StartCleanupAccessTokenJob()
environHandler := environ.Handler(
cfg.Secret,
plugin_env.New(client, cfg, &tokenCache),
logrus.StandardLogger(),
)
http.Handle("/env", environHandler)
secretHandler := secret.Handler(
cfg.Secret,
plugin_secret.New(client, cfg, &tokenCache),
logrus.StandardLogger(),
)
http.Handle("/secret", secretHandler)
registryHandler := registry.Handler(
cfg.Secret,
plugin_registry.New(client, cfg, &tokenCache),
logrus.StandardLogger(),
)
http.Handle("/registry", registryHandler)
converterHandler := converter.Handler(
plugin_converter.New(client, cfg, &tokenCache),
cfg.Secret,
logrus.StandardLogger(),
)
http.Handle("/convert", converterHandler)
configHandler := config.Handler(
plugin_config.New(client, cfg, &tokenCache),
cfg.Secret,
logrus.StandardLogger(),
)
http.Handle("/config", configHandler)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = fmt.Fprintf(writer, "OK")
})
if cfg.GiteaDroneTokenGCEnable {
shared.StartGiteaTokenCleanupBackgroundJob(client, cfg)
}
logrus.Infof("server listening on address %s", cfg.Bind)
logrus.Fatal(http.ListenAndServe(cfg.Bind, nil))
}
func createGiteaClient(cfg *shared.AppConfig) *gitea.Client {
// gitea.SetToken(cfg.GiteaToken)
client, err := gitea.NewClient(cfg.GiteaURL, gitea.SetBasicAuth(cfg.GiteaUser, cfg.GiteaPass))
if err != nil {
logrus.Fatalf("Failed to create gitea client, %v", err)
}
return client
}