Skip to content

Commit ae8ea82

Browse files
authored
Merge pull request #1182 from GoLinks/content-type
Add `content-type` Header to served files
2 parents 1644480 + 3738161 commit ae8ea82

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

monocle.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ library
142142
, envparse < 1
143143
, exceptions >= 0.10
144144
, fakedata >= 1.0
145+
, filepath
145146
, fast-logger
146147
, foldl
147148
, gerrit >= 0.1.6

src/Monocle/Main.hs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Monocle.Prelude
1717
import Monocle.Search.Query (loadAliases)
1818
import Monocle.Servant.HTTP (server)
1919
import Monocle.Servant.HTTPMain (RootAPI)
20+
import Network.HTTP.Types (hContentType)
2021
import Network.HTTP.Types.Status qualified as HTTP
2122
import Network.Wai qualified as Wai
2223
import Network.Wai.Handler.Warp qualified as Warp
@@ -28,6 +29,7 @@ import Prometheus.Metric.GHC (ghcMetrics)
2829
import Servant
2930
import Servant.Auth.Server (CookieSettings (..), cookieXsrfSetting, defaultCookieSettings, defaultJWTSettings)
3031
import System.Directory qualified
32+
import System.FilePath (takeExtension)
3133

3234
import Effectful qualified as E
3335
import Effectful.Concurrent.MVar qualified as E
@@ -62,6 +64,22 @@ mkStaticMiddleware publicUrl title webAppPath = do
6264
exist <- System.Directory.doesPathExist fp
6365
pure $ if exist then Just fp else otherFP
6466

67+
-- Content-Type for static files
68+
staticFileHeaders path =
69+
[
70+
( hContentType
71+
, case takeExtension path of
72+
".css" -> "text/css"
73+
".js" -> "application/javascript"
74+
".ico" -> "image/x-icon"
75+
".png" -> "image/png"
76+
".svg" -> "image/svg+xml"
77+
".woff" -> "font/woff"
78+
".woff2" -> "font/woff2"
79+
_ -> "application/octet-stream"
80+
)
81+
]
82+
6583
-- The middleware pass the request to the monocle app
6684
staticMiddleware :: LByteString -> FilePath -> Wai.Application -> Wai.Application
6785
staticMiddleware index rootDir app' req waiRespond = app' req responder
@@ -76,11 +94,11 @@ mkStaticMiddleware publicUrl title webAppPath = do
7694
if Data.List.null reqPath || ".." `Data.List.isInfixOf` reqPath
7795
then -- The path is empty or fishy
7896
pure Nothing
79-
else -- Checks if the request match a file, such as favico or css
97+
else -- Checks if the request matches a file, such as favico or css
8098
(rootDir <> reqPath) `existOr` Nothing
8199
waiRespond $ case respPath of
82-
-- The path exist, returns it
83-
Just path -> Wai.responseFile HTTP.status200 [] path Nothing
100+
-- The path exist, returns it with correct Content-Type
101+
Just path -> Wai.responseFile HTTP.status200 (staticFileHeaders path) path Nothing
84102
-- Otherwise returns the index
85103
Nothing -> Wai.responseLBS HTTP.status200 [] index
86104

0 commit comments

Comments
 (0)