mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 13:56:57 +00:00
own file
This commit is contained in:
parent
041a1e11b9
commit
0bc0a149d0
4 changed files with 63 additions and 63 deletions
|
@ -21,5 +21,3 @@ var fileCacheSizeLimit = 1024 * 1024
|
|||
var canonicalDomainCacheTimeout = 15 * time.Minute
|
||||
|
||||
const canonicalDomainConfig = ".domains"
|
||||
|
||||
const giteaAPIRepos = "/api/v1/repos/"
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
package upstream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
|
||||
"codeberg.org/codeberg/pages/server/cache"
|
||||
)
|
||||
|
@ -17,22 +11,6 @@ func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, m
|
|||
return checkCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken, canonicalDomainCache)
|
||||
}
|
||||
|
||||
func giteaRawContent(targetOwner, targetRepo, ref, giteaRoot, giteaAPIToken, resource string) ([]byte, error) {
|
||||
req := fasthttp.AcquireRequest()
|
||||
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func checkCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken string, canonicalDomainCache cache.SetGetKey) (string, bool) {
|
||||
var (
|
||||
domains []string
|
||||
|
|
63
server/upstream/gitea.go
Normal file
63
server/upstream/gitea.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package upstream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fastjson"
|
||||
)
|
||||
|
||||
const giteaAPIRepos = "/api/v1/repos/"
|
||||
|
||||
func giteaRawContent(targetOwner, targetRepo, ref, giteaRoot, giteaAPIToken, resource string) ([]byte, error) {
|
||||
req := fasthttp.AcquireRequest()
|
||||
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func giteaGetRepoBranchTimestamp(giteaRoot, repoOwner, repoName, branchName, giteaAPIToken string) (time.Time, error) {
|
||||
client := getFastHTTPClient(5 * time.Second)
|
||||
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := client.Do(req, res); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return time.Time{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp"))
|
||||
}
|
||||
|
||||
func giteaGetRepoDefaultBranch(giteaRoot, repoOwner, repoName, giteaAPIToken string) (string, error) {
|
||||
client := getFastHTTPClient(5 * time.Second)
|
||||
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := client.Do(req, res); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return fastjson.GetString(res.Body(), "default_branch"), nil
|
||||
}
|
|
@ -1,13 +1,8 @@
|
|||
package upstream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fastjson"
|
||||
|
||||
"codeberg.org/codeberg/pages/server/cache"
|
||||
)
|
||||
|
||||
|
@ -47,40 +42,6 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, br
|
|||
return result
|
||||
}
|
||||
|
||||
func giteaGetRepoBranchTimestamp(giteaRoot, repoOwner, repoName, branchName, giteaAPIToken string) (time.Time, error) {
|
||||
client := getFastHTTPClient(5 * time.Second)
|
||||
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := client.Do(req, res); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return time.Time{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp"))
|
||||
}
|
||||
|
||||
func giteaGetRepoDefaultBranch(giteaRoot, repoOwner, repoName, giteaAPIToken string) (string, error) {
|
||||
client := getFastHTTPClient(5 * time.Second)
|
||||
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName))
|
||||
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
||||
res := fasthttp.AcquireResponse()
|
||||
|
||||
if err := client.Do(req, res); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if res.StatusCode() != fasthttp.StatusOK {
|
||||
return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||
}
|
||||
return fastjson.GetString(res.Body(), "default_branch"), nil
|
||||
}
|
||||
|
||||
type fileResponse struct {
|
||||
exists bool
|
||||
mimeType string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue