@@ -46,6 +46,8 @@ type ServerOptions struct {
46
46
EnableRender bool
47
47
EnableProxy bool
48
48
49
+ StaticDir string
50
+
49
51
DefaultErrorHandler func (w http.ResponseWriter , r * http.Request , err error )
50
52
}
51
53
@@ -61,6 +63,7 @@ func DefaultOptions(domain string) ServerOptions {
61
63
MetaTTL : time .Minute ,
62
64
EnableRender : true ,
63
65
EnableProxy : true ,
66
+ StaticDir : "" ,
64
67
DefaultErrorHandler : func (w http.ResponseWriter , r * http.Request , err error ) {
65
68
if errors .Is (err , os .ErrNotExist ) {
66
69
http .Error (w , "page not found." , http .StatusNotFound )
@@ -76,24 +79,36 @@ type Server struct {
76
79
meta * core.PageDomain
77
80
reader * core.CacheBackendBlobReader
78
81
backend core.Backend
82
+ fs http.Handler
79
83
}
80
84
85
+ var staticPrefix = "/.well-known/page-server/"
86
+
81
87
func NewPageServer (backend core.Backend , options ServerOptions ) * Server {
82
88
backend = core .NewCacheBackend (backend , options .KVConfig , options .MetaTTL )
83
89
svcMeta := core .NewServerMeta (options .HttpClient , backend , options .KVConfig , options .Domain , options .MetaTTL )
84
90
pageMeta := core .NewPageDomain (svcMeta , options .KVConfig , options .Domain , options .DefaultBranch )
85
91
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
+ }
86
96
return & Server {
87
97
backend : backend ,
88
98
options : & options ,
89
99
meta : pageMeta ,
90
100
reader : reader ,
101
+ fs : fs ,
91
102
}
92
103
}
93
104
94
105
func (s * Server ) ServeHTTP (writer http.ResponseWriter , request * http.Request ) {
95
106
sessionId , _ := uuid .NewRandom ()
96
107
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
+ }
97
112
defer func () {
98
113
if e := recover (); e != nil {
99
114
zap .L ().Error ("panic!" , zap .Any ("error" , e ), zap .Any ("id" , sessionId ))
0 commit comments