Skip to content

Commit

Permalink
Add more details in README.md & add docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyiyo committed Apr 5, 2022
1 parent 17d1c3f commit 1ba673f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 72 deletions.
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
# URL Shortener

About technical details, please see [details](details.md).
A URL Shortener.

## 3rd Part Library Usage

- Web Engineering
- Use gin for web framework.
- Use cors for cross-domain requests. It will let you make requests from any domain.
- Config file
- Use viper to parse the config file, to make the codes more flexible.
- Database
- Use sqlite3 for database.
- Cache Storage
- Use Redis as cache storage.
- If the Short ID does not exist in the DB, it will add a miss to the Redis.
- If the Short ID exists in the DB, it will add as hit to the Redis.
- Limit IP Address.

For the full list, please check on [go.mod](go.mod).

## Features

- Init
- Check if the table or file exists. If not, create it.
- Loading .env file.
- Add Short Link
- HTTP POST with JSON body.
- Check if the input (url, expireAt) is valid.
- `url`: require a valid URL. Use `url.ParseRequestURI` to check.
- `expireAt`: require a valid date. Use `tools.ConvertTimetoUnix` to check. (btw, It will be converted to unix timestamp.)
- Redirect
- HTTP GET with URL query.
- It will query the database to get the origin URL. (btw, It will also add a hit to the Redis.)

### Function List

```go
func AddURL(c *gin.Context) // Request for a Short Link
func RedirectURL(c *gin.Context) // Redirect to the origin link

func CreateTable() // Init Table (If a table or file is not found, it will create one.)
func AddData(ShortID string, Link string, ExpireAt int64) // Prepend to DB
func QueryData(ID string) (bool, string) // Query the Origin Link
func ErrCheck(err error) // Check Error

func RandomString(length int) string // Generate a 5 char string
func CheckLinkValid(Link string) bool // Check if Link valid
func ConvertTimetoUnix(date string) (bool, int64) // Convert the time format from RFC 3339 UTC to Unix
func CheckIPAddress(ip string) bool // Check if IP Address valid
```

## Install

- Download the libraries
`go mod download`

- Test the server
`go run main.go`

- Build
`go build -o url_shortener`

## Usage

Download the [release version](https://github.com/steveyiyo/URL-Shortener/releases) from here.

Upload URL API:

1. "url" require a valid URL. (Eg: https://google.com/)
Expand Down Expand Up @@ -32,4 +93,16 @@ Redirect URL API:
```
curl -L -X GET http://localhost/<url_id> => REDIRECT to original URL # HTTP 301
```
```
## Run with the docker?
- Download the docker image
`docker pull steveyiyo/url-shortener:latest`
- Run the docker image
`sudo docker run -p 0.0.0.0:7600:7600 steveyiyo/url-shortener`
## TODO
- [] docker-compose.yaml
59 changes: 0 additions & 59 deletions details.md

This file was deleted.

11 changes: 11 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'

services:
cache:
image: redis:latest
restart: always
app:
image: steveyiyo/url-shortener:latest
restart: always
ports:
- 0.0.0.0:7600:7600
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang:1.18 as builder
WORKDIR /app
COPY . .
RUN go build -o app -buildvcs=false
RUN go build -o app

FROM scratch
COPY --from=builder /app/app /app
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/go-redis/redis/v8"
"github.com/steveyiyo/url-shortener/package/tools"
"github.com/steveyiyo/url-shortener/pkg/tools"
)

var Redis *redis.Client
Expand Down
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

_ "github.com/mattn/go-sqlite3"
"github.com/steveyiyo/url-shortener/package/tools"
"github.com/steveyiyo/url-shortener/pkg/tools"
)

var db *sql.DB
Expand Down
7 changes: 6 additions & 1 deletion internal/webserver/webserver.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package webserver

import (
"fmt"
"log"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/steveyiyo/url-shortener/internal/cache"
"github.com/steveyiyo/url-shortener/internal/database"
"github.com/steveyiyo/url-shortener/package/tools"
"github.com/steveyiyo/url-shortener/pkg/tools"
)

// Predefined variable and struct
Expand Down Expand Up @@ -53,6 +56,8 @@ func Init(Init_Listen, Init_Host, Init_Port, Init_URL string) {
route.GET("/:ShortID", RedirectURL)
route.POST("/api/v1/urls", AddURL)

log.Println(fmt.Sprintf("Server is running on %s:%s", Listen, Port))

route.Run(Listen + ":" + Port)
}

Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"github.com/steveyiyo/url-shortener/internal/cache"
"github.com/steveyiyo/url-shortener/internal/database"
"github.com/steveyiyo/url-shortener/internal/webserver"
"github.com/steveyiyo/url-shortener/package/tools"
"github.com/steveyiyo/url-shortener/pkg/tools"
)

func main() {

// Load .env
err := godotenv.Load()
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions package/utils/utils.go

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package utils

const (
RFC3339UTC = "2006-01-02T15:04:05Z"
)

0 comments on commit 1ba673f

Please sign in to comment.