更新内容
This commit is contained in:
10
pkg/models/page.go
Normal file
10
pkg/models/page.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type PageConfig struct {
|
||||
// pages 分支
|
||||
Branch string `json:"branch" yaml:"branch"`
|
||||
// 匹配的域名和路径
|
||||
Domain string `json:"domain" yaml:"domain"`
|
||||
// 路由模式 (default / history)
|
||||
RouteMode string `json:"route" yaml:"route"`
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package providers
|
||||
|
||||
import "io"
|
||||
|
||||
type Blob struct {
|
||||
MediaType string
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
type Backend interface {
|
||||
Owners() ([]string, error)
|
||||
Repos(owner string) ([]string, error)
|
||||
Branches(owner, repo string) ([]string, error)
|
||||
Open(owner, repo, branch, path string) (Blob, error)
|
||||
}
|
||||
@@ -1,24 +1,122 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
const GiteaMaxCount = 9999
|
||||
|
||||
type ProviderGitea struct {
|
||||
BaseUrl string
|
||||
Token string
|
||||
|
||||
gitea *gitea.Client
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Owners() ([]string, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func NewGitea(url string, token string) (*ProviderGitea, error) {
|
||||
client, err := gitea.NewClient(url, gitea.SetGiteaVersion(""), gitea.SetToken(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ProviderGitea{
|
||||
BaseUrl: url,
|
||||
Token: token,
|
||||
gitea: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Repos(owner string) ([]string, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *ProviderGitea) Repos(owner string) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
if repos, resp, err := g.gitea.ListOrgRepos(owner, gitea.ListOrgReposOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
}); err != nil || (resp != nil && resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound) {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return nil, err
|
||||
} else {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, item := range repos {
|
||||
result[item.Name] = item.DefaultBranch
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
if repos, resp, err := g.gitea.ListUserRepos(owner, gitea.ListReposOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
}); err != nil || (resp != nil && resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound) {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return nil, err
|
||||
} else {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, item := range repos {
|
||||
result[item.Name] = item.DefaultBranch
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return result, nil
|
||||
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Branches(owner, repo string) ([]string, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *ProviderGitea) Branches(owner, repo string) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
if branches, resp, err := g.gitea.ListRepoBranches(owner, repo, gitea.ListRepoBranchesOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
}); err != nil {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return nil, err
|
||||
} else {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, branch := range branches {
|
||||
result[branch.Name] = branch.Commit.ID
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Open(owner, repo, branch, path string) (Blob, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
func (g *ProviderGitea) Open(client http.Client, owner, repo, commit, path string, headers map[string]string) (*http.Response, error) {
|
||||
giteaURL, err := url.JoinPath(g.BaseUrl, "api/v1/repos", owner, repo, "media", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
giteaURL += "?ref=" + url.QueryEscape(commit)
|
||||
req, err := http.NewRequest(http.MethodGet, giteaURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if headers != nil {
|
||||
for key, value := range headers {
|
||||
req.Header.Add(key, value)
|
||||
}
|
||||
}
|
||||
req.Header.Add("Authorization", "token "+g.Token)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil && resp == nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
16
pkg/server.go
Normal file
16
pkg/server.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/services"
|
||||
)
|
||||
|
||||
type ServerOptions struct {
|
||||
Domain string
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
}
|
||||
|
||||
func NewServer(backend services.Backend, options *ServerOptions) *Server {
|
||||
|
||||
}
|
||||
14
pkg/services/backend.go
Normal file
14
pkg/services/backend.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Backend interface {
|
||||
// Repos return repo name + default branch
|
||||
Repos(owner string) (map[string]string, error)
|
||||
// Branches return branch + commit id
|
||||
Branches(owner, repo string) (map[string]string, error)
|
||||
// Open return file or error
|
||||
Open(client http.Client, owner, repo, commit, path string, headers map[string]string) (*http.Response, error)
|
||||
}
|
||||
7
pkg/services/cache.go
Normal file
7
pkg/services/cache.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package services
|
||||
|
||||
type Cache interface {
|
||||
Put(key string, value []byte) error
|
||||
Get(key string) ([]byte, error)
|
||||
Delete(key string) error
|
||||
}
|
||||
Reference in New Issue
Block a user