Backend APIs at urlscuts.co/
Swagger API Docs at urlscuts.co/api-docs
Install Node dependencies:
npm install
Create the env files for dev, test and prod, refer env.example
.env.development
.env.production
The boilerplate uses MongoDB with Mongoose.
and update the db URI in the env file
Launch your server with this command:
npm run dev
- Pull the docker Image from here: https://urlscut.co/dockerimg || https://hub.docker.com/r/macbeth98/url-shortener
- Run the server by pulling the docker image. Check the
env.example
file for Docker related settings and based on that create or update the.env.production
file. - This file will be used by the docker-compose to inject the env variables.
docker-compose up -d
- The APIs are documented properly using the fastify-Swagger utility module.
- The App uses Fastify freamework instead of express. I have used this as Fastify boasts to be the fastest and with less overhead. Technically it would mean that fastify server would be able to handle more requests/sec. You can see the benchmarks here.
- I have used Typescript so that my code will be clean and much readable. But also to reduce the run time errors. With Typescript I can do better apply the OOD principles.
- I have extensively used Dependency Injection wherever possible, as that would give us much better usecase in switching of services and also better tests!
- The fastify framework with it's plugins usually becomes tightly coupled as we would need to pass the fastify app instances into controller and even services to use the plugins and also for the corresponding constructor Injections.
- To avoid that I created a
ServiceContainer
that essentially acts as the place where all the Classes will be instantiated and will be served wherever necessary through dependency Injection. This class will also hold fastify Plugins as properties and can be used anywhere through dependency Injection or directly importing the instance. - All the classes are singleton and are only instantiated once through
ServiceContainer
andServiceContainer
class itself is also instantiated only once. - Through this way instead of depending on the fastify Instance and fastify framework, the controller and service layer depends on this ServiceContainer. By doing this way, the app becomes platform independent and can easily switch between frameworks, by changing the routing layer and assigning proper corresponding ServiceContainer proprties.
- To avoid that I created a
- The app also seperates the Data Access Object layer: communication with Database through the use of Interfaces and Dependency Injection in the service layer. In this way, it will be easier to switch the Database.
- The design makes extensive use of this Interfaces and Dependency Injection, as can be seen from the DAOs and Auth Providers, where I have implemented two Auth Provider, one is Basic Auth and the Other is AWS Cognito.
- The folder or directory structure is also divided into modules, where in each module intiates its own services and its corresponding provider dependencies. This will be done at the bootup of the app through the ServiceContainer class.
- I have used the counter approach to generate new Aliases that will be mapped to the long urls.
- I have chosen to do this way, because I want to generate new Alias irrespective of the long URL unless the user chose a custom Alias.
- The counter will keep getting increment for each URL and the number will be converted to base62 which will be used as the Alias.
- By following this approach trillions of unique Alias can be generated. As the number from the counter increases, the Alias will also increase.
- The current Alias will atleast have 4 characters in it.
- Read more about counter approach from GeeksforGeeks : https://urlscut.co/counter
- The server uses in memory cache, that is design similar LRU (Least Recently Used), in conjunction with Redis.
- This makes the processing of Short URLs, much faster.
- Written end to end tests by calling the routing apis through fastify inject.
- AUTH Router testing completed.
- URL Router testing completed.
- The server is hosted on the AWS EC2 instance through nginx proxy server.
- The server is also configured http2.
- As mentioned, the APIs are live at urlscut.co