mpngin

A simple and fast URL shortener with built in stats redis url-shortener
0.5.5 released

MPNG.IN Build Status

A simple and fast URL shortener with built in stats. Requires Redis and assumes it is local for lowest possible latency.

Installation

Copy env.example to .env and set the values. For the SECRET_TOKEN, try Random.new.hex(32).

NOTE: If building in release mode for production, ensure KEMAL_ENV is uncommented in .env and set to production.

Then run:

shards install
crystal run src/mpngin.cr

For production, make a release build:

# Assumes .env file is correctly filled out, including KEMAL_ENV=production
crystal build --release src/mpngin.cr

Usage

Creating an short URL and getting the stats require an application key first.

  1. Create an application first:
➜ curl -X "POST" "http://localhost:3000/application" -H "Authorization: bearer <SECRET_TOKEN_HERE>"

# New app key response
d48655ff210c3e9e4ed8f6ad4f1923a3
  1. Use the app key in place of the SECRET_TOKEN to make shortened URLs, passing in the final redirect to URL as a application/x-form-urlencoded body param named redirect_url:
➜ curl -X "POST" "http://localhost:3000/" \
     -H 'Authorization: bearer d48655ff210c3e9e4ed8f6ad4f1923a3' \
     -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
     --data-urlencode "redirect_url=https://www.nintendo.com"
     
# Your shiny new shortened redirect URL
http://localhost:3000/541450
  1. Get number of requests for this shortened URL:
➜ curl "http://localhost:3000/541450/stats" \
     -H 'Authorization: bearer d48655ff210c3e9e4ed8f6ad4f1923a3' \
     -H 'Content-Type: text/plain; charset=utf-8'

# Request count response
1337

Testing

For spec tests:

KEMAL_ENV=test crystal spec

For coverage:

KEMAL_ENV=test ./bin/crystal-coverage spec/mpngin_spec.cr
open coverage/index.html

Static Analysis

To run static analysis checks, use locally installed Ameba (comes with shards install):

./bin/ameba

Contributing

  1. Fork it ( https://github.com/thewalkingtoast/mpngin/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

mpngin:
  github: thewalkingtoast/mpngin
  version: ~> 0.5.5
License MIT
Crystal 0.31.0

Authors

Dependencies 3

  • dotenv
    {'github' => 'gdotdesign/cr-dotenv'}
  • kemal
    {'github' => 'kemalcr/kemal'}
  • redis
    {'github' => 'stefanwille/crystal-redis'}

Development Dependencies 4

  • ameba
    {'github' => 'veelenga/ameba'}
  • coverage
    {'github' => 'anykeyh/crystal-coverage'}
  • icr master
    {'branch' => 'master', 'github' => 'crystal-community/icr'}
  • spec-kemal
    {'github' => 'kemalcr/spec-kemal'}

Dependents 0

Last synced .
search fire star recently