rename and move related codes together

This commit is contained in:
Ronmi Ren 2025-01-07 22:17:16 +08:00
commit 63e4038a28
3 changed files with 45 additions and 51 deletions

View file

@ -1,13 +1,56 @@
package lib
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
func Run(bind string, f *Forgejo) (*http.Server, error) {
type Forgejo struct {
Server url.URL
Token string
Branch string
}
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
p := u.Query()
p.Set("ref", branch)
u.RawQuery = p.Encode()
fmt.Println(u.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return
}
for k, v := range headers {
if v == "" {
continue
}
req.Header.Set(k, v)
}
req.Header.Set("Authorization", "token "+f.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
if resp.StatusCode == http.StatusNotFound {
err = ErrNotFound
return
}
return resp, nil
}
func UseAPI(bind string, f *Forgejo) (*http.Server, error) {
if f == nil {
return nil, errors.New("forgejo is nil")
}