Shortgun provides REST API for URL shortener service building. It includes account creation, authorization, CRUD operations for URL shortcuts and additional features such as saving messages from users.
This project built for learning purposes. Do not use it for business-critical or other essential tasks.
- Latest LTS or current installation of Node.js (tested on 13.8.0)
- MongoDB server up and running
- Nginx or another web-server up and running for serving page preview thumbnails and proxy requests
- Git installed
- Clone this repository to local machine
git clone https://github.com/konrad-molitor/shortgun.git
- Navigate to
shortgun
directory
cd shortgun
- Perform dependecies installation
npm install
- Build project
npm run build
- Navigate to
build
directory
cd build
-
Create
.env
file..env
file shoult contain following information:- MongoDB connection string (https://docs.mongodb.com/manual/reference/connection-string/)
DB_URL=<mongodb_connection_string>
- JsonWebToken secret key string
SECRET_KEY=<jwt_secret_key>
- Thumbnails directory path (make sure that user running this app has write permission to it)
THUMBNAILS_PATH=<thumbnails directory full path>
-
Configure your web server to host thumnails directory to
/images
path -
Configure your web server to proxy requests all requests to
/a
to desired port/URL -
Set your
process.env.PORT
variable to desired port (this also can be done using.env
file) -
Run
build/app.js
node app.js
I suggest using process management software such as pm2, IBM StrongLoop, Forever or systemd
API provides following routes:
/a/
signup
login
profile
item/<itemId>
message
add
/s/
<shortcut>
Route for creating a new user account
Parameters:
HTTP Verb:
POST
Headers:
Content-Type
required - must beapplication/json
Request body:
email
requiredString
- new user emailpassword
requiredString
- new user password
Returns:
HTTP Status:
409
- if user already exists (no data saved in DB)500
- Server error200
- New account created
Response body:
User already exists
- if user already existsError
- if server error occured<email>
- created user email
Login route
Parameters:
HTTP Verb:
POST
Headers:
Content-Type
required must beapplication/json
Request body:
email
requiredString
- user emailpassword
requiredString
- user password
Returns:
HTTP Status:
403
- Authentication failed500
- Server error200
- Login successful
Response body:
No such user.
- provided email does not exist in databaseInvalid password.
- provided password does not match stored in database for provided emailServer error
- server error occured during processing the request<token>
- JWT token
Profile data route
Parameters:
HTTP Verb:
GET
Headers:
X-Auth
required - must contain user token in formatBearer:<space><usertoken>
as this:Bearer: thisIsUser.Token
Returns:
HTTP Status:
400
- Incorrect request headers401
- Incorrect or missing token500
- Server error200
- OK
Response body:
Server error
- server error occured during processing the requestInvalid token.
- Token was processed, but no according records were found in databaseIncorrect request headers.
- Token can not be processedAuthorization required
- NoX-Auth
header present in request[Object]
JSON user object in following format:
{
_id: <userId>,
email: <userEmail>,
urls: [
{
longUrl: <longUrl>, //long URL string
shortUrl: <shortUrl>, //shortcut string
_id: <shortcutId>,
preview: <previewImage>, //only filename
pageTitle: <pageTitle> //original page title
}
]
}
Shortcut item managing route
Example:
/a/item/5e3ec5f2a986c875e30d088f
Parameters:
HTTP Verb:
DELETE
Headers:
X-Auth
required - must contain user token in formatBearer:<space><usertoken>
as this:Bearer: thisIsUser.Token
URL parameters:
<itemId>
required - shortcut item id.
Returns:
HTTP Status:
400
- Incorrect request headers or invalid item ID401
- Incorrect or missing token, attempt to delete another user shortcut404
- Shortcut ID not found500
- Server error200
- OK
Response body:
-
Server error
- server error occured during processing the request -
Only shortcut author can delete it.
- attempt to delete another user shortcut -
No such shortcut.
- shortcut with such ID not found -
Authorization required
- Token invalid or missing -
Invalid item ID
- provided item ID is invalid -
[Object]
- Deleted item in following format:{ longUrl: <longUrl>, //long URL string shortUrl: <shortUrl>, //shortcut string _id: <shortcutId>, preview: <previewImage>, //only filename pageTitle: <pageTitle> //original page title }
Route for direct messaging server owner
Parameters:
HTTP Verb:
POST
Headers:
Content-Type
required must beapplication/json
X-Auth
- may contain user token in formatBearer:<space><usertoken>
as this:Bearer: thisIsUser.Token
Request body:
email
requiredString
sender emailmessage
requiredString
message body
Returns:
HTTP Status:
204
- Message successfully created500
- Server error
Route for adding shortcuts
Parameters
HTTP Verb:
POST
Headers:
Content-Type
required must beapplication/json
X-Auth
required - must contain user token in formatBearer:<space><usertoken>
as this:Bearer: thisIsUser.Token
Request body:
longUrl
requiredString
- desired URL for creating shortcut
Returns:
HTTP Status:
401
- Authentication failed500
- Server error200
- OK
Response body:
-
[Object]
- created item in following format:{ longUrl: <longUrl>, //long URL string shortUrl: <shortUrl>, //shortcut string _id: <shortcutId>, preview: <previewImage>, //only filename pageTitle: <pageTitle> //original page title }
Redirect to original URL path
Parameters:
HTTP Verb:
GET
Returns:
HTTP Status:
302
- Found
Headers:
-
Location
- original URL location.Will redirect to
/
if shortcut not found
Essentially, shortgun operates only few content unit types: User
, Shortcut
and Message
.
User profile record
Fields:
email
required uniqueString
- user email. For authentication purposes only (for now)password
requiredString
- user password. Stored as hash with help ofbcrypt
library.urls
[ObjectID]
- an array containing user created shortcuts IDs
Shortcut item record
Fields:
longUrl
requiredString
- original URLshortUrl
uniqueString
- shortcut string (without actual URL, it's up to client to form full link URL)author
requiredObjectID
- shortcut author record IDpreview
String
- preview image filename (without path)pageTitle
String
- page title of original URL
Messages to site owner format
Fields:
email
requiredString
- sender emailmessage
requiredString
- message bodyuser
String
- not implemented yet
Previews are generated during shortcut creation. Hovewer, preview generation takes some time, so initially server will send shortcut item without preview
and pageTitle
fields. They will appear later, as preview generation process will finish.
Previews are stored in JPEG format with filename format <shortcutId>.jpg
. Original page screenshots are taken with viewport size 1280x720 px and then resized to 400x225 px.
PRs are welcome. For major changes, please open an issue first to discuss what you would like to change. https://www.firsttimersonly.com/ - friendly.
Unless stated elsewhere, file headers or otherwise, the license as stated in the LICENSE file.