Created
April 19, 2021 07:21
-
-
Save mrotaru/dfa728ce90d9869518b865a677750e46 to your computer and use it in GitHub Desktop.
This file contains 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 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