Advertisement
Guest User

Untitled

a guest
Apr 18th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private def start_server(port)
  2. ws_handler = ->( ws : HTTP::Websocket, ctx : HTTP::Server::Context ) do
  3. loop do
  4. select
  5. when msg = chan.receive then ws.send msg
  6. when timeout 1.second then nil
  7. end
  8. end
  9. end
  10.  
  11. server = HTTP::Server.new([
  12. HTTP::ErrorHandler.new,
  13. HTTP::LogHandler.new,
  14. HTTP::CompressHandler.new,
  15. HTTP::WebocketHandler.new(ws_handler),
  16. HTTP::StaticFileHandler.new("./output/", directory_listing: false),
  17. ]) do |context|
  18. if is_html_index_request?(context)
  19. context.response.status_code = 301
  20. context.response.headers["Location"] = "#{context.request.path}index.html"
  21. puts "\nRedirecting implicit HTML index request..." # TODO colorize
  22. else
  23. context.response.status_code = 404
  24. STDERR.puts "\nFile not found!" # TODO colorize
  25. end
  26. end
  27.  
  28. address = server.bind_tcp port
  29. puts "Listening on http://#{address}"
  30. server.listen
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement