Skip to content

Commit

Permalink
support for defining default MIME types for responses. (#45) (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
vyan authored Sep 13, 2023
1 parent 18d3af9 commit 15b749b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
9 changes: 6 additions & 3 deletions pjs/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Configs": {
"EnableDebug": true
"EnableDebug": true,
"DefaultMimeType": "application/xhtml+xml"
},
"Listeners": [
{
Expand Down Expand Up @@ -35,8 +36,10 @@
},
"8081": {
"*": {
"DefaultMimeType": "text/plain",
"Matches": [
{
"DefaultMimeType": "text/html",
"ServerRoot": "/var/www/html",
"Index": [
"index.html",
Expand All @@ -58,7 +61,7 @@
"ServerRoot": "www2",
"Index": [
"default.html",
"index.htm"
"index.html"
]
}
]
Expand Down Expand Up @@ -155,4 +158,4 @@
]
},
"Version": "0"
}
}
27 changes: 19 additions & 8 deletions pjs/server/web-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
((
{ isDebugEnabled } = pipy.solve('config.js'),
{ config, isDebugEnabled } = pipy.solve('config.js'),

extTypes = ((mime = JSON.decode(pipy.load('files/mime.json'))) => (
Object.fromEntries(
Expand All @@ -13,14 +13,24 @@
)
))(),

matchContentType = ext => (
(ext === '') ? (
'text/html'
) : (
extTypes?.[ext] || 'application/octet-stream'
defaultMimeTypeCache = new algo.Cache(
route => (
route?.config?.DefaultMimeType ? (
route.config.DefaultMimeType
) : (
__domain?.DefaultMimeType ? (
__domain.DefaultMimeType
) : (
config?.Configs?.DefaultMimeType
)
)
)
),

matchContentType = ext => (
extTypes?.[ext] || defaultMimeTypeCache.get(__route) || 'application/octet-stream'
),

checkFileMode = filepath => (
(
s = os.stat(filepath)
Expand Down Expand Up @@ -113,6 +123,7 @@
})

.import({
__domain: 'route',
__route: 'route',
})

Expand All @@ -133,7 +144,7 @@
)
)
),
_message || new Message({ status: 404 }, 'Not Found')
_message || new Message({ status: 404 }, 'Not Found')
)
)
.branch(
Expand All @@ -146,4 +157,4 @@
)
)

)()
)()

0 comments on commit 15b749b

Please sign in to comment.