Simple URL shorten-er REST service, think of it like Hello World of bitly.com API done in Java Spring-Boot project :)
These instructions are for linux
only:
- Check your
JAVA
version you can do that by runningjava --version
. You should haveJAVA 11
, project was build usingopenjdk 11.0.6 2020-01-14
- Check your
MAVEN
instalation, you can do that by runningmvn -v
, project ueses Maven version3.6.3
- Clone the project
git clone https://github.com/igor-susic/url-shortener.git
cd
into/url-shortener
directory- Run
mvn clean install
Look for the output of this command, you should see directory wheremaven
did build your.jar
file it will look something like this/home/$USER/.m2/repository/com/demo/url-shortener/0.0.1-SNAPSHOT/url-shortener-0.0.1-SNAPSHOT.jar
cd
into0.0.1-SNAPSHOT
directory and runjava -jar url-shortener-0.0.1-SNAPSHOT.jar
or runjava -jar /absolute/path/to/.jar
This project is not for production use, it is only for demonstration purposes.
** What is important **
- Catching the
shortenUrl
is done here with use ofjavax.servlet.Filer
, better solution would be to move evry API endpoint under/v1/rest-of-the-path
and create@RestController
that would catchregex
expression for anything under/
of depth1
- This implementation uses
H2
in-memory database. For this purposes it is enough, but you would want to useNoSQL
database to save all those mappings oflongUrl: shortUrl
- Currently generation of short
URL
is based on uniqueint
in database, this could be removed from DB to save some space - This implementation is creating short
URL
s using base62 technique Url shortening Wiki - Charachters
_
and/
are removed to gain more friendlyURL
s
This is my first project using Java and Spring Boot, so I'm positive that some things are not best practices from Spring/Java standpoint, keep that in mind if you are looking to copy some of the code
- Add unit tests for
UrlService
- Refactor documentation for
IUrlService
HTTP STATUS | Meaning |
---|---|
200 - OK | Everything worked as expected. |
201 - Created | The request has succeeded and has led to the creation of a resource. |
400 - Bad Request | The request was unacceptable, often due to missing a required parameter. |
401 - Unauthorized | No valid Basic auth was provided. |
404 - Not Found | The requested resource doesn't exist. |
422 - Unprocessable Entity | server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions. |
500 ... - Server Errors | Something went wrong with application. These are rare but you never know. |
Parameters:
@Size(max = 255)
String accountId
Request example:
curl --header "Content-Type: application/json" --request POST --data '{"accountId":"xyz"}' http://localhost:8080/account
Response:
{
"success": true,
"description": "Your account is successfully opened",
"password": "GNCaaavL" // pasword is encoded so if you don't write it down you will have to create new account
}
Parameters:
String url
int redirectType // optional parameter, possible values 301 | 302, and 302 is default value
Request example:
curl --header "Content-Type: application/json" --request POST --user name:password --data '{"url":"https://google.com"}' http://localhost:8080/register
curl --header "Content-Type: application/json" --request POST --user name:password --data '{"url":"https://google.com", "redirectType":301}' http://localhost:8080/register
Response:
{
"shortUrl": "http://localhost:8080/d"
}
Parameters:
// No parameters in data, access endpoint only using GET and URL with basic autorization
Request example:
curl --request GET --user name:password http://localhost:8080/statistic/name
Response:
{
"https://www.kaggle.com/": 1,
"https://www.pmfst.unist.hr/": 0,
"https://news.ycombinator.com/": 2
}
For example:
curl -L http://localhost:8080/c
-L
argument is important as cURL
won't follow redirect otherwise