Skip to content

Instantly share code, notes, and snippets.

@mrotaru
Created April 19, 2021 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrotaru/dfa728ce90d9869518b865a677750e46 to your computer and use it in GitHub Desktop.
Save mrotaru/dfa728ce90d9869518b865a677750e46 to your computer and use it in GitHub Desktop.
import os
import asynchttpserver
import asyncdispatch
import strutils
import fsnotify
proc main {.async.} =
var server = newAsyncHttpServer()
proc cb(req: Request) {.async.} =
echo req.url.path
var headers = newHttpHeaders()
headers["Content-type"] = "text/event-stream"
headers["Cache-control"] = "no-cache"
if req.url.path == "/sse":
let absDir = absolutePath("./foo")
var watcher = fsnotify.initWatcher(25)
proc myFsNotification(events: seq[PathEvent]) =
echo "got fswatch notification: ", events
asyncCheck req.respond(Http200, "SSE", headers)
fsnotify.register(watcher, absDir, myFsNotification, ms = 25)
while true:
poll(25)
else:
await req.respond(Http200, "other", headers)
server.listen Port(8080)
echo "listening ..."
while true:
if server.shouldAcceptRequest():
asyncCheck server.acceptRequest(cb)
else:
poll()
when isMainModule:
asyncCheck main()
runForever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment