Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private def start_server(port)
- ws_handler = ->( ws : HTTP::Websocket, ctx : HTTP::Server::Context ) do {
- loop do
- select
- when msg = chan.receive then ws.send msg
- when timeout 1.second then nil
- end
- end
- }
- server = HTTP::Server.new([
- HTTP::ErrorHandler.new,
- HTTP::LogHandler.new,
- HTTP::CompressHandler.new,
- HTTP::WebocketHandler.new(ws_handler),
- HTTP::StaticFileHandler.new("./output/", directory_listing: false),
- ]) do |context|
- if is_html_index_request?(context)
- context.response.status_code = 301
- context.response.headers["Location"] = "#{context.request.path}index.html"
- puts "\nRedirecting implicit HTML index request..." # TODO colorize
- else
- context.response.status_code = 404
- STDERR.puts "\nFile not found!" # TODO colorize
- end
- end
- address = server.bind_tcp port
- puts "Listening on http://#{address}"
- server.listen
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement