Created
August 6, 2024 15:30
-
-
Save btbytes/9a9f45047ce8ec38c9d31dba126f7b26 to your computer and use it in GitHub Desktop.
static file server in nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/[asynchttpserver, asyncdispatch, os, strutils] | |
proc handleRequest(req: Request) {.async.} = | |
let path = "." & req.url.path | |
if fileExists(path): | |
let content = readFile(path) | |
let contentType = case path.splitFile.ext | |
of ".html": "text/html" | |
of ".css": "text/css" | |
of ".js": "application/javascript" | |
else: "application/octet-stream" | |
await req.respond(Http200, content, newHttpHeaders({"Content-Type": contentType})) | |
else: | |
await req.respond(Http404, "404 Not Found") | |
proc main() = | |
let server = newAsyncHttpServer() | |
waitFor server.serve(Port(8080), handleRequest) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment