Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active March 30, 2017 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/df181ce4fde5ed6710e4aa4164bd26cc to your computer and use it in GitHub Desktop.
Save creationix/df181ce4fde5ed6710e4aa4164bd26cc to your computer and use it in GitHub Desktop.
Example of http -> https redirect in weblit
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()
@creationix
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment