@@ -46,6 +46,8 @@ type ServerOptions struct {
4646 EnableRender bool
4747 EnableProxy bool
4848
49+ StaticDir string
50+
4951 DefaultErrorHandler func (w http.ResponseWriter , r * http.Request , err error )
5052}
5153
@@ -61,6 +63,7 @@ func DefaultOptions(domain string) ServerOptions {
6163 MetaTTL : time .Minute ,
6264 EnableRender : true ,
6365 EnableProxy : true ,
66+ StaticDir : "" ,
6467 DefaultErrorHandler : func (w http.ResponseWriter , r * http.Request , err error ) {
6568 if errors .Is (err , os .ErrNotExist ) {
6669 http .Error (w , "page not found." , http .StatusNotFound )
@@ -76,24 +79,36 @@ type Server struct {
7679 meta * core.PageDomain
7780 reader * core.CacheBackendBlobReader
7881 backend core.Backend
82+ fs http.Handler
7983}
8084
85+ var staticPrefix = "/.well-known/page-server/"
86+
8187func NewPageServer (backend core.Backend , options ServerOptions ) * Server {
8288 backend = core .NewCacheBackend (backend , options .KVConfig , options .MetaTTL )
8389 svcMeta := core .NewServerMeta (options .HttpClient , backend , options .KVConfig , options .Domain , options .MetaTTL )
8490 pageMeta := core .NewPageDomain (svcMeta , options .KVConfig , options .Domain , options .DefaultBranch )
8591 reader := core .NewCacheBackendBlobReader (options .HttpClient , backend , options .Cache , options .MaxCacheSize )
92+ var fs http.Handler
93+ if options .StaticDir != "" {
94+ fs = http .StripPrefix (staticPrefix , http .FileServer (http .Dir (options .StaticDir )))
95+ }
8696 return & Server {
8797 backend : backend ,
8898 options : & options ,
8999 meta : pageMeta ,
90100 reader : reader ,
101+ fs : fs ,
91102 }
92103}
93104
94105func (s * Server ) ServeHTTP (writer http.ResponseWriter , request * http.Request ) {
95106 sessionId , _ := uuid .NewRandom ()
96107 request .Header .Set ("Session-ID" , sessionId .String ())
108+ if s .fs != nil && strings .HasPrefix (request .URL .Path , staticPrefix ) {
109+ s .fs .ServeHTTP (writer , request )
110+ return
111+ }
97112 defer func () {
98113 if e := recover (); e != nil {
99114 zap .L ().Error ("panic!" , zap .Any ("error" , e ), zap .Any ("id" , sessionId ))
0 commit comments