Add support for serving files when API doesn't return good result.
Some checks failed
/ basic (push) Failing after 19s
Some checks failed
/ basic (push) Failing after 19s
This commit is contained in:
parent
857eee205c
commit
486cbfb6b4
6 changed files with 352 additions and 18 deletions
83
lib/web.go
83
lib/web.go
|
|
@ -9,17 +9,21 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"mime"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Forgejo struct {
|
||||
Server url.URL
|
||||
Token string
|
||||
Branch string
|
||||
Server url.URL
|
||||
Token string
|
||||
Branch string
|
||||
NotFound string
|
||||
Files string
|
||||
}
|
||||
|
||||
var ErrNotFound = errors.New("404 not found")
|
||||
|
|
@ -27,7 +31,14 @@ var ErrNotFound = errors.New("404 not found")
|
|||
// headers must ot be nil
|
||||
func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, repo, branch, file string) (ret *http.Response, err error) {
|
||||
u := f.Server
|
||||
u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file
|
||||
u.Path = path.Join(
|
||||
u.Path,
|
||||
"api/v1/repos",
|
||||
user,
|
||||
repo,
|
||||
"raw",
|
||||
file,
|
||||
)
|
||||
p := u.Query()
|
||||
p.Set("ref", branch)
|
||||
u.RawQuery = p.Encode()
|
||||
|
|
@ -36,7 +47,7 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user,
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
for k, v := range headers {
|
||||
if v == "" {
|
||||
continue
|
||||
|
|
@ -46,13 +57,13 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user,
|
|||
req.Header.Set("Authorization", "token "+f.Token)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
err = ErrNotFound
|
||||
return
|
||||
resp.Body.Close()
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
|
|
@ -87,12 +98,49 @@ func trySet(h http.Header, key string, src http.Header) {
|
|||
h.Set(key, value)
|
||||
}
|
||||
|
||||
func (f *Forgejo) serveLocalFile(w http.ResponseWriter, r *http.Request, status int) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if path == "" {
|
||||
path = "index.html"
|
||||
}
|
||||
|
||||
if f.Files != "" {
|
||||
path = filepath.Join(f.Files, path)
|
||||
}
|
||||
|
||||
fmt.Println("Serving local file:", path)
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
path := f.NotFound
|
||||
|
||||
data, readErr := os.ReadFile(path)
|
||||
if readErr != nil {
|
||||
http.Error(w, "404 not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
if ct := mime.TypeByExtension(filepath.Ext(path)); ct != "" {
|
||||
w.Header().Set("Content-Type", ct)
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
||||
arr := strings.Split(r.URL.Path[1:], "/")
|
||||
if len(arr) < 2 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
f.serveLocalFile(w, r, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
user, repo := arr[0], arr[1]
|
||||
if user == "u" {
|
||||
user, repo = repo, f.Branch
|
||||
|
|
@ -104,7 +152,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
file = strings.Join(arr[2:], "/")
|
||||
}
|
||||
|
||||
|
||||
headers := map[string]string{}
|
||||
headers["If-None-Match"] = r.Header.Get("If-None-Match")
|
||||
headers["If-Modified-Since"] = r.Header.Get("If-Modified-Since")
|
||||
|
|
@ -122,11 +170,16 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
|||
headers["CF-Ray"] = r.Header.Get("CF-Ray")
|
||||
resp, err := f.GetFile(r.Context(), headers, user, repo, f.Branch, file)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
f.serveLocalFile(w, r, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
||||
trySet(w.Header(), "Etag", resp.Header)
|
||||
trySet(w.Header(), "Last-Modified", resp.Header)
|
||||
trySet(w.Header(), "Content-Length", resp.Header)
|
||||
|
|
@ -140,7 +193,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
|||
if contentType != "" {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
io.Copy(w, resp.Body)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue