Deployment
Running JBend on a server
Build on the server
sudo apt install default-jdk-headless git # Debian or Ubuntu; any JDK 11+ git clone https://github.com/kanetempleton/JBend cd JBend && ./init.sh
Edit universe/config/application.conf, then ./run.sh to try it in the foreground.
Keep secrets out of the config
Put the database address in the environment and refer to it:
dataserver: source env:DB_DSN
With systemd, an environment file readable only by root holds the value:
# /etc/myapp.env DB_DSN=shop_user:s3cret@tcp(localhost:3306)/shop
Run it as a service
# /etc/systemd/system/myapp.service [Unit] Description=My JBend application After=network.target mariadb.service [Service] User=myuser WorkingDirectory=/home/myuser/JBend EnvironmentFile=/etc/myapp.env ExecStart=/home/myuser/JBend/run.sh Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target
sudo systemctl daemon-reload sudo systemctl enable --now myapp journalctl -u myapp -f # the log
JBend does not need root. Use ports above 1024 and let a reverse proxy own 80 and 443.
Behind a reverse proxy
JBend speaks plain HTTP and WebSocket. For a public site, bind it to this machine only and put a proxy in front for TLS:
webserver: listen 8080 bind 127.0.0.1 upgrade /ws 8081 websocket: listen 8081 bind 127.0.0.1 handler com.example.Chat
With nginx:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade; # lets WebSockets through
proxy_set_header Connection "upgrade";
proxy_read_timeout 1h;
}
What JBend does with the headers a proxy adds, only when the connection comes from this machine:
| Header | Effect |
|---|---|
X-Forwarded-For | Becomes the visitor's address for logging, login lockouts and c.getClientIP(). |
X-Forwarded-Proto: https | Session cookies are marked Secure. |
X-Forwarded-Host | Used for the same-site check on login and registration. |
A proxy may serve the application under a path, such as example.com/app/, if it strips the prefix before forwarding.
Write links in your pages as relative URLs (api/login, not /api/login) and they work at any prefix. That is how the
live demo sits under /JBend/demo/ on a site served by a different program.
Checklist
bind 127.0.0.1on every block that the proxy, not the public, should reach.origin your-domain.comon WebSocket blocks that rely on the session cookie.- A database user that can touch only this application's database.
- A
caretakerblock, so the log shows the server is alive. curl http://127.0.0.1:8080/api/statusfrom the server to confirm it is up.
Updating
cd JBend && git pull && ./build.sh && sudo systemctl restart myapp
Sessions survive the restart. WebSocket clients are disconnected and should reconnect.
The API reference
./build.sh docs runs javadoc over the source and writes the class reference to docs/api/, next to these pages.