https://github.com/dmuhs/url-shortener
A simple Go url shortener API built to learn Go
https://github.com/dmuhs/url-shortener
golang rest-api sqlite3 url-shortener
Last synced: about 1 year ago
JSON representation
A simple Go url shortener API built to learn Go
- Host: GitHub
- URL: https://github.com/dmuhs/url-shortener
- Owner: dmuhs
- Created: 2018-08-16T18:04:57.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-16T18:09:28.000Z (almost 8 years ago)
- Last Synced: 2025-02-08T19:24:44.264Z (over 1 year ago)
- Topics: golang, rest-api, sqlite3, url-shortener
- Language: Go
- Size: 4.62 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A simple Go URL Shortener
This is a simple URL shortening API that I built without any previous experience in Go. Built with ♥ in a solid day of hacking.
## Features
- centralized JSON configuration file (accessible as middleware by request handlers)
- same short code served for the same links submitted
- SQLite integration
## Deployment
Simply run `make build` to rebuild the application and automatically install required dependencies. To run it, just run `make run` in the project root directory.
## Endpoints
### `/`
#### GET
Display the number of rows in the table (i.e. the number of link to short code combinations)
#### POST
Submit a new URL to be shortened by the API. This will take a JSON object in the request body with the format:
```json
{
"url": "http://dmuhs.com"
}
```
and yield a response
```json
{
"LongURL": "http://dmuhs.com/",
"ShortURL": "http://my-shortener.com:8000/ByH81bL0"
}
```
### `/{code}`
#### GET
Delivers a 302 permanent redirect response towards the URL associated with the short code in the database.
## Configuration
Configuration is handled through a JSON file in `url-shortener/config.json`.
```json
{
"SQLitePath": "./urls.db",
"Port": 8000,
"ShortCodeLength": 8,
"Domain": "http://my-shortener.com",
"Public": true
}
```
Here the `Public` key toggles whether the service is accessible from hosts other than `localhost`. The `shortCodeLength` property defines the length of the generated short codes, which are randomly chosen from the charset `a-zA-Z0-9`. The `Domain` property decides which domain is used by the shortener's response, e.g. `http://my-shortener.com:8000/ByH81bL0` for the above example configuration. To be able to directly follow links in a local deployment, it can also be set to `http://localhost`.