@@ -2,9 +2,9 @@ package http
2
2
3
3
import (
4
4
"io/fs"
5
+ "net/http"
5
6
6
7
"github.com/ItsNotGoodName/smtpbridge/internal/core"
7
- "github.com/ItsNotGoodName/smtpbridge/pkg/chiext"
8
8
"github.com/ItsNotGoodName/smtpbridge/web"
9
9
"github.com/ItsNotGoodName/smtpbridge/web/pages"
10
10
"github.com/ItsNotGoodName/smtpbridge/web/routes"
@@ -25,7 +25,7 @@ func NewRouter(ct pages.Controller, app core.App, fileFS fs.FS, csrfSecret []byt
25
25
r .Use (middleware .Recoverer )
26
26
r .Use (csrf .Protect (csrfSecret , csrf .Secure (false )))
27
27
28
- chiext . MountFS (r , web .FS )
28
+ mountWebFS (r , web .FS )
29
29
30
30
// Login
31
31
r .Group (func (r chi.Router ) {
@@ -125,3 +125,37 @@ func NewRouter(ct pages.Controller, app core.App, fileFS fs.FS, csrfSecret []byt
125
125
126
126
return r
127
127
}
128
+
129
+ func mountWebFS (r chi.Router , f fs.FS ) error {
130
+ fsHandler := http .StripPrefix ("/" , http .FileServer (http .FS (f )))
131
+
132
+ normalFS := func (w http.ResponseWriter , r * http.Request ) {
133
+ w .Header ().Set ("Cache-Control" , "max-age=3600" )
134
+ fsHandler .ServeHTTP (w , r )
135
+ }
136
+
137
+ // Files in assets have a hash
138
+ assetsFS := func (w http.ResponseWriter , r * http.Request ) {
139
+ w .Header ().Set ("Cache-Control" , "max-age=31536000,immutable" )
140
+ fsHandler .ServeHTTP (w , r )
141
+ }
142
+
143
+ if files , err := fs .ReadDir (f , "." ); err == nil {
144
+ for _ , f := range files {
145
+ name := f .Name ()
146
+ if f .IsDir () {
147
+ if name == "assets" {
148
+ r .Get ("/" + name + "/*" , assetsFS )
149
+ } else {
150
+ r .Get ("/" + name + "/*" , normalFS )
151
+ }
152
+ } else {
153
+ r .Get ("/" + name , normalFS )
154
+ }
155
+ }
156
+ } else if err != fs .ErrNotExist {
157
+ return err
158
+ }
159
+
160
+ return nil
161
+ }
0 commit comments