Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vividvilla/lil

Simple URL shortener built with Go
https://github.com/vividvilla/lil

Last synced: 11 days ago
JSON representation

Simple URL shortener built with Go

Awesome Lists containing this project

README

        

# Lil

Simple URL shortener based on Go.

## API

### Redirect

`GET /` - Redirects to target URL.

### Redirect page

`GET /p/` - HTML page which redirects to target url. Page additionally renders
OpenGraph tags specified while creating short url. Useful for sharing
previewable link in social media sites.

### Create a short url

`POST /api/new` - Create a random short url for given url, accepts JSON post body.

#### Params

- `url` - Target url to redirect.
- `title` - Page title used in paged redirect.
- `og_tags` - List of Open graph tags to be used in paged redirect.
- `property` - Open graph property tag
- `content` - Open graph content tag

#### Response

Response returns short uri ID, redirect url and page redirect url.

```json
{
"data": {
"id": "",
"url": "http://localhost/",
"page": "http://localhost/p/"
}
}
```

### Get redirect links

`GET /api/` - Get redirect links for given short uri.

#### Response

Response returns short uri ID, redirect url and page redirect url.

```json
{
"data": {
"id": "",
"url": "http://localhost/",
"page": "http://localhost/p/"
}
}
```

### Delete a short url

`DELETE /api/` - Delete a give short url.

#### Response

```json
{
"data": true
}
```

## Examples

### Create a short url

```
# Request
curl -X "POST" "http://localhost:8085/api/new" \
-H 'Content-Type: text/plain; charset=utf-8' \
-d $'{
"url": "https://zerodha.com",
"title": "Zerodha",
"og_tags": [
{"property": "og:image", "content": "https://zerodha.com/static/images/kite-dashboard.png"}
]
}'

# Response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
"data": {
"id": "72p9abOM",
"url": "http://localhost:8085/72p9abOM",
"page": "http://localhost:8085/p/72p9abOM"
}
}
```

### Direct redirect

```
# Request
curl http://localhost:8085/72p9abOM

# Response
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://zerodha.com

Moved Permanently.
```

### Page redirect

```
# Page redirect which renders additional open graph data.
curl http://localhost:8085/p/72p9abOM

# Response
HTTP/1.1 200 OK
Date: Thu, 18 Jul 2019 08:14:03 GMT

Zerodha


window.location.replace("https:\/\/zerodha.com");

```

### Get redirect links

```
# Request
curl "http://localhost:8085/api/72p9abOM"

# Response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
"data": {
"id": "72p9abOM",
"url": "http://localhost:8085/72p9abOM",
"page": "http://localhost:8085/p/72p9abOM"
}
}
```

# Delete a short url

```
# Request
curl -X "DELETE" "http://localhost:8085/api/72p9abOM"

# Response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
"data": true
}
```

## Backend store

Currently Redis is the only backend store available but new stores can be easily
added by implementing [store interface](store/store.go), for example here is the
[Redis store implementation](store/redis/store.go).

## TODO

- Basic auth for create and delete APIs. Currently this can be implemented
behind reverse proxy like Nginx or API gateways like Kong, AWS API gateway.
- Custom path for short URLs instead of random generated paths.
- Redirect stats.