Skip to content
Snippets Groups Projects
Commit ba5d9cc4 authored by q3k's avatar q3k
Browse files

api: support path_in_repo, add CORS

parent 5df68109
Branches
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"path"
"regexp" "regexp"
"strings" "strings"
"time" "time"
...@@ -21,6 +22,7 @@ import ( ...@@ -21,6 +22,7 @@ import (
type appDescriptor struct { type appDescriptor struct {
repository string repository string
pathInRepo string
appInfo *appInfo appInfo *appInfo
} }
...@@ -38,10 +40,11 @@ var ( ...@@ -38,10 +40,11 @@ var (
reAppRepo = regexp.MustCompile(`^([a-zA-Z\-_\.0-9]+)/([a-zA-Z\-_0-9]+)$`) reAppRepo = regexp.MustCompile(`^([a-zA-Z\-_\.0-9]+)/([a-zA-Z\-_0-9]+)$`)
) )
func (s *server) parseAppToml(ctx context.Context, obj *object.Commit) (*appInfo, error) { func (s *server) parseAppToml(ctx context.Context, pathInRepo string, obj *object.Commit) (*appInfo, error) {
f, err := obj.File("flow3r.toml") p := path.Join(pathInRepo, "flow3r.toml")
f, err := obj.File(p)
if err != nil { if err != nil {
return nil, fmt.Errorf("toml open failed: %w", err) return nil, fmt.Errorf("toml open (%q) failed: %w", p, err)
} }
reader, err := f.Reader() reader, err := f.Reader()
if err != nil { if err != nil {
...@@ -99,7 +102,7 @@ func (s *server) parseAppToml(ctx context.Context, obj *object.Commit) (*appInfo ...@@ -99,7 +102,7 @@ func (s *server) parseAppToml(ctx context.Context, obj *object.Commit) (*appInfo
}, nil }, nil
} }
func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error) { func (s *server) getAppInfo(ctx context.Context, pathInRepo, repo string) (*appInfo, error) {
url := "https://git.flow3r.garden/" + repo url := "https://git.flow3r.garden/" + repo
g, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ g, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: url, URL: url,
...@@ -116,7 +119,7 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error) ...@@ -116,7 +119,7 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error)
return nil, fmt.Errorf("no branches") return nil, fmt.Errorf("no branches")
} }
var bname string var bname string
for name, _ := range cfg.Branches { for name := range cfg.Branches {
bname = name bname = name
break break
} }
...@@ -141,7 +144,7 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error) ...@@ -141,7 +144,7 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error)
highsetVerNil := true highsetVerNil := true
firstTime := make(map[int]*appInfo) firstTime := make(map[int]*appInfo)
for { for {
info, err := s.parseAppToml(ctx, obj) info, err := s.parseAppToml(ctx, pathInRepo, obj)
if err == nil { if err == nil {
ver := info.version ver := info.version
firstTime[ver] = info firstTime[ver] = info
...@@ -149,6 +152,8 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error) ...@@ -149,6 +152,8 @@ func (s *server) getAppInfo(ctx context.Context, repo string) (*appInfo, error)
highestVer = ver highestVer = ver
highsetVerNil = false highsetVerNil = false
} }
} else {
log.Printf("%s@%s: %v", repo, obj.Hash.String(), err)
} }
if len(obj.ParentHashes) == 0 { if len(obj.ParentHashes) == 0 {
break break
...@@ -225,6 +230,7 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) { ...@@ -225,6 +230,7 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) {
dec := toml.NewDecoder(reader) dec := toml.NewDecoder(reader)
var dat struct { var dat struct {
Repo string Repo string
PathInRepo string `toml:"path_in_repo"`
} }
err = dec.Decode(&dat) err = dec.Decode(&dat)
reader.Close() reader.Close()
...@@ -232,6 +238,7 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) { ...@@ -232,6 +238,7 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) {
log.Printf("App %q: couldn't parse TOML: %v", m[1], err) log.Printf("App %q: couldn't parse TOML: %v", m[1], err)
continue continue
} }
log.Printf("%+v", dat)
n := reAppRepo.FindStringSubmatch(dat.Repo) n := reAppRepo.FindStringSubmatch(dat.Repo)
if n == nil { if n == nil {
log.Printf("App %q: invalid repo %q", m[1], dat.Repo) log.Printf("App %q: invalid repo %q", m[1], dat.Repo)
...@@ -239,11 +246,12 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) { ...@@ -239,11 +246,12 @@ func (s *server) getAppRegistry(ctx context.Context) ([]*appDescriptor, error) {
} }
res = append(res, &appDescriptor{ res = append(res, &appDescriptor{
repository: dat.Repo, repository: dat.Repo,
pathInRepo: dat.PathInRepo,
}) })
} }
for _, app := range res { for _, app := range res {
info, err := s.getAppInfo(ctx, app.repository) info, err := s.getAppInfo(ctx, app.pathInRepo, app.repository)
if err != nil { if err != nil {
log.Printf("App %q: %v", app.repository, err) log.Printf("App %q: %v", app.repository, err)
continue continue
...@@ -267,7 +275,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) { ...@@ -267,7 +275,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
type app struct { type app struct {
RepoURL string `json:"repoUrl"` RepoURL string `json:"repoUrl"`
Commit string `json:"commit"` Commit string `json:"commit"`
DownloadURL string `json:"downloadURL"` DownloadURL string `json:"downloadUr"`
Name string `json:"name"` Name string `json:"name"`
Menu string `json:"menu"` Menu string `json:"menu"`
Author string `json:"author"` Author string `json:"author"`
...@@ -302,6 +310,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) { ...@@ -302,6 +310,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
Version: a.appInfo.version, Version: a.appInfo.version,
}) })
} }
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
j := json.NewEncoder(w) j := json.NewEncoder(w)
j.Encode(resp) j.Encode(resp)
......
...@@ -96,6 +96,7 @@ func (s *server) handleReleases(w http.ResponseWriter, r *http.Request) { ...@@ -96,6 +96,7 @@ func (s *server) handleReleases(w http.ResponseWriter, r *http.Request) {
Partitions: partitions, Partitions: partitions,
}) })
} }
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
j := json.NewEncoder(w) j := json.NewEncoder(w)
j.Encode(resp) j.Encode(resp)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment