11package core
22
33import (
4+ "context"
45 "fmt"
56 "io"
67 "net/http"
@@ -39,9 +40,9 @@ func NewServerMeta(client *http.Client, backend Backend, kv utils.KVConfig, doma
3940 return & ServerMeta {backend , domain , client , kv , ttl , utils .NewLocker ()}
4041}
4142
42- func (s * ServerMeta ) GetMeta (owner , repo , branch string ) (* PageMetaContent , error ) {
43+ func (s * ServerMeta ) GetMeta (ctx context. Context , owner , repo , branch string ) (* PageMetaContent , error ) {
4344 rel := NewPageMetaContent ()
44- if repos , err := s .Repos (owner ); err != nil {
45+ if repos , err := s .Repos (ctx , owner ); err != nil {
4546 return nil , err
4647 } else {
4748 defBranch := repos [repo ]
@@ -52,7 +53,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
5253 branch = defBranch
5354 }
5455 }
55- if branches , err := s .Branches (owner , repo ); err != nil {
56+ if branches , err := s .Branches (ctx , owner , repo ); err != nil {
5657 return nil , err
5758 } else {
5859 info := branches [branch ]
@@ -64,7 +65,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
6465 }
6566
6667 key := fmt .Sprintf ("meta/%s/%s/%s" , owner , repo , branch )
67- cache , err := s .cache .Get (key )
68+ cache , err := s .cache .Get (ctx , key )
6869 if err != nil && ! errors .Is (err , os .ErrNotExist ) {
6970 return nil , err
7071 }
@@ -79,7 +80,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
7980 mux := s .locker .Open (key )
8081 mux .Lock ()
8182 defer mux .Unlock ()
82- cache , err = s .cache .Get (key )
83+ cache , err = s .cache .Get (ctx , key )
8384 if err == nil {
8485 if err = rel .From (cache ); err == nil {
8586 if ! rel .IsPage {
@@ -90,25 +91,25 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
9091 }
9192
9293 // 确定存在 index.html , 否则跳过
93- if find , _ := s .FileExists (owner , repo , rel .CommitID , "index.html" ); ! find {
94+ if find , _ := s .FileExists (ctx , owner , repo , rel .CommitID , "index.html" ); ! find {
9495 rel .IsPage = false
95- _ = s .cache .Put (key , rel .String (), s .ttl )
96+ _ = s .cache .Put (ctx , key , rel .String (), s .ttl )
9697 return nil , os .ErrNotExist
9798 } else {
9899 rel .IsPage = true
99100 }
100101 errFunc := func (err error ) (* PageMetaContent , error ) {
101102 rel .IsPage = false
102103 rel .ErrorMsg = err .Error ()
103- _ = s .cache .Put (key , rel .String (), s .ttl )
104+ _ = s .cache .Put (ctx , key , rel .String (), s .ttl )
104105 return nil , err
105106 }
106107 // 添加默认跳过的内容
107108 for _ , defIgnore := range rel .Ignore {
108109 rel .ignoreL = append (rel .ignoreL , glob .MustCompile (defIgnore ))
109110 }
110111 // 解析配置
111- if data , err := s .ReadString (owner , repo , rel .CommitID , ".pages.yaml" ); err == nil {
112+ if data , err := s .ReadString (ctx , owner , repo , rel .CommitID , ".pages.yaml" ); err == nil {
112113 cfg := new (PageConfig )
113114 if err = yaml .Unmarshal ([]byte (data ), cfg ); err != nil {
114115 return errFunc (err )
@@ -172,7 +173,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
172173 }
173174
174175 // 兼容 github 的 CNAME 模式
175- if cname , err := s .ReadString (owner , repo , rel .CommitID , "CNAME" ); err == nil {
176+ if cname , err := s .ReadString (ctx , owner , repo , rel .CommitID , "CNAME" ); err == nil {
176177 cname = strings .TrimSpace (cname )
177178 if regexpHostname .MatchString (cname ) && ! strings .HasSuffix (strings .ToLower (cname ), strings .ToLower (s .Domain )) {
178179 rel .Alias = append (rel .Alias , cname )
@@ -182,12 +183,12 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
182183 }
183184 rel .Alias = utils .ClearDuplicates (rel .Alias )
184185 rel .Ignore = utils .ClearDuplicates (rel .Ignore )
185- _ = s .cache .Put (key , rel .String (), s .ttl )
186+ _ = s .cache .Put (ctx , key , rel .String (), s .ttl )
186187 return rel , nil
187188}
188189
189- func (s * ServerMeta ) ReadString (owner , repo , branch , path string ) (string , error ) {
190- resp , err := s .Open (s .client , owner , repo , branch , path , nil )
190+ func (s * ServerMeta ) ReadString (ctx context. Context , owner , repo , branch , path string ) (string , error ) {
191+ resp , err := s .Open (ctx , s .client , owner , repo , branch , path , nil )
191192 if resp != nil {
192193 defer resp .Body .Close ()
193194 }
@@ -204,8 +205,8 @@ func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error
204205 return string (all ), nil
205206}
206207
207- func (s * ServerMeta ) FileExists (owner , repo , branch , path string ) (bool , error ) {
208- resp , err := s .Open (s .client , owner , repo , branch , path , nil )
208+ func (s * ServerMeta ) FileExists (ctx context. Context , owner , repo , branch , path string ) (bool , error ) {
209+ resp , err := s .Open (ctx , s .client , owner , repo , branch , path , nil )
209210 if resp != nil {
210211 defer resp .Body .Close ()
211212 }
0 commit comments