gitea: optionally support authenticated api
This commit is contained in:
parent
1b3a720323
commit
8de7d565a1
@ -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]
|
||||
|
@ -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
|
||||
|
4
main.go
4
main.go
@ -15,7 +15,7 @@ import (
|
||||
type Config struct {
|
||||
BindTo string
|
||||
Gitea struct {
|
||||
URL, Org string
|
||||
URL, Org, Token string
|
||||
MaxConnections int64
|
||||
}
|
||||
Redirect map[string]string
|
||||
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user