Yet another URL shortener. Written in Go with SQLite backend.
1.6K
shortyShorty is yet another URL shortener application. Basically, you inform a URL plus a arbitrary string to Shorty, and then based on the short string Shorty will redirect your request to the original URL. Simple like that.
Install shorty using go get:
go get -x -u github.com/otaviof/shorty/cmd/shorty
And then:
shorty --database-file /var/tmp/shorty.sqlite
Images are stored on Docker-Hub, tags using a version
number are stable releases, and master is built from latest commits on branch.
For example, use:
docker run --publish "8000:8000" otaviof/shorty:latest
Alternatively, you can share a local volume to persist its database file:
docker run --publish "8000:8000" --volume "<VOLUME_PATH>:/var/lib/shorty" otaviof/shorty:latest
The following example shows how to add a short link to a URL via curl.
curl -X POST http://127.0.0.1:8000/shorty/shorty -d '{ "url": "https://github.com/otaviof/shorty" }'
As output, you should see:
{
"short": "shorty",
"url": "https://github.com/otaviof/shorty",
"created_at": 1553442790
}
And then, to curl with redirect to original URL:
curl -L http://127.0.0.1:8000/shorty/shorty
Application configuration can also be set via environment variables, or command-line parameters,
where the environment overwrites command-line. So for instance, if you want to set --address
option, you can export SHORTY_ADDRESS in environment. By setting the prefix as application name
(SHORTY_), followed by option name, in this case ADDRESS, split by underscore and all capitals.
The basic usage is:
shorty [flags]
Where the following flags are available:
--address: address and port to listen on;--database-file: database file path;--idle-timeout: idle connection timeout, in seconds;--read-timeout: read timeout, in second;--write-timeout: write timeout, in seconds;--sqlite-flags: connection string SQLite flags;--help: shows command-line help message;Over the endpoint /metrics this application offers Prometheus compatible metrics, those are
collected using OpenCensus:
$ curl -s http://127.0.0.1:8000/metrics |tail
opencensus_io_http_server_response_bytes_bucket{le="6.7108864e+07"} 4
opencensus_io_http_server_response_bytes_bucket{le="2.68435456e+08"} 4
opencensus_io_http_server_response_bytes_bucket{le="1.073741824e+09"} 4
opencensus_io_http_server_response_bytes_bucket{le="4.294967296e+09"} 4
opencensus_io_http_server_response_bytes_bucket{le="+Inf"} 4
opencensus_io_http_server_response_bytes_sum 3845
opencensus_io_http_server_response_bytes_count 4
# HELP opencensus_io_http_server_response_count_by_status_code Server response count by status code
# TYPE opencensus_io_http_server_response_count_by_status_code counter
opencensus_io_http_server_response_count_by_status_code{http_status="200"} 4
You can find documentation about HTTP metrics on OpenCensus documentation. Furthermore, Shorty is integrated with OCSQL, you can read recorded metrics documentation here.
Backend storage is currently using SQLite. This application creates a table that's able to store
the records from the REST interface, and does not allow repetition of short strings.
On command-line or environment you can specify the location of the database file, by default data is
located on /var/lib/shorty directory.
Following a description of directory and files important on how this project is organized.
| Folder | Role | Description |
|---|---|---|
assets | doc | Contains project logo |
cmd/shorty | cmd | Command line entrypoint |
pkg/shorty | pkg | Shorty package |
test/e2e | tst | Integration tests |
vendor | build | Vendor directory, dependencies |
Regarding the relevant files:
| File | Role | Description |
|---|---|---|
.goreleaser.yml | build | Build the project and organize a release on Github |
.travis.yml | CI | Drives Travis-CI actions |
Dockerfile | build | Docker image manifest |
Gopkg.* | build | Dep, vendor management |
Makefile | build | Automation of actions against project |
version | version | Carry project version in a text file |
Unit tests are located on pkg/shorty directory, and using the suffix _test. To run unit-tests:
make test-unit
Integration tests are on test/e2e directory. To run integration-tests:
make test-e2e
And all tests can be triggered with:
make test
Content type
Image
Digest
Size
15.3 MB
Last updated
almost 6 years ago
docker pull otaviof/shorty