Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created August 6, 2024 15:30
Show Gist options
  • Save btbytes/9a9f45047ce8ec38c9d31dba126f7b26 to your computer and use it in GitHub Desktop.
Save btbytes/9a9f45047ce8ec38c9d31dba126f7b26 to your computer and use it in GitHub Desktop.

Revisions

  1. btbytes created this gist Aug 6, 2024.
    20 changes: 20 additions & 0 deletions serve.nim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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()