Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 20 additions & 4 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ name: CD
on:
push:
tags: ['v*']
workflow_dispatch:
inputs:
version:
description: 'Image tag (e.g. dev, v0.1.0)'
required: true
default: 'dev'

env:
REGISTRY: ghcr.io/layfz/maas-cloud-api
REGISTRY: ghcr.io/maas-cloud-api

jobs:
build-push:
Expand All @@ -15,12 +21,17 @@ jobs:
packages: write
strategy:
matrix:
service: [auth, core, billing, syncer, adapter]
service: [auth, core]
steps:
- uses: actions/checkout@v4

- name: Set version
run: echo "VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
else
echo "VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
fi

- name: Login to GHCR
uses: docker/login-action@v3
Expand Down Expand Up @@ -51,7 +62,12 @@ jobs:
- uses: actions/checkout@v4

- name: Set version
run: echo "VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
else
echo "VERSION=${GITHUB_REF_NAME}" >> $GITHUB_ENV
fi

- name: Login to GHCR
uses: docker/login-action@v3
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ docker-compose.dev.yaml

# Generated / temporary
logo-philosophy.md
syncer
/auth
/billing
/core
web/tsconfig.tsbuildinfo
.agents/status/
.agents/issues.md
.agents
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
REGISTRY ?= ghcr.io/layfz/maas-cloud-api
REGISTRY ?= ghcr.io/maas-cloud-api
SERVICES := auth core billing syncer adapter
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.buildTime=$(BUILD_TIME)

Expand Down
47 changes: 46 additions & 1 deletion cmd/billing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ package main

import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/LayFz/maas-cloud-api/internal/billing/consumer"
"github.com/LayFz/maas-cloud-api/internal/billing/handler"
"github.com/LayFz/maas-cloud-api/internal/billing/server"
"github.com/LayFz/maas-cloud-api/internal/pkg/config"
"github.com/LayFz/maas-cloud-api/internal/pkg/database"
"github.com/LayFz/maas-cloud-api/internal/pkg/middleware"
"github.com/LayFz/maas-cloud-api/internal/pkg/mq"
appOtel "github.com/LayFz/maas-cloud-api/internal/pkg/otel"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -46,7 +52,19 @@ func main() {
log.Fatalf("init redis: %v", err)
}

// 消费用量事件
producer := mq.NewProducer(cfg.Kafka.Brokers)
defer producer.Close()

rdb := database.Redis()
billingSrv := server.NewBillingServer(cfg, producer, rdb, logger)

// 启动额度持久化协程
go billingSrv.QuotaPersister.Run(ctx)

// 启动 ClickHouse 批量写入
go billingSrv.CHWriter.Run(ctx)

// 启动 Kafka 消费者
usageConsumer := consumer.NewUsageConsumer(cfg, logger)
go func() {
logger.Info("billing consumer started",
Expand All @@ -58,10 +76,37 @@ func main() {
}
}()

// 启动 HTTP 服务
r := gin.Default()
r.Use(middleware.CORS())
handler.RegisterRoutes(r, billingSrv)
handler.RegisterInternalRoutes(r, billingSrv)

port := cfg.Server.HTTPPort
if port == 0 {
port = 8083
}

httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: r,
}
go func() {
logger.Info("billing HTTP server started",
zap.Int("port", port),
zap.String("version", version),
)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal("serve http", zap.Error(err))
}
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit

logger.Info("shutting down billing service...")
cancel()
httpServer.Shutdown(context.Background())
billingSrv.CHWriter.Close()
}
2 changes: 1 addition & 1 deletion cmd/syncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
}

// Config Syncer: 消费 tenant.events → 同步到 Higress
s := syncer.NewSyncer(cfg, logger)
s := syncer.NewSyncer(cfg, database.RawDB(), logger)
go func() {
logger.Info("config syncer started",
zap.String("topic", mq.TopicTenantEvents),
Expand Down
7 changes: 7 additions & 0 deletions configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ oauth:
client_id: ""
client_secret: ""

smtp:
host: "" # SMTP server, e.g. smtp.gmail.com (empty = dev mode, logs code to stdout)
port: 587
username: ""
password: ""
from: "noreply@anyfast.com"

otel:
endpoint: "" # 本地开发可不开 OTel
insecure: true
Expand Down
2 changes: 1 addition & 1 deletion deploy/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Usage: docker build --build-arg SERVICE=auth -t anyfast/auth:v1.0.0 .

# ============ Build Stage ============
FROM golang:1.22-alpine AS builder
FROM golang:1.25-alpine AS builder

ARG SERVICE
ARG LDFLAGS
Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/base/adapter/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: adapter
image: ghcr.io/layfz/maas-cloud-api/adapter:latest
image: ghcr.io/maas-cloud-api/adapter:latest
ports:
- name: http
containerPort: 8080
Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/base/auth/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: auth
image: ghcr.io/layfz/maas-cloud-api/auth:latest
image: ghcr.io/maas-cloud-api/auth:latest
ports:
- name: http
containerPort: 8080
Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/base/billing/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: billing
image: ghcr.io/layfz/maas-cloud-api/billing:latest
image: ghcr.io/maas-cloud-api/billing:latest
envFrom:
- configMapRef:
name: anyfast-common
Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/base/core/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
spec:
containers:
- name: core
image: ghcr.io/layfz/maas-cloud-api/core:latest
image: ghcr.io/maas-cloud-api/core:latest
ports:
- name: http
containerPort: 8080
Expand Down
2 changes: 1 addition & 1 deletion deploy/k8s/base/syncer/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
serviceAccountName: config-syncer # 需要 K8s API 权限写 CRD
containers:
- name: syncer
image: ghcr.io/layfz/maas-cloud-api/syncer:latest
image: ghcr.io/maas-cloud-api/syncer:latest
envFrom:
- configMapRef:
name: anyfast-common
Expand Down
Loading
Loading