-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (82 loc) · 2.93 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 (
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/iver-wharf/wharf-core/pkg/cacertutil"
"github.com/iver-wharf/wharf-core/pkg/ginutil"
"github.com/iver-wharf/wharf-core/pkg/logger"
"github.com/iver-wharf/wharf-core/pkg/logger/consolepretty"
"github.com/iver-wharf/wharf-provider-gitlab/docs"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// BuildDefinitionFileName is a name of the file that should exists in project
// repository if project will have ability to be built by Wharf.
const BuildDefinitionFileName = ".wharf-ci.yml"
// ProviderName is a provider name that is used in whole wharf system for GitLab.
const ProviderName = "gitlab"
const (
exitCodeFailLoadVersionFile = 1
exitCodeFailLoadConfigFile = 1
exitCodeFailLoadCerts = 1
exitCodeFailBindAddress = 2
)
var log = logger.NewScoped("WHARF-PROVIDER-GITLAB")
// @title Wharf provider API for GitLab
// @description Wharf backend API for integrating GitLab repositories with
// @description the Wharf main API.
// @license.name MIT
// @license.url https://github.com/iver-wharf/wharf-provider-gitlab/blob/master/LICENSE
// @contact.name Iver Wharf GitLab provider API support
// @contact.url https://github.com/iver-wharf/wharf-provider-gitlab/issues
// @contact.email [email protected]
// @basePath /import
func main() {
logger.AddOutput(logger.LevelDebug, consolepretty.Default)
var (
config Config
err error
)
if err = loadEmbeddedVersionFile(); err != nil {
log.Error().WithError(err).Message("Failed to read embedded version.yaml.")
os.Exit(exitCodeFailLoadVersionFile)
}
if config, err = loadConfig(); err != nil {
log.Error().WithError(err).Message("Failed to read config.")
os.Exit(exitCodeFailLoadConfigFile)
}
docs.SwaggerInfo.Version = AppVersion.Version
if config.CA.CertsFile != "" {
client, err := cacertutil.NewHTTPClientWithCerts(config.CA.CertsFile)
if err != nil {
log.Error().WithError(err).Message("Failed to get net/http.Client with certs.")
os.Exit(exitCodeFailLoadCerts)
}
http.DefaultClient = client
}
gin.DefaultWriter = ginutil.DefaultLoggerWriter
gin.DefaultErrorWriter = ginutil.DefaultLoggerWriter
r := gin.New()
r.Use(
ginutil.DefaultLoggerHandler,
//ginutil.RecoverProblem,
)
if config.HTTP.CORS.AllowAllOrigins {
log.Info().Message("Allowing all origins in CORS.")
r.Use(cors.Default())
}
r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{"message": "pong"}) })
r.POST("/import/gitlab/trigger", runGitLabTriggerHandler)
r.GET("/import/gitlab/version", getVersionHandler)
r.GET("/import/gitlab/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
importModule{&config}.register(r)
if err := r.Run(config.HTTP.BindAddress); err != nil {
log.Error().
WithError(err).
WithString("address", config.HTTP.BindAddress).
Message("Failed to start web server.")
os.Exit(exitCodeFailBindAddress)
}
}