Skip to content

Commit

Permalink
build: refactor dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
thuongtruong109 committed Aug 10, 2024
1 parent ae2fe39 commit 32324bb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 18 deletions.
2 changes: 2 additions & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env.example
.env.dev
fly.toml
Dockerfile
Dockerfile.dev
10 changes: 6 additions & 4 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
FROM golang:1.20-alpine AS development
FROM golang:1.22-alpine AS development

RUN mkdir /app

ADD go.mod go.sum /app/

WORKDIR /app

RUN go mod tidy
RUN go install golang.org/x/tools/gopls@latest

RUN go mod download

COPY . /app

RUN GOOS=linux go build -ldflags="-s -w" -o main main.go

FROM alpine AS production

LABEL "maintainer"="thuongtruong1009"
LABEL "maintainer"="thuongtruong109"
LABEL "org.opencontainers.image.authors"="Tran Nguyen Thuong Truong <thuongtruongofficial@gmail.com>"
LABEL "org.opencontainers.image.version"="1.0"
LABEL "org.opencontainers.image.description"="Official image of short1url api"
LABEL "org.opencontainers.image.description"="Official image of onelink api"
LABEL "org.opencontainers.image.licenses"="MIT"
LABEL "org.opencontainers.image.source"="https://github.com/thuongtruong109/onelink/api"
LABEL "org.opencontainers.image.documentation"="https://github.com/thuongtruong109/onelink/blob/main/README.md"
Expand Down
10 changes: 6 additions & 4 deletions api/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"os"

"github.com/go-redis/redis/v8"
"github.com/thuongtruong109/gouse/connection"
)

func CreateClient(dbNo int) *redis.Client {
addr := os.Getenv("DB_ADDR")
pass := os.Getenv("DB_PASS")
rdb := connection.Redis(addr, pass, dbNo)
rdb := redis.NewClient(&redis.Options{
Addr: os.Getenv("DB_ADDR"),
Username: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASS"),
DB: dbNo,
})

return rdb
}
15 changes: 10 additions & 5 deletions api/fly.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# fly.toml app configuration file generated for short1url-api on 2023-10-21T22:51:32+07:00
# fly.toml app configuration file generated for onelink on 2024-08-10T20:06:17+07:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "short1url-api"
primary_region = "sin"
app = 'onelink'
primary_region = 'sin'

[build]

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
processes = ['app']

[[vm]]
size = 'shared-cpu-1x'
4 changes: 1 addition & 3 deletions api/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/thuongtruong109/onelink

go 1.21

toolchain go1.22.3
go 1.22

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func main() {
}))

app.Use(cors.New(cors.Config{
// AllowOrigins: "*",
AllowOrigins: "http://localhost:3000, http://localhost:3001, https://onelink.vercel.app",
AllowOrigins: "*",
// AllowOrigins: "http://localhost:3000, http://localhost:3001, https://onelink.vercel.app",
AllowMethods: strings.Join([]string{
fiber.MethodGet,
fiber.MethodPost,
Expand Down
5 changes: 5 additions & 0 deletions api/routes/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ func ShortenURL(c *fiber.Ctx) error {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": helpers.CANNOT_PARSE_ENV})
}

if body.IP == "" {
body.IP = c.IP()
}

r2 := database.CreateClient(1)
defer r2.Close()

val, err := r2.Get(helpers.Ctx, c.IP()).Result()
if err == redis.Nil {
_ = r2.Set(helpers.Ctx, c.IP(), os.Getenv("API_QUOTA"), 1*time.Second).Err()
Expand Down

0 comments on commit 32324bb

Please sign in to comment.