From 32324bb45a23dbfde9a97d33b3fa9afba43dec06 Mon Sep 17 00:00:00 2001 From: truongtop1 Date: Sat, 10 Aug 2024 20:19:42 +0700 Subject: [PATCH] build: refactor dependencies --- api/.dockerignore | 2 ++ api/Dockerfile | 10 ++++++---- api/database/database.go | 10 ++++++---- api/fly.toml | 15 ++++++++++----- api/go.mod | 4 +--- api/helpers/{helpers.go => utils.go} | 0 api/main.go | 4 ++-- api/routes/shorten.go | 5 +++++ 8 files changed, 32 insertions(+), 18 deletions(-) rename api/helpers/{helpers.go => utils.go} (100%) diff --git a/api/.dockerignore b/api/.dockerignore index d154e01..5cf1296 100644 --- a/api/.dockerignore +++ b/api/.dockerignore @@ -1,3 +1,5 @@ .env.example +.env.dev fly.toml Dockerfile +Dockerfile.dev \ No newline at end of file diff --git a/api/Dockerfile b/api/Dockerfile index 9febf2d..e8d2dd3 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine AS development +FROM golang:1.22-alpine AS development RUN mkdir /app @@ -6,7 +6,9 @@ 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 @@ -14,10 +16,10 @@ 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 " 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" diff --git a/api/database/database.go b/api/database/database.go index 6f47a79..7d60060 100644 --- a/api/database/database.go +++ b/api/database/database.go @@ -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 } diff --git a/api/fly.toml b/api/fly.toml index 7cc8b3a..a291d7e 100644 --- a/api/fly.toml +++ b/api/fly.toml @@ -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' diff --git a/api/go.mod b/api/go.mod index 26d9d48..6969f8b 100644 --- a/api/go.mod +++ b/api/go.mod @@ -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 diff --git a/api/helpers/helpers.go b/api/helpers/utils.go similarity index 100% rename from api/helpers/helpers.go rename to api/helpers/utils.go diff --git a/api/main.go b/api/main.go index 0380ce5..2cba3fb 100644 --- a/api/main.go +++ b/api/main.go @@ -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, diff --git a/api/routes/shorten.go b/api/routes/shorten.go index 83b79e6..dbbaf5d 100644 --- a/api/routes/shorten.go +++ b/api/routes/shorten.go @@ -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()