Skip to content

Commit

Permalink
Implement server base path configuration for reverse proxies (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardek66 authored Oct 25, 2022
1 parent a89b80b commit 01d84e4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions server.example.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[Server]
port = 9001
staticDir = "./static"
basePath = "/"
adminPassword = "1337"
pauseOnChange = true
pauseOnLeave = false
20 changes: 11 additions & 9 deletions src/kino_server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,18 @@ proc serveFile(req: Request) {.async.} =

let
root = cfg.staticDir
index = root & "/client.html"
path = req.url.path
fullPath = root & path
index = root / "client.html"
path = req.url.path.relativePath(cfg.basePath)
filePath = root / path
error404 = "File not found (404)"

if dirExists(root):
if path == "/" and fileExists(index):
file = index;
if fileExists(fullPath):
file = fullPath
if req.url.path == cfg.basePath:
if fileExists(index):
file = index

if fileExists(filePath):
file = filePath

if file.len > 0:
if file notin httpCache:
Expand All @@ -193,7 +195,7 @@ proc serveFile(req: Request) {.async.} =
await req.respond(code, content)

proc cb(req: Request) {.async, gcsafe.} =
if req.url.path == "/ws":
if req.url.path.relativePath(cfg.basePath) == "ws":
var client = Client()
try:
client.ws = await newWebSocket(req)
Expand All @@ -213,6 +215,6 @@ proc cb(req: Request) {.async, gcsafe.} =
else:
await serveFile(req)

echo "Listening at ws://localhost:", cfg.port, "/ws"
echo "Listening at ws://localhost:", cfg.port, cfg.basePath / "ws"
var server = newAsyncHttpServer()
waitFor server.serve(Port(cfg.port), cb)
2 changes: 2 additions & 0 deletions src/server/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type
Config* = object
port*: int
staticDir*: string
basePath*: string
adminPassword*: string
pauseOnChange*: bool
pauseOnLeave*: bool
Expand All @@ -14,6 +15,7 @@ proc getConfig*(): Config =
Config(
port: cfg.get("Server", "port", 9001),
staticDir: cfg.get("Server", "staticDir", "./static"),
basePath: cfg.get("Server", "basePath", "/"),
adminPassword: cfg.get("Server", "adminPassword", "1337"),
pauseOnChange: cfg.get("Server", "pauseOnChange", true),
pauseOnLeave: cfg.get("Server", "pauseOnLeave", false)
Expand Down
4 changes: 2 additions & 2 deletions static/client.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="./style.css" />
<link rel="stylesheet" href="https://cdn.plyr.io/3.7.2/plyr.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kinoplex Web</title>
Expand All @@ -10,6 +10,6 @@
<div id="ROOT"></div>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="https://cdn.plyr.io/3.7.2/plyr.js"></script>
<script src="client.js"></script>
<script src="./client.js"></script>
</body>
</html>

0 comments on commit 01d84e4

Please sign in to comment.