Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: api v2 init #2832

Merged
merged 24 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ tasks:
cmds:
- task: seed
- pnpm test:integration


generate-sql:
dir: internal/db
cmds:
- pnpm drizzle-kit generate --dialect=mysql
4 changes: 2 additions & 2 deletions apps/agent/pkg/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@
"tags": ["ratelimit"]
}
},
"/ratelimit.v1.RatelimitService/Ratelimit": {
"/v1/ratelimit.limit": {
"post": {
"operationId": "ratelimit.v1.ratelimit",
"operationId": "v1.ratelimit.limit",
"requestBody": {
"content": {
"application/json": {
Expand Down
17 changes: 17 additions & 0 deletions deployment/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ services:
ports:
- 3900:3900

apiv2:
deploy:
replicas: 1
endpoint_mode: vip

command: [ "api", "--config", "config.docker.json"]
build:
context: ../go
dockerfile: ./Dockerfile
depends_on:
- mysql
- clickhouse
environment:
Comment on lines +34 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add health checks and resource limits.

The service should include health checks and resource constraints for better reliability and resource management.

 apiv2:
   deploy:
     replicas: 1
     endpoint_mode: vip
+    resources:
+      limits:
+        cpus: '1'
+        memory: 512M
+      reservations:
+        cpus: '0.25'
+        memory: 128M
+  healthcheck:
+    test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
+    interval: 30s
+    timeout: 10s
+    retries: 3
+    start_period: 40s
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
apiv2:
deploy:
replicas: 1
endpoint_mode: vip
command: [ "api", "--config", "config.docker.json"]
build:
context: ../go
dockerfile: ./Dockerfile
depends_on:
- mysql
- clickhouse
environment:
apiv2:
deploy:
replicas: 1
endpoint_mode: vip
resources:
limits:
cpus: '1'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
command: [ "api", "--config", "config.docker.json"]
build:
context: ../go
dockerfile: ./Dockerfile
depends_on:
- mysql
- clickhouse
environment:

PORT: 8080
DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey"
CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"
Comment on lines +47 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use secrets management for sensitive information.

Hard-coded credentials in environment variables pose a security risk.

Consider using Docker secrets or environment files:

 environment:
   PORT: 8080
-  DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey"
-  CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"
+  DATABASE_PRIMARY_DSN: "${DATABASE_PRIMARY_DSN}"
+  CLICKHOUSE_URL: "${CLICKHOUSE_URL}"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
PORT: 8080
DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey"
CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"
PORT: 8080
DATABASE_PRIMARY_DSN: "${DATABASE_PRIMARY_DSN}"
CLICKHOUSE_URL: "${CLICKHOUSE_URL}"


agent:
deploy:
replicas: 3
Expand Down
351 changes: 351 additions & 0 deletions go/.golangci.yaml

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.23-alpine AS builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix invalid Go version in base image.

The specified Go version 1.23 doesn't exist. The latest stable version is 1.21.5.

-FROM golang:1.23-alpine AS builder
+FROM golang:1.21.5-alpine AS builder
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
FROM golang:1.23-alpine AS builder
FROM golang:1.21.5-alpine AS builder




WORKDIR /go/src/github.com/unkeyed/unkey/go
COPY go.sum go.mod ./
RUN go mod download

COPY . .
ARG VERSION
RUN go build -o bin/unkey -ldflags "-X 'github.com/unkeyed/unkey/go/pkg/version.Version=${VERSION}'" ./cmd/main.go

FROM golang:1.23-alpine
WORKDIR /usr/local/bin
COPY --from=builder /go/src/github.com/unkeyed/unkey/go/bin/unkey .
COPY --from=builder /go/src/github.com/unkeyed/unkey/go/config.docker.json .

ENTRYPOINT [ "/usr/local/bin/unkey"]
22 changes: 22 additions & 0 deletions go/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

tasks:
install:
cmd:
go mod tidy
fmt:
cmds:
- go fmt ./...
- task: lint
test:
cmds:
- go test -cover -json -failfast ./... | tparse -all -progress

build:
cmds:
- go build -o unkey ./cmd/main.go


lint:
cmds:
- golangci-lint run
9 changes: 9 additions & 0 deletions go/api/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/HEAD/configuration-schema.json
package: api
output: ./gen.go
generate:
models: true


output-options:
nullable-type: true
Loading
Loading