Small server that uses MongoDB for redirecting. Can be used as an self-hosted shortlink service for example.
In January 2021 project will be archived and no new changes will be made. Please also note that the server hasn't been tested in production, so be careful! You are free to borrow any code you might find useful. 😄
Read more at https://klooven.link/archive.
There's a small setup needed to get the server up and running...
Create a .env
file in the server root directory. Set these values (example below):
RS_NAME=Development
RS_PORT=2000
RS_DATABASE=mongodb://localhost/rs-development
RS_SECRET=supersecret
RS_NAME
is the name for the server, use anything you likeRS_PORT
is the port where the server runs on, there's no default value built in!RS_DATABASE
is the MongoDB connection URLRS_SECRET
is a secret for JSON Web Tokens (more later)
The redirects should be saved to MongoDB to the redirects
collection. Every entry looks like this:
{
"path": "example",
"destination": "http://example.com",
"type": 307
}
path
is the server path, so in this case the redirect would happen when visitinghttp://your-redirect-server.com/example
destination
is the URL where to redirect the usertype
is the HTTP status code, recommended codes are 301 (permanent) and 307 (temporary)
redirect-server
uses a cache that will reset when the server restarts. In the case that you want to clear the case for one path without restarting the server, you have to do this:
- Sign a JSON Web Token using the secret in
RS_SECRET
(security: make sure that the JWT expires in a short time) - Send a
DELETE
request tohttp://your-redirect-server.com/rs/cache/<PATH>
- Use this header:
Authorization: Bearer <YOUR JSON WEB TOKEN>
- Use this header:
So if we want to reset the cache for the example redirect that we created earlier we send a DELETE
request to http://your-redirect-server.com/rs/cache/example
with the header Authorization: Bearer jU5tANex4mplEt0ken
.
The primary reason is if you have updated an redirect in the database. redirect-server
will not notice this if the redirect is in the internal cache.