gitea: optionally support authenticated api

This commit is contained in:
mappu 2024-03-19 18:42:13 +13:00
parent 1b3a720323
commit 8de7d565a1
3 changed files with 11 additions and 4 deletions

View File

@ -5,6 +5,7 @@ BindTo="0.0.0.0:5656"
[Gitea] [Gitea]
URL="https://gitea.com/" URL="https://gitea.com/"
Org="gitea" Org="gitea"
Token="" # Must have misc:write, org:read, repo:read permission
MaxConnections=2 # Use zero for unlimited MaxConnections=2 # Use zero for unlimited
[Redirect] [Redirect]

View File

@ -17,12 +17,13 @@ import (
type APIClient struct { type APIClient struct {
urlBase string urlBase string
orgName string orgName string
token string
apiSem *semaphore.Weighted apiSem *semaphore.Weighted
} }
// NewAPIClient creates a new Gitea API client for a single Gitea organization. // NewAPIClient creates a new Gitea API client for a single Gitea organization.
// Set maxConnections to 0 for unlimited concurrent API calls. // Set maxConnections to 0 for unlimited concurrent API calls.
func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClient { func NewAPIClient(urlBase string, orgName string, token string, maxConnections int64) *APIClient {
if !strings.HasSuffix(urlBase, `/`) { if !strings.HasSuffix(urlBase, `/`) {
urlBase += `/` urlBase += `/`
@ -31,6 +32,7 @@ func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClie
ret := &APIClient{ ret := &APIClient{
urlBase: urlBase, urlBase: urlBase,
orgName: orgName, orgName: orgName,
token: token,
} }
if maxConnections == 0 { // unlimited if maxConnections == 0 { // unlimited
@ -98,6 +100,10 @@ func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target int
return err return err
} }
if ac.token != "" {
req.Header.Set(`Authorization`, `token `+ac.token)
}
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return err return err

View File

@ -15,8 +15,8 @@ import (
type Config struct { type Config struct {
BindTo string BindTo string
Gitea struct { Gitea struct {
URL, Org string URL, Org, Token string
MaxConnections int64 MaxConnections int64
} }
Redirect map[string]string Redirect map[string]string
Template struct { Template struct {
@ -55,7 +55,7 @@ func main() {
} }
// Create Gitea API client // Create Gitea API client
app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.MaxConnections) app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.Token, app.cfg.Gitea.MaxConnections)
// Sync worker // Sync worker
go app.syncWorker(context.Background()) go app.syncWorker(context.Background())