Last active
March 30, 2017 15:08
-
-
Save creationix/df181ce4fde5ed6710e4aa4164bd26cc to your computer and use it in GitHub Desktop.
Example of http -> https redirect in weblit
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
require('weblit-app') | |
-- Listen on port 80 without encryption | |
.bind {host = "0.0.0.0", port = 80 } | |
-- Listen on port 443 with encryption | |
.bind {host = "0.0.0.0", port = 443, tls = { | |
cert = module:load("cert.pem"), | |
key = module:load("key.pem") | |
}} | |
-- Set an outer middleware for logging requests and responses | |
.use(require('weblit-logger')) | |
-- This adds missing headers, and tries to do automatic cleanup. | |
.use(require('weblit-auto-headers')) | |
.use(function (req, res, go) | |
-- If the request is already secure, ignore this route. | |
if req.socket.tls then return go() end | |
-- Otherwise, redirect them to the same url, but https. | |
res.code = 301 | |
res.headers.Location = "https://" .. req.headers.Host .. req.path | |
res.body = "Redirecting to secure protocol...\n" | |
end) | |
.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this requires coro-net at least at 3.1.0 for the
req.socket.tls
property to exist on secure connections.https://lit.luvit.io/packages/creationix/coro-net/3.1.0