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]
URL="https://gitea.com/"
Org="gitea"
Token="" # Must have misc:write, org:read, repo:read permission
MaxConnections=2 # Use zero for unlimited
[Redirect]

View File

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

View File

@ -15,8 +15,8 @@ import (
type Config struct {
BindTo string
Gitea struct {
URL, Org string
MaxConnections int64
URL, Org, Token string
MaxConnections int64
}
Redirect map[string]string
Template struct {
@ -55,7 +55,7 @@ func main() {
}
// 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
go app.syncWorker(context.Background())