FR: Add URL for viewing pages at tag or commit #44

Open
opened 8 months ago by ktprograms · 3 comments

Currently, using @branch in the URL, you can have pages serve the repo on that branch, but it isn't possible with tags or commits.

BTW this isn't massively important to me so feel free to mark it as low priority.

Currently, using `@branch` in the URL, you can have pages serve the repo on that branch, but it isn't possible with tags or commits. BTW this isn't massively important to me so feel free to mark it as low priority.
Collaborator

Hmm, I don't really know if that's possible with the Gitea API - we're using the API route https://codeberg.org/api/swagger#/repository/repoGetRawFile to retrieve the files, and I think that it can't really do that... We could switch to the non-API raw route, but I don't know if that has any other disadvantages (e.g. caching, latency, ...).

Hmm, I don't really know if that's possible with the Gitea API - we're using the API route https://codeberg.org/api/swagger#/repository/repoGetRawFile to retrieve the files, and I think that it can't really do that... We could switch to the non-API raw route, but I don't know if that has any other disadvantages (e.g. caching, latency, ...).
Poster

How is the branch name currently specified in the API call?

You can probably use the ref query parameter, which is described as: The name of the commit/branch/tag. Default the repository’s default branch (usually master). I just tested and it works with the 10 character shortened commit id (via swagger). The curl command auto generated by swagger is:

curl -X 'GET' \
  'https://codeberg.org/api/v1/repos/ktprograms/table_editor_html/raw/index.html?ref=72a925eb20' \
  -H 'accept: application/json'
How is the branch name currently specified in the API call? You can probably use the `ref` query parameter, which is described as: `The name of the commit/branch/tag. Default the repository’s default branch (usually master)`. I just tested and it works with the 10 character shortened commit id (via swagger). The curl command auto generated by swagger is: ```sh curl -X 'GET' \ 'https://codeberg.org/api/v1/repos/ktprograms/table_editor_html/raw/index.html?ref=72a925eb20' \ -H 'accept: application/json' ```
Poster

@momar: Here's a (very messy) patch that allows me to use (for example) https://ktprograms.localhost.mock.directory:4430/table_editor_html/@72a925eb20/ and view the index.html at commit 72a925eb20:

diff --git a/server/handler.go b/server/handler.go
index 1aaf476..ba31d87 100644
--- a/server/handler.go
+++ b/server/handler.go
@@ -87,16 +87,19 @@ func Handler(mainDomainSuffix, rawDomain []byte,
 			// Check if the branch exists, otherwise treat it as a file path
 			branchTimestampResult := upstream.GetBranchTimestamp(targetOwner, repo, branch, giteaRoot, giteaAPIToken, branchTimestampCache)
 			if branchTimestampResult == nil {
-				// branch doesn't exist
-				return false
+				// // branch doesn't exist
+				// return false
+				targetBranch = branch
 			}
 
 			// Branch exists, use it
 			targetRepo = repo
 			targetPath = strings.Trim(strings.Join(path, "/"), "/")
-			targetBranch = branchTimestampResult.Branch
+			if branchTimestampResult != nil {
+				targetBranch = branchTimestampResult.Branch
 
-			targetOptions.BranchTimestamp = branchTimestampResult.Timestamp
+				targetOptions.BranchTimestamp = branchTimestampResult.Timestamp
+			}
 
 			if canonicalLink != "" {
 				// Hide from search machines & add canonical link
diff --git a/server/upstream/domains.go b/server/upstream/domains.go
index 47a5564..eebb322 100644
--- a/server/upstream/domains.go
+++ b/server/upstream/domains.go
@@ -12,17 +12,18 @@ import (
 func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken string, canonicalDomainCache cache.SetGetKey) (string, bool) {
 	domains := []string{}
 	valid := false
-	if cachedValue, ok := canonicalDomainCache.Get(targetOwner + "/" + targetRepo + "/" + targetBranch); ok {
-		domains = cachedValue.([]string)
-		for _, domain := range domains {
-			if domain == actualDomain {
-				valid = true
-				break
-			}
-		}
+	// if cachedValue, ok := canonicalDomainCache.Get(targetOwner + "/" + targetRepo + "/" + targetBranch); ok {
+	if false {
+		// domains = cachedValue.([]string)
+		// for _, domain := range domains {
+		// 	if domain == actualDomain {
+		// 		valid = true
+		// 		break
+		// 	}
+		// }
 	} else {
 		req := fasthttp.AcquireRequest()
-		req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/.domains" + "?access_token=" + giteaAPIToken)
+		req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/.domains?ref=" + targetBranch + "&access_token=" + giteaAPIToken)
 		res := fasthttp.AcquireResponse()
 
 		err := client.Do(req, res)
diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go
index 88d3471..8d6f564 100644
--- a/server/upstream/upstream.go
+++ b/server/upstream/upstream.go
@@ -53,17 +53,17 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
 		o.ForbiddenMimeTypes = map[string]struct{}{}
 	}
 
-	// Check if the branch exists and when it was modified
-	if o.BranchTimestamp.IsZero() {
-		branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache)
-
-		if branch == nil {
-			html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
-			return true
-		}
-		o.TargetBranch = branch.Branch
-		o.BranchTimestamp = branch.Timestamp
-	}
+	// // Check if the branch exists and when it was modified
+	// if o.BranchTimestamp.IsZero() {
+	// 	branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache)
+
+	// 	if branch == nil {
+	// 		html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency)
+	// 		return true
+	// 	}
+	// 	o.TargetBranch = branch.Branch
+	// 	o.BranchTimestamp = branch.Timestamp
+	// }
 
 	if o.TargetOwner == "" || o.TargetRepo == "" || o.TargetBranch == "" {
 		html.ReturnErrorPage(ctx, fasthttp.StatusBadRequest)
@@ -80,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
 	log.Debug().Msg("preparations")
 
 	// Make a GET request to the upstream URL
-	uri := o.TargetOwner + "/" + o.TargetRepo + "/raw/" + o.TargetBranch + "/" + o.TargetPath
+	uri := o.TargetOwner + "/" + o.TargetRepo + "/raw/" + o.TargetPath + "?ref=" + o.TargetBranch
 	var req *fasthttp.Request
 	var res *fasthttp.Response
 	var cachedResponse fileResponse
@@ -89,7 +89,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st
 		cachedResponse = cachedValue.(fileResponse)
 	} else {
 		req = fasthttp.AcquireRequest()
-		req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "?access_token=" + giteaAPIToken)
+		req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "&access_token=" + giteaAPIToken)
 		res = fasthttp.AcquireResponse()
 		res.SetBodyStream(&strings.Reader{}, -1)
 		err = client.Do(req, res)

It still needs a lot more work with the cache handling because I've commented some of it out where I couldn't be bothered to implement it properly.

@momar: Here's a (_very messy_) patch that allows me to use (for example) https://ktprograms.localhost.mock.directory:4430/table_editor_html/@72a925eb20/ and view the index.html at commit 72a925eb20: ```diff diff --git a/server/handler.go b/server/handler.go index 1aaf476..ba31d87 100644 --- a/server/handler.go +++ b/server/handler.go @@ -87,16 +87,19 @@ func Handler(mainDomainSuffix, rawDomain []byte, // Check if the branch exists, otherwise treat it as a file path branchTimestampResult := upstream.GetBranchTimestamp(targetOwner, repo, branch, giteaRoot, giteaAPIToken, branchTimestampCache) if branchTimestampResult == nil { - // branch doesn't exist - return false + // // branch doesn't exist + // return false + targetBranch = branch } // Branch exists, use it targetRepo = repo targetPath = strings.Trim(strings.Join(path, "/"), "/") - targetBranch = branchTimestampResult.Branch + if branchTimestampResult != nil { + targetBranch = branchTimestampResult.Branch - targetOptions.BranchTimestamp = branchTimestampResult.Timestamp + targetOptions.BranchTimestamp = branchTimestampResult.Timestamp + } if canonicalLink != "" { // Hide from search machines & add canonical link diff --git a/server/upstream/domains.go b/server/upstream/domains.go index 47a5564..eebb322 100644 --- a/server/upstream/domains.go +++ b/server/upstream/domains.go @@ -12,17 +12,18 @@ import ( func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, mainDomainSuffix, giteaRoot, giteaAPIToken string, canonicalDomainCache cache.SetGetKey) (string, bool) { domains := []string{} valid := false - if cachedValue, ok := canonicalDomainCache.Get(targetOwner + "/" + targetRepo + "/" + targetBranch); ok { - domains = cachedValue.([]string) - for _, domain := range domains { - if domain == actualDomain { - valid = true - break - } - } + // if cachedValue, ok := canonicalDomainCache.Get(targetOwner + "/" + targetRepo + "/" + targetBranch); ok { + if false { + // domains = cachedValue.([]string) + // for _, domain := range domains { + // if domain == actualDomain { + // valid = true + // break + // } + // } } else { req := fasthttp.AcquireRequest() - req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/.domains" + "?access_token=" + giteaAPIToken) + req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/.domains?ref=" + targetBranch + "&access_token=" + giteaAPIToken) res := fasthttp.AcquireResponse() err := client.Do(req, res) diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 88d3471..8d6f564 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -53,17 +53,17 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st o.ForbiddenMimeTypes = map[string]struct{}{} } - // Check if the branch exists and when it was modified - if o.BranchTimestamp.IsZero() { - branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache) - - if branch == nil { - html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency) - return true - } - o.TargetBranch = branch.Branch - o.BranchTimestamp = branch.Timestamp - } + // // Check if the branch exists and when it was modified + // if o.BranchTimestamp.IsZero() { + // branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache) + + // if branch == nil { + // html.ReturnErrorPage(ctx, fasthttp.StatusFailedDependency) + // return true + // } + // o.TargetBranch = branch.Branch + // o.BranchTimestamp = branch.Timestamp + // } if o.TargetOwner == "" || o.TargetRepo == "" || o.TargetBranch == "" { html.ReturnErrorPage(ctx, fasthttp.StatusBadRequest) @@ -80,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st log.Debug().Msg("preparations") // Make a GET request to the upstream URL - uri := o.TargetOwner + "/" + o.TargetRepo + "/raw/" + o.TargetBranch + "/" + o.TargetPath + uri := o.TargetOwner + "/" + o.TargetRepo + "/raw/" + o.TargetPath + "?ref=" + o.TargetBranch var req *fasthttp.Request var res *fasthttp.Response var cachedResponse fileResponse @@ -89,7 +89,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st cachedResponse = cachedValue.(fileResponse) } else { req = fasthttp.AcquireRequest() - req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "?access_token=" + giteaAPIToken) + req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "&access_token=" + giteaAPIToken) res = fasthttp.AcquireResponse() res.SetBodyStream(&strings.Reader{}, -1) err = client.Do(req, res) ``` It still needs a lot more work with the cache handling because I've commented some of it out where I couldn't be bothered to implement it properly.
momar added the
contribution welcome
improvement
labels 7 months ago
Sign in to join this conversation.
Loading…
There is no content yet.