Skip to content

Commit 0285f1e

Browse files
committed
Initial Commit
0 parents  commit 0285f1e

File tree

12 files changed

+383
-0
lines changed

12 files changed

+383
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.env

api/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# stage 1 to build the app
2+
FROM golang:alpine as builder
3+
4+
RUN mkdir /build
5+
6+
ADD . /build/
7+
8+
WORKDIR /build
9+
10+
RUN go build -o main .
11+
12+
# stage 2 deploys the app built in stage 1
13+
FROM alpine
14+
15+
RUN adduser -S -D -H -h /app appuser
16+
17+
USER appuser
18+
19+
COPY . /app
20+
21+
COPY --from=builder /build/main /app/
22+
23+
WORKDIR /app
24+
25+
EXPOSE 3000
26+
27+
CMD ["./main"]

api/database/database.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"github.com/go-redis/redis/v8"
6+
"os"
7+
)
8+
9+
var Ctx = context.Background()
10+
11+
func CreateClient(dbNo int) *redis.Client {
12+
rdb := redis.NewClient(&redis.Options{
13+
Addr: os.Getenv("DB_HOST"),
14+
Password: os.Getenv("DB_PASSWORD"),
15+
DB: dbNo,
16+
})
17+
return rdb
18+
}

api/go.mod

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module url-shortner-fiber-redis
2+
3+
go 1.20
4+
5+
require (
6+
github.com/andybalholm/brotli v1.0.5 // indirect
7+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
8+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
9+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
10+
github.com/go-redis/redis/v8 v8.11.5 // indirect
11+
github.com/gofiber/fiber/v2 v2.47.0 // indirect
12+
github.com/google/uuid v1.3.0 // indirect
13+
github.com/joho/godotenv v1.5.1 // indirect
14+
github.com/klauspost/compress v1.16.3 // indirect
15+
github.com/mattn/go-colorable v0.1.13 // indirect
16+
github.com/mattn/go-isatty v0.0.19 // indirect
17+
github.com/mattn/go-runewidth v0.0.14 // indirect
18+
github.com/philhofer/fwd v1.1.2 // indirect
19+
github.com/rivo/uniseg v0.2.0 // indirect
20+
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
21+
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
22+
github.com/tinylib/msgp v1.1.8 // indirect
23+
github.com/valyala/bytebufferpool v1.0.0 // indirect
24+
github.com/valyala/fasthttp v1.47.0 // indirect
25+
github.com/valyala/tcplisten v1.0.0 // indirect
26+
golang.org/x/sys v0.9.0 // indirect
27+
)

api/go.sum

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
2+
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
3+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
4+
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
5+
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
6+
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
8+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
9+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
10+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
11+
github.com/gofiber/fiber/v2 v2.47.0 h1:EN5lHVCc+Pyqh5OEsk8fzRiifgwpbrP0rulQ4iNf3fs=
12+
github.com/gofiber/fiber/v2 v2.47.0/go.mod h1:mbFMVN1lQuzziTkkakgtKKdjfsXSw9BKR5lmcNksUoU=
13+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
14+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
16+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
17+
github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
18+
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
19+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
20+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
21+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
22+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
23+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
24+
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
25+
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
26+
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
27+
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
28+
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
29+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
30+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
31+
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4=
32+
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8=
33+
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
34+
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
35+
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
36+
github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw=
37+
github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0=
38+
github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
39+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
40+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
41+
github.com/valyala/fasthttp v1.47.0 h1:y7moDoxYzMooFpT5aHgNgVOQDrS3qlkfiP9mDtGGK9c=
42+
github.com/valyala/fasthttp v1.47.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
43+
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
44+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
45+
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
46+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
47+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
48+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
49+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
50+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
51+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
52+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
53+
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
54+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
55+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
56+
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
57+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
58+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
59+
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
60+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
61+
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
62+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
63+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
64+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
65+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
66+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
67+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
68+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
69+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
70+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
71+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
72+
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
73+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
74+
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
75+
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
76+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
77+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
78+
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
79+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
80+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
81+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
82+
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
83+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
84+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
85+
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
86+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
87+
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
88+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
89+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
90+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

api/helpers/helpers.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package helpers
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
func CheckDomainError(url string) bool {
9+
10+
if url == os.Getenv("APP_URL") {
11+
return false
12+
}
13+
newURL := strings.Replace(url, "http://", "", 1)
14+
newURL = strings.Replace(newURL, "https://", "", 1)
15+
newURL = strings.Replace(newURL, "https://", "", 1)
16+
newURL = strings.Replace(newURL, "www.", "", 1)
17+
newURL = strings.Split(newURL, "/")[0]
18+
if newURL == os.Getenv("APP_URL") {
19+
return false
20+
}
21+
return true
22+
}
23+
24+
func EnforceHTTP(url string) string {
25+
if url[:4] != "http" {
26+
return "http://" + url
27+
}
28+
return url
29+
}

api/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gofiber/fiber/v2"
6+
"github.com/gofiber/fiber/v2/middleware/logger"
7+
"github.com/joho/godotenv"
8+
"log"
9+
"os"
10+
"url-shortner-fiber-redis/routes"
11+
)
12+
13+
func setupRoutes(app *fiber.App) {
14+
app.Get("/:url", routes.ResolveURL)
15+
app.Post("/api/v1", routes.ShortenURL)
16+
}
17+
18+
func main() {
19+
20+
err := godotenv.Load()
21+
if err != nil {
22+
fmt.Println(err)
23+
}
24+
25+
app := fiber.New()
26+
27+
app.Use(logger.New())
28+
setupRoutes(app)
29+
30+
log.Fatal(app.Listen(os.Getenv("APP_PORT")))
31+
}

api/routes/resolve.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package routes
2+
3+
import (
4+
"github.com/go-redis/redis/v8"
5+
"github.com/gofiber/fiber/v2"
6+
"url-shortner-fiber-redis/database"
7+
)
8+
9+
func ResolveURL(c *fiber.Ctx) error {
10+
url := c.Params("url")
11+
12+
r := database.CreateClient(0)
13+
defer r.Close()
14+
15+
value, err := r.Get(database.Ctx, url).Result()
16+
if err == redis.Nil {
17+
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
18+
"error": "Invalid short URL",
19+
})
20+
} else if err != nil {
21+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
22+
"error": "Can not connect to DB",
23+
})
24+
}
25+
rInr := database.CreateClient(1)
26+
defer rInr.Close()
27+
_ = rInr.Incr(database.Ctx, "counter")
28+
return c.Redirect(value, 301)
29+
}

api/routes/shorten.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package routes
2+
3+
import (
4+
"github.com/asaskevich/govalidator"
5+
"github.com/go-redis/redis/v8"
6+
"github.com/gofiber/fiber/v2"
7+
"github.com/google/uuid"
8+
"os"
9+
"strconv"
10+
"time"
11+
"url-shortner-fiber-redis/database"
12+
"url-shortner-fiber-redis/helpers"
13+
)
14+
15+
type request struct {
16+
URL string `json:"url"`
17+
CustomShort string `json:"customShort"`
18+
Expiry time.Duration `json:"expiry"`
19+
}
20+
21+
type response struct {
22+
URL string `json:"url"`
23+
CustomShort string `json:"customShort"`
24+
Expiry time.Duration `json:"expiry"`
25+
XRateRemaining int `json:"x_rate_remaining"`
26+
XRateLimitReset time.Duration `json:"x_rate_limit_reset"`
27+
}
28+
29+
func ShortenURL(c *fiber.Ctx) error {
30+
body := new(request)
31+
32+
err := c.BodyParser(&body)
33+
if err != nil {
34+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
35+
"error": "Cannot Parse Json",
36+
})
37+
}
38+
39+
//rate limiting
40+
41+
r2 := database.CreateClient(1)
42+
defer r2.Close()
43+
val, err := r2.Get(database.Ctx, c.IP()).Result()
44+
if err == redis.Nil {
45+
_ = r2.Set(database.Ctx, c.IP(), os.Getenv("API_QUOTA"), 30*60*time.Second).Err()
46+
} else {
47+
valInt, _ := strconv.Atoi(val)
48+
if valInt <= 0 {
49+
limit, _ := r2.TTL(database.Ctx, c.IP()).Result()
50+
return c.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{
51+
"error": "Rate limit exceeded",
52+
"rate_limit_reset": limit / time.Nanosecond / time.Minute,
53+
})
54+
}
55+
}
56+
57+
if !govalidator.IsURL(body.URL) {
58+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
59+
"error": "Invalid URL",
60+
})
61+
}
62+
63+
if !helpers.CheckDomainError(body.URL) {
64+
return c.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{
65+
"error": "",
66+
})
67+
}
68+
69+
body.URL = helpers.EnforceHTTP(body.URL)
70+
71+
var id string
72+
if body.CustomShort == "" {
73+
id = uuid.New().String()[:6]
74+
} else {
75+
id = body.CustomShort
76+
}
77+
r := database.CreateClient(0)
78+
defer r.Close()
79+
80+
val, _ = r.Get(database.Ctx, id).Result()
81+
if val != "" {
82+
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
83+
"error": "Custom URL is already in use",
84+
})
85+
}
86+
if body.Expiry == 0 {
87+
body.Expiry = 24
88+
}
89+
90+
err = r.Set(database.Ctx, id, body.URL, body.Expiry).Err()
91+
if err != nil {
92+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
93+
"error": "Unable to connect to Server",
94+
})
95+
}
96+
97+
resp := response{
98+
URL: body.URL,
99+
CustomShort: "",
100+
Expiry: body.Expiry,
101+
XRateRemaining: 10,
102+
XRateLimitReset: 30,
103+
}
104+
r2.Decr(database.Ctx, c.IP())
105+
106+
val, err = r2.Get(database.Ctx, c.IP()).Result()
107+
resp.XRateRemaining, _ = strconv.Atoi(val)
108+
ttl, _ := r2.TTL(database.Ctx, c.IP()).Result()
109+
resp.XRateLimitReset = ttl / time.Nanosecond / time.Minute
110+
resp.CustomShort = os.Getenv("APP_URL") + "/" + id
111+
return c.Status(fiber.StatusOK).JSON(resp)
112+
}

data/dump.rdb

89 Bytes
Binary file not shown.

db/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM redis:alpine
2+
3+
EXPOSE 6379

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "3"
2+
3+
services:
4+
api:
5+
build: api
6+
ports:
7+
- "3000:3000"
8+
depends_on:
9+
- db
10+
db:
11+
build: db
12+
ports:
13+
- "6379:6379"
14+
volumes:
15+
- ./data:/data

0 commit comments

Comments
 (0)