2024-02-11 04:50:46 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"git.lumeweb.com/LumeWeb/gitea-github-proxy/config"
|
|
|
|
"github.com/gorilla/mux"
|
2024-02-11 05:50:51 +00:00
|
|
|
"go.uber.org/fx"
|
2024-02-11 04:50:46 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-02-11 05:15:57 +00:00
|
|
|
type RouteParams struct {
|
2024-02-11 05:50:51 +00:00
|
|
|
fx.In
|
2024-02-11 08:56:32 +00:00
|
|
|
Config *config.Config
|
|
|
|
Db *gorm.DB
|
|
|
|
Logger *zap.Logger
|
|
|
|
R *mux.Router
|
|
|
|
WebhookManager *WebhookManager
|
2024-02-11 05:15:57 +00:00
|
|
|
}
|
2024-02-11 04:50:46 +00:00
|
|
|
|
2024-02-11 05:15:57 +00:00
|
|
|
func SetupRoutes(params RouteParams) {
|
|
|
|
logger := params.Logger
|
|
|
|
r := params.R
|
2024-02-11 04:50:46 +00:00
|
|
|
|
2024-02-11 05:15:57 +00:00
|
|
|
r.Use(loggingMiddleware(logger))
|
2024-02-11 04:50:46 +00:00
|
|
|
|
2024-02-11 05:15:57 +00:00
|
|
|
setupApiRoutes(params)
|
|
|
|
setupSettingsRoutes(params)
|
|
|
|
setupManifestsRoutes(params)
|
|
|
|
setupAppRoutes(params)
|
2024-02-11 05:58:47 +00:00
|
|
|
setupWebhookRoutes(params)
|
2024-02-11 19:17:19 +00:00
|
|
|
setupRestRoutes(params)
|
2024-02-11 04:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ClientParams struct {
|
|
|
|
Config *config.Config
|
|
|
|
AuthToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClient(params ClientParams) (*gitea.Client, error) {
|
|
|
|
options := make([]gitea.ClientOption, 0)
|
|
|
|
|
2024-02-11 19:12:48 +00:00
|
|
|
authToken := ""
|
2024-02-11 04:50:46 +00:00
|
|
|
if len(params.AuthToken) > 0 {
|
2024-02-11 19:12:48 +00:00
|
|
|
authToken = params.AuthToken
|
2024-02-11 04:50:46 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 19:12:48 +00:00
|
|
|
if len(authToken) == 0 {
|
|
|
|
authToken = params.Config.Oauth.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
options = append(options, gitea.SetToken(authToken))
|
|
|
|
|
2024-02-11 04:50:46 +00:00
|
|
|
client, err := gitea.NewClient(params.Config.GiteaUrl, options...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|