Skip to content

Instantly share code, notes, and snippets.

@zakame
Last active March 6, 2021 22:18
Show Gist options
  • Save zakame/eb48bbecd2f74e9bb2463687ba8d611b to your computer and use it in GitHub Desktop.
Save zakame/eb48bbecd2f74e9bb2463687ba8d611b to your computer and use it in GitHub Desktop.
mojo-envoy
#/usr/bin/env perl
use Mojolicious::Lite -signatures;
# Template with browser-side code
get '/' => 'index';
# WebSocket echo service
websocket '/echo' => sub ($c) {
# Opened
$c->app->log->debug('WebSocket opened');
# Increase inactivity timeout for connection a bit
$c->inactivity_timeout(300);
# Incoming message
$c->on(message => sub ($c, $msg) {
$c->send("echo: $msg");
});
# Closed
$c->on(finish => sub ($c, $code, $reason = undef) {
$c->app->log->debug("WebSocket closed with status $code");
});
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head><title>Echo</title></head>
<body>
<script>
var ws = new WebSocket('<%= url_for('echo')->to_abs %>');
// Incoming messages
ws.onmessage = function (event) {
document.body.innerHTML += event.data + '<br/>';
};
// Outgoing messages
ws.onopen = function (event) {
window.setInterval(function () { ws.send('Hello Mojo!') }, 1000);
};
</script>
</body>
</html>
version: "3"
services:
mojo:
build: .
ports:
- "3000"
networks:
- envoymesh
envoy:
image: envoyproxy/envoy-dev
command: /usr/local/bin/envoy -c /envoy.yaml
ports:
- "10000:10000"
- "18081:18081"
networks:
- envoymesh
volumes:
- "./envoy.yaml:/envoy.yaml:ro"
networks:
envoymesh:
FROM perl:5.32-slim-buster
RUN cpanm -nq Mojolicious
COPY app.pl /
CMD ["/usr/local/bin/perl", "/app.pl", "daemon", "-l", "http://*:8080"]
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 10000 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: index_http
route_config:
name: local_route
virtual_hosts:
- name: service
domains: ["*"]
routes:
- match:
prefix: "/"
route:
cluster: local_service
upgrade_configs:
- upgrade_type: websocket
http_filters:
- name: envoy.filters.http.router
typed_config:
clusters:
- name: local_service
connect_timeout: 0.25s
type: strict_dns
lb_policy: round_robin
load_assignment:
cluster_name: local_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address: { address: mojo, port_value: 8080 }
admin:
access_log_path: "/dev/null"
address:
socket_address: { address: 0.0.0.0, port_value: 18081 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment