feat: add fullstack deployment and oracle app
This commit is contained in:
4
server/.dockerignore
Normal file
4
server/.dockerignore
Normal file
@@ -0,0 +1,4 @@
|
||||
.env
|
||||
.git
|
||||
bin
|
||||
*.log
|
||||
8
server/.env.example
Normal file
8
server/.env.example
Normal file
@@ -0,0 +1,8 @@
|
||||
SERVER_PORT=8181
|
||||
AI_PROVIDER_NAME=OpenAI Compatible
|
||||
AI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode
|
||||
AI_CHAT_PATH=/v1/chat/completions
|
||||
AI_MODEL=qwen3.5-397b-a17b
|
||||
AI_API_KEY=sk-028de7b23ecb47fc8c4bd069679b8420
|
||||
AI_TIMEOUT_SECONDS=60
|
||||
WEB_ALLOWED_ORIGINS=http://127.0.0.1:5174,http://localhost:5174
|
||||
1
server/.gitignore
vendored
Normal file
1
server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.env
|
||||
23
server/Dockerfile
Normal file
23
server/Dockerfile
Normal file
@@ -0,0 +1,23 @@
|
||||
FROM golang:1.26.2-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/xly-server ./cmd/xly-server
|
||||
|
||||
FROM alpine:3.21
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN adduser -D -H -u 10001 appuser
|
||||
|
||||
COPY --from=builder /out/xly-server /app/xly-server
|
||||
|
||||
USER appuser
|
||||
|
||||
EXPOSE 8181
|
||||
|
||||
CMD ["./xly-server"]
|
||||
44
server/cmd/xly-server/main.go
Normal file
44
server/cmd/xly-server/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"xly/server/internal/config"
|
||||
"xly/server/internal/handler"
|
||||
"xly/server/internal/service"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := godotenv.Load(".env"); err != nil {
|
||||
log.Printf("skip .env load: %v", err)
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
router := gin.New()
|
||||
router.Use(gin.Logger(), gin.Recovery())
|
||||
if err := router.SetTrustedProxies(nil); err != nil {
|
||||
log.Fatalf("set trusted proxies: %v", err)
|
||||
}
|
||||
router.Use(cors.New(cors.Config{
|
||||
AllowOrigins: cfg.AllowedOrigins,
|
||||
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodOptions},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
|
||||
MaxAge: 12 * time.Hour,
|
||||
}))
|
||||
|
||||
aiService := service.NewAIService(cfg)
|
||||
httpHandler := handler.NewHTTPHandler(aiService)
|
||||
httpHandler.Register(router)
|
||||
|
||||
address := ":" + cfg.Port
|
||||
log.Printf("xly server listening on %s", address)
|
||||
if err := router.Run(address); err != nil {
|
||||
log.Fatalf("start gin server: %v", err)
|
||||
}
|
||||
}
|
||||
41
server/go.mod
Normal file
41
server/go.mod
Normal file
@@ -0,0 +1,41 @@
|
||||
module xly/server
|
||||
|
||||
go 1.26.2
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/cors v1.7.7
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.59.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
golang.org/x/arch v0.23.0 // indirect
|
||||
golang.org/x/crypto v0.48.0 // indirect
|
||||
golang.org/x/net v0.51.0 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
golang.org/x/text v0.35.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
)
|
||||
93
server/go.sum
Normal file
93
server/go.sum
Normal file
@@ -0,0 +1,93 @@
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
|
||||
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
|
||||
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/gin-contrib/cors v1.7.7 h1:Oh9joP463x7Mw72vhvJ61YQm8ODh9b04YR7vsOErD0Q=
|
||||
github.com/gin-contrib/cors v1.7.7/go.mod h1:K5tW0RkzJtWSiOdikXloy8VEZlgdVNpHNw8FpjUPNrE=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
|
||||
github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
golang.org/x/arch v0.23.0 h1:lKF64A2jF6Zd8L0knGltUnegD62JMFBiCPBmQpToHhg=
|
||||
golang.org/x/arch v0.23.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
||||
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
|
||||
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
|
||||
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
|
||||
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
|
||||
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
91
server/internal/config/config.go
Normal file
91
server/internal/config/config.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config 定义服务运行配置。
|
||||
// AI 调用统一走 OpenAI 兼容协议,上游地址和鉴权由环境变量提供。
|
||||
type Config struct {
|
||||
Port string
|
||||
ProviderName string
|
||||
AIBaseURL string
|
||||
AIChatPath string
|
||||
AIAPIKey string
|
||||
AIModel string
|
||||
AITimeout time.Duration
|
||||
AllowedOrigins []string
|
||||
}
|
||||
|
||||
// Load 从环境变量装载运行配置。
|
||||
// 未提供的字段使用保守默认值,确保本地开发可以直接启动服务。
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Port: getenv("SERVER_PORT", "8080"),
|
||||
ProviderName: getenv("AI_PROVIDER_NAME", "OpenAI Compatible"),
|
||||
AIBaseURL: strings.TrimRight(getenv("AI_BASE_URL", "https://api.openai.com"), "/"),
|
||||
AIChatPath: normalizePath(getenv("AI_CHAT_PATH", "/v1/chat/completions")),
|
||||
AIAPIKey: strings.TrimSpace(os.Getenv("AI_API_KEY")),
|
||||
AIModel: strings.TrimSpace(os.Getenv("AI_MODEL")),
|
||||
AITimeout: time.Duration(getenvInt("AI_TIMEOUT_SECONDS", 60)) * time.Second,
|
||||
AllowedOrigins: splitOrigins(getenv("WEB_ALLOWED_ORIGINS", "http://127.0.0.1:5173,http://localhost:5173")),
|
||||
}
|
||||
}
|
||||
|
||||
// AIEnabled 判断 AI 服务是否已具备调用条件。
|
||||
func (c Config) AIEnabled() bool {
|
||||
return c.AIAPIKey != "" && c.AIModel != "" && c.AIBaseURL != ""
|
||||
}
|
||||
|
||||
func getenv(key string, fallback string) string {
|
||||
value := strings.TrimSpace(os.Getenv(key))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func getenvInt(key string, fallback int) int {
|
||||
value := strings.TrimSpace(os.Getenv(key))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed <= 0 {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
func normalizePath(path string) string {
|
||||
if path == "" {
|
||||
return "/v1/chat/completions"
|
||||
}
|
||||
if strings.HasPrefix(path, "/") {
|
||||
return path
|
||||
}
|
||||
return "/" + path
|
||||
}
|
||||
|
||||
func splitOrigins(raw string) []string {
|
||||
parts := strings.Split(raw, ",")
|
||||
origins := make([]string, 0, len(parts))
|
||||
|
||||
for _, part := range parts {
|
||||
origin := strings.TrimSpace(part)
|
||||
if origin != "" {
|
||||
origins = append(origins, origin)
|
||||
}
|
||||
}
|
||||
|
||||
if len(origins) == 0 {
|
||||
return []string{"http://127.0.0.1:5173", "http://localhost:5173"}
|
||||
}
|
||||
|
||||
return origins
|
||||
}
|
||||
90
server/internal/handler/http.go
Normal file
90
server/internal/handler/http.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"xly/server/internal/model"
|
||||
"xly/server/internal/service"
|
||||
)
|
||||
|
||||
// HTTPHandler 负责注册 HTTP 路由。
|
||||
// 控制层只做参数校验和响应包装,不承担上游调用逻辑。
|
||||
type HTTPHandler struct {
|
||||
ai *service.AIService
|
||||
}
|
||||
|
||||
// NewHTTPHandler 创建 HTTP 控制器。
|
||||
func NewHTTPHandler(ai *service.AIService) *HTTPHandler {
|
||||
return &HTTPHandler{ai: ai}
|
||||
}
|
||||
|
||||
// Register 将接口注册到 Gin 路由树。
|
||||
func (h *HTTPHandler) Register(router gin.IRouter) {
|
||||
router.GET("/healthz", h.healthz)
|
||||
router.GET("/api/v1/ai/status", h.aiStatus)
|
||||
router.POST("/api/v1/ai/interpret", h.aiInterpret)
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) healthz(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, model.APIResponse[map[string]string]{
|
||||
Data: &map[string]string{
|
||||
"status": "ok",
|
||||
},
|
||||
Message: "service is healthy",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) aiStatus(ctx *gin.Context) {
|
||||
status := h.ai.Status()
|
||||
ctx.JSON(http.StatusOK, model.APIResponse[model.AIStatusResponse]{
|
||||
Data: &status,
|
||||
Message: "ok",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) aiInterpret(ctx *gin.Context) {
|
||||
var request model.InterpretRequest
|
||||
if err := ctx.ShouldBindJSON(&request); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, model.APIResponse[model.InterpretResponse]{
|
||||
Message: "请求体无效,请检查解卦上下文是否完整。",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
request.Question = strings.TrimSpace(request.Question)
|
||||
if request.Question == "" {
|
||||
ctx.JSON(http.StatusBadRequest, model.APIResponse[model.InterpretResponse]{
|
||||
Message: "所问之事不能为空。",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
content, err := h.ai.Interpret(ctx.Request.Context(), request)
|
||||
if err != nil {
|
||||
statusCode := http.StatusBadGateway
|
||||
message := "AI 解卦失败。"
|
||||
|
||||
switch {
|
||||
case errors.Is(err, service.ErrAIUnavailable):
|
||||
statusCode = http.StatusServiceUnavailable
|
||||
message = "后端 AI 服务尚未完成配置。"
|
||||
default:
|
||||
message = err.Error()
|
||||
}
|
||||
|
||||
ctx.JSON(statusCode, model.APIResponse[model.InterpretResponse]{
|
||||
Message: message,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
response := model.InterpretResponse{Content: content}
|
||||
ctx.JSON(http.StatusOK, model.APIResponse[model.InterpretResponse]{
|
||||
Data: &response,
|
||||
Message: "ok",
|
||||
})
|
||||
}
|
||||
72
server/internal/model/api.go
Normal file
72
server/internal/model/api.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package model
|
||||
|
||||
// AIStatusResponse 定义前端可见的 AI 服务状态。
|
||||
// 该对象只暴露公开信息,不包含密钥和上游鉴权细节。
|
||||
type AIStatusResponse struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
ProviderName string `json:"providerName"`
|
||||
Model string `json:"model"`
|
||||
Mode string `json:"mode"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// InterpretSlot 定义单个课位的占断上下文。
|
||||
// 该对象同时包含取值来源和落宫结果,供后端组装用户消息。
|
||||
type InterpretSlot struct {
|
||||
Label string `json:"label" binding:"required"`
|
||||
Token string `json:"token" binding:"required"`
|
||||
Value int `json:"value" binding:"required"`
|
||||
Source string `json:"source" binding:"required"`
|
||||
SourceNote string `json:"sourceNote" binding:"required"`
|
||||
GanZhi string `json:"ganZhi"`
|
||||
Palace string `json:"palace" binding:"required"`
|
||||
Element string `json:"element" binding:"required"`
|
||||
Theme string `json:"theme" binding:"required"`
|
||||
Detail string `json:"detail" binding:"required"`
|
||||
SlotExplanation string `json:"slotExplanation" binding:"required"`
|
||||
}
|
||||
|
||||
// InterpretRelation 定义四宫之间的生克链路。
|
||||
type InterpretRelation struct {
|
||||
Kind string `json:"kind" binding:"required"`
|
||||
Summary string `json:"summary" binding:"required"`
|
||||
}
|
||||
|
||||
// InterpretSummary 定义前端本地规则得出的摘要结果。
|
||||
type InterpretSummary struct {
|
||||
Verdict string `json:"verdict" binding:"required"`
|
||||
Score int `json:"score" binding:"required"`
|
||||
Summary string `json:"summary" binding:"required"`
|
||||
DomainLabel string `json:"domainLabel" binding:"required"`
|
||||
ChainSummary string `json:"chainSummary"`
|
||||
ActionAdvice []string `json:"actionAdvice" binding:"required"`
|
||||
RiskAdvice []string `json:"riskAdvice" binding:"required"`
|
||||
}
|
||||
|
||||
// InterpretRequest 定义 AI 解卦请求。
|
||||
// 前端只传结构化盘面数据,具体提示词由后端统一维护。
|
||||
type InterpretRequest struct {
|
||||
Mode string `json:"mode" binding:"required"`
|
||||
ModeLabel string `json:"modeLabel" binding:"required"`
|
||||
Question string `json:"question" binding:"required"`
|
||||
SolarLabel string `json:"solarLabel" binding:"required"`
|
||||
LunarLabel string `json:"lunarLabel" binding:"required"`
|
||||
MethodNote string `json:"methodNote" binding:"required"`
|
||||
FinalPalace string `json:"finalPalace" binding:"required"`
|
||||
FinalElement string `json:"finalElement" binding:"required"`
|
||||
Slots []InterpretSlot `json:"slots" binding:"required,min=4"`
|
||||
Relations []InterpretRelation `json:"relations"`
|
||||
LocalInterpretation InterpretSummary `json:"localInterpretation" binding:"required"`
|
||||
}
|
||||
|
||||
// InterpretResponse 定义 AI 解卦响应。
|
||||
type InterpretResponse struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// APIResponse 统一包装 HTTP 返回体。
|
||||
// Message 用于说明当前结果或错误原因。
|
||||
type APIResponse[T any] struct {
|
||||
Data *T `json:"data,omitempty"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
296
server/internal/service/ai.go
Normal file
296
server/internal/service/ai.go
Normal file
@@ -0,0 +1,296 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"xly/server/internal/config"
|
||||
"xly/server/internal/model"
|
||||
)
|
||||
|
||||
var ErrAIUnavailable = errors.New("ai service is not configured")
|
||||
|
||||
const divinationSystemPrompt = `你是“四课小六壬”解卦助手,负责把盘面结果翻译成现实语境下可执行的中文建议。
|
||||
|
||||
你的工作边界:
|
||||
1. 只基于用户提供的盘面数据解读,不臆造不存在的课位、关系或额外神煞。
|
||||
2. 结果必须写成趋势判断、阻力判断和行动建议,不得写成绝对预言。
|
||||
3. 语言要直白、稳重、可落实,不故作神秘,不堆砌玄学术语。
|
||||
4. 优先解释“为什么这样判断”,尤其要结合四宫次第、终宫落点与五行生克。
|
||||
|
||||
内置断法:
|
||||
1. 四课顺序固定为年宫 → 月宫 → 日宫 → 时宫。
|
||||
2. 年宫主大环境、外部周期、权力方态度。
|
||||
3. 月宫主当下阶段的资源、氛围、预算、团队状态。
|
||||
4. 日宫主执行面、沟通、临场表现、眼前动作。
|
||||
5. 时宫主结果落点,成败判断以时宫为主。
|
||||
6. 要结合四宫之间的生、克、比和链路判断助力与阻力是否顺势串联。
|
||||
7. 前端给出的“本地规则总断”只能作为参考线索,不能机械复述,要重新组织成完整分析。
|
||||
|
||||
输出规则:
|
||||
1. 必须使用 Markdown。
|
||||
2. 必须严格按下面结构输出,不要新增无关标题,不要输出代码块:
|
||||
### 总断
|
||||
先用 1 句总结走势,再用 1 段解释核心判断。
|
||||
|
||||
### 四宫详解与五行生克分析
|
||||
#### 年宫
|
||||
#### 月宫
|
||||
#### 日宫
|
||||
#### 时宫
|
||||
四个小节都要解释:该宫位含义、当前盘面显示的倾向、它对结果的作用。
|
||||
这一大节最后补 1 段,专门总结生克链路是顺还是逆、哪里在助推、哪里在掣肘。
|
||||
|
||||
### 趋吉次第
|
||||
输出 3 到 4 条有先后顺序的编号列表。每条都必须是可执行动作。
|
||||
|
||||
### 避忌要点
|
||||
输出 2 到 3 条无序列表。每条都写清具体风险。
|
||||
|
||||
### 结语
|
||||
最后单独写一句:仅供民俗文化参考,不替代现实决策。
|
||||
|
||||
格式细则:
|
||||
1. 标题只用 ### 和 ####。
|
||||
2. 列表只用“1.”或“-”。
|
||||
3. 不要输出表格。
|
||||
4. 不要用“根据你提供的信息我认为”这种空话开头。
|
||||
5. 不要在标题前后添加多余的星号。`
|
||||
|
||||
// AIService 负责代理上游 AI 服务。
|
||||
// 该服务只暴露统一的解卦入口,不向前端泄露上游鉴权细节。
|
||||
type AIService struct {
|
||||
config config.Config
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
type upstreamChatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
Messages []upstreamChatMessage `json:"messages"`
|
||||
EnableThinking bool `json:"enable_thinking"`
|
||||
}
|
||||
|
||||
type upstreamChatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type upstreamChatResponse struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content any `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// NewAIService 创建 AI 代理服务。
|
||||
func NewAIService(cfg config.Config) *AIService {
|
||||
return &AIService{
|
||||
config: cfg,
|
||||
httpClient: &http.Client{
|
||||
Timeout: cfg.AITimeout,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Status 返回前端可见的 AI 服务状态。
|
||||
func (s *AIService) Status() model.AIStatusResponse {
|
||||
if !s.config.AIEnabled() {
|
||||
return model.AIStatusResponse{
|
||||
Enabled: false,
|
||||
ProviderName: s.config.ProviderName,
|
||||
Model: emptyAs(s.config.AIModel, "未配置"),
|
||||
Mode: "server-proxy",
|
||||
Message: "后端已启动,但 AI_BASE_URL / AI_MODEL / AI_API_KEY 尚未完整配置。",
|
||||
}
|
||||
}
|
||||
|
||||
return model.AIStatusResponse{
|
||||
Enabled: true,
|
||||
ProviderName: s.config.ProviderName,
|
||||
Model: s.config.AIModel,
|
||||
Mode: "server-proxy",
|
||||
Message: "后端将代表前端调用上游 AI 服务。",
|
||||
}
|
||||
}
|
||||
|
||||
// Interpret 调用上游 AI 服务生成解卦文本。
|
||||
func (s *AIService) Interpret(ctx context.Context, input model.InterpretRequest) (string, error) {
|
||||
if !s.config.AIEnabled() {
|
||||
return "", ErrAIUnavailable
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(upstreamChatRequest{
|
||||
Model: s.config.AIModel,
|
||||
Temperature: 0.8,
|
||||
EnableThinking: false,
|
||||
Messages: []upstreamChatMessage{
|
||||
{
|
||||
Role: "system",
|
||||
Content: divinationSystemPrompt,
|
||||
},
|
||||
{
|
||||
Role: "user",
|
||||
Content: buildUserPrompt(input),
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("marshal upstream request: %w", err)
|
||||
}
|
||||
|
||||
request, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
s.config.AIBaseURL+s.config.AIChatPath,
|
||||
bytes.NewReader(payload),
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build upstream request: %w", err)
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", "Bearer "+s.config.AIAPIKey)
|
||||
|
||||
response, err := s.httpClient.Do(request)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("request upstream ai: %w", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(response.Body, 2<<20))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read upstream response: %w", err)
|
||||
}
|
||||
|
||||
var decoded upstreamChatResponse
|
||||
if err := json.Unmarshal(body, &decoded); err != nil {
|
||||
return "", fmt.Errorf("decode upstream response: %w", err)
|
||||
}
|
||||
|
||||
if response.StatusCode >= http.StatusBadRequest {
|
||||
message := "上游 AI 服务返回错误。"
|
||||
if decoded.Error != nil && strings.TrimSpace(decoded.Error.Message) != "" {
|
||||
message = decoded.Error.Message
|
||||
}
|
||||
return "", errors.New(message)
|
||||
}
|
||||
|
||||
if len(decoded.Choices) == 0 {
|
||||
return "", errors.New("上游 AI 服务未返回可用结果")
|
||||
}
|
||||
|
||||
content := extractContent(decoded.Choices[0].Message.Content)
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return "", errors.New("上游 AI 服务返回成功,但内容为空")
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func buildUserPrompt(input model.InterpretRequest) string {
|
||||
slotLines := make([]string, 0, len(input.Slots))
|
||||
for _, slot := range input.Slots {
|
||||
slotLines = append(slotLines, fmt.Sprintf(
|
||||
"%s:取值=%s(%d),来源=%s,来源说明=%s,落宫=%s,五行=%s,宫义=%s%s,职责=%s,干支参考=%s",
|
||||
slot.Label,
|
||||
slot.Token,
|
||||
slot.Value,
|
||||
slot.Source,
|
||||
slot.SourceNote,
|
||||
slot.Palace,
|
||||
slot.Element,
|
||||
slot.Theme,
|
||||
slot.Detail,
|
||||
slot.SlotExplanation,
|
||||
emptyAs(slot.GanZhi, "-"),
|
||||
))
|
||||
}
|
||||
|
||||
relationLines := make([]string, 0, len(input.Relations))
|
||||
for _, relation := range input.Relations {
|
||||
relationLines = append(relationLines, fmt.Sprintf("- %s:%s", relation.Kind, relation.Summary))
|
||||
}
|
||||
if len(relationLines) == 0 {
|
||||
relationLines = append(relationLines, "- 无可用链路")
|
||||
}
|
||||
|
||||
return fmt.Sprintf(`问题:%s
|
||||
起课方式:%s(%s)
|
||||
公历时间:%s
|
||||
农历时间:%s
|
||||
起课说明:%s
|
||||
终宫:%s · 五行%s
|
||||
|
||||
四宫盘面:
|
||||
%s
|
||||
|
||||
生克链路:
|
||||
%s
|
||||
|
||||
本地规则总断参考:
|
||||
- 总评:%s
|
||||
- 分数:%d
|
||||
- 摘要:%s
|
||||
- 事项类型:%s
|
||||
- 链路总结:%s
|
||||
- 趋吉建议:%s
|
||||
- 风险提示:%s`,
|
||||
input.Question,
|
||||
input.ModeLabel,
|
||||
input.Mode,
|
||||
input.SolarLabel,
|
||||
input.LunarLabel,
|
||||
input.MethodNote,
|
||||
input.FinalPalace,
|
||||
input.FinalElement,
|
||||
strings.Join(slotLines, "\n"),
|
||||
strings.Join(relationLines, "\n"),
|
||||
input.LocalInterpretation.Verdict,
|
||||
input.LocalInterpretation.Score,
|
||||
input.LocalInterpretation.Summary,
|
||||
input.LocalInterpretation.DomainLabel,
|
||||
emptyAs(input.LocalInterpretation.ChainSummary, "无"),
|
||||
strings.Join(input.LocalInterpretation.ActionAdvice, ";"),
|
||||
strings.Join(input.LocalInterpretation.RiskAdvice, ";"),
|
||||
)
|
||||
}
|
||||
|
||||
func extractContent(content any) string {
|
||||
switch value := content.(type) {
|
||||
case string:
|
||||
return strings.TrimSpace(value)
|
||||
case []any:
|
||||
parts := make([]string, 0, len(value))
|
||||
for _, item := range value {
|
||||
part, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
text, _ := part["text"].(string)
|
||||
if strings.TrimSpace(text) != "" {
|
||||
parts = append(parts, strings.TrimSpace(text))
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func emptyAs(value string, fallback string) string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user