This commit is contained in:
6543 2022-05-16 00:43:04 +02:00
parent 4f2c94c7d9
commit ec7fcca0f5
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
8 changed files with 96 additions and 128 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/valyala/fasthttp"
@ -12,11 +13,21 @@ import (
"codeberg.org/codeberg/pages/server/shared"
)
const giteaAPIRepos = "/api/v1/repos/"
type Client struct {
giteaRoot string
giteaAPIToken string
}
type FileResponse struct {
Exists bool
MimeType string
Body []byte
}
func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
func NewClient(giteaRoot, giteaAPIToken string) *Client {
return &Client{
giteaRoot: giteaRoot,
@ -24,8 +35,6 @@ func NewClient(giteaRoot, giteaAPIToken string) *Client {
}
}
const giteaAPIRepos = "/api/v1/repos/"
// TODOs:
// * own client to store token & giteaRoot
// * handle 404 -> page will show 500 atm
@ -46,6 +55,19 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str
return res.Body(), nil
}
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
fastClient := shared.GetFastHTTPClient(10 * time.Second)
req := fasthttp.AcquireRequest()
req.SetRequestURI(path.Join(client.giteaRoot, giteaAPIRepos, uri))
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
resp := fasthttp.AcquireResponse()
resp.SetBodyStream(&strings.Reader{}, -1)
return resp, fastClient.Do(req, resp)
}
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
fastClient := shared.GetFastHTTPClient(5 * time.Second)

16
server/gitea/fasthttp.go Normal file
View file

@ -0,0 +1,16 @@
package gitea
import (
"time"
"github.com/valyala/fasthttp"
)
func getFastHTTPClient(timeout time.Duration) *fasthttp.Client {
return &fasthttp.Client{
ReadTimeout: timeout,
MaxConnDuration: 60 * time.Second,
MaxConnWaitTimeout: 1000 * time.Millisecond,
MaxConnsPerHost: 128 * 16, // TODO: adjust bottlenecks for best performance with Gitea!
}
}