Refactor server layout to rag-server (#525)
This commit is contained in:
parent
97a4de4152
commit
92a3458f4e
10
Makefile
10
Makefile
@ -80,7 +80,7 @@ endif
|
||||
# Database initialization
|
||||
# -----------------------------------------------------------------------------
|
||||
init-db:
|
||||
@psql $(PG_DSN) -f docs/init.sql
|
||||
@psql $(PG_DSN) -f rag-server/sql/schema.sql
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Build targets
|
||||
@ -89,10 +89,10 @@ init-db:
|
||||
build: update-homepage-manifests build-cli build-server build-homepage
|
||||
|
||||
build-cli:
|
||||
$(MAKE) -C client build
|
||||
$(MAKE) -C rag-server/cmd/rag-server-cli build
|
||||
|
||||
build-server:
|
||||
$(MAKE) -C server build
|
||||
$(MAKE) -C rag-server build
|
||||
|
||||
build-homepage:
|
||||
$(MAKE) -C ui/homepage build SKIP_SYNC=1
|
||||
@ -107,7 +107,7 @@ update-homepage-manifests:
|
||||
start: start-openresty start-server start-homepage start-dl start-docs
|
||||
|
||||
start-server:
|
||||
$(MAKE) -C server start
|
||||
$(MAKE) -C rag-server start
|
||||
|
||||
start-homepage:
|
||||
$(MAKE) -C ui/homepage start
|
||||
@ -116,7 +116,7 @@ start-homepage:
|
||||
stop: stop-server stop-homepage stop-openresty
|
||||
|
||||
stop-server:
|
||||
$(MAKE) -C server stop
|
||||
$(MAKE) -C rag-server stop
|
||||
|
||||
stop-homepage:
|
||||
$(MAKE) -C ui/homepage stop
|
||||
|
||||
@ -8,11 +8,11 @@ import (
|
||||
|
||||
cfgpkg "xcontrol/internal/rag/config"
|
||||
"xcontrol/internal/rag/ingest"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configPath := flag.String("config", "example/server/config/server.yaml", "config path")
|
||||
configPath := flag.String("config", "rag-server/config/server.yaml", "config path")
|
||||
onlyRepo := flag.String("only-repo", "", "only ingest repo by name")
|
||||
dryRun := flag.Bool("dry-run", false, "dry run")
|
||||
maxFiles := flag.Int("max-files", 0, "limit number of files")
|
||||
|
||||
@ -16,10 +16,10 @@ import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
rconfig "xcontrol/internal/rag/config"
|
||||
"xcontrol/server"
|
||||
"xcontrol/server/api"
|
||||
"xcontrol/server/config"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server"
|
||||
"xcontrol/rag-server/api"
|
||||
"xcontrol/rag-server/config"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
- `internal/store`: 使用 `pgx` 连接 PostgreSQL,定义用户、会话、绑定等模型。
|
||||
- `internal/cache`: 基于 `go-redis` 实现 token、会话的缓存与黑名单。
|
||||
- `api`: 提供 `/login`、`/logout`、`/userinfo` 等 REST 接口,可按需扩展 gRPC。
|
||||
- `config`: 参照 `server/config` 风格,提供 YAML/ENV 配置解析。
|
||||
- `config`: 参照 `rag-server/config` 风格,提供 YAML/ENV 配置解析。
|
||||
|
||||
## 3. 协议支持
|
||||
|
||||
@ -97,7 +97,7 @@ CREATE TABLE IF NOT EXISTS sessions (
|
||||
- 各模块以接口形式定义,新增认证协议只需实现 `auth.Provider` 接口并在配置中注册。
|
||||
- 数据库与缓存层均预留版本迁移脚本,支持通过 `migrate` 工具升级。
|
||||
- 通过 `plugins/` 目录支持业务插件,API 层暴露 Hook 以注入自定义逻辑。
|
||||
- 与现有 `xcontrol/server` 共享部分通用库(如 `config`、`logging`),保持代码风格一致。
|
||||
- 与现有 `xcontrol/rag-server` 共享部分通用库(如 `config`、`logging`),保持代码风格一致。
|
||||
|
||||
## 8. 代码目录规划
|
||||
|
||||
|
||||
@ -180,7 +180,7 @@ Expected response on success: `{"rows":1}`. If the vector database is unavailabl
|
||||
- **Description:** Ask the AI service for an answer. The endpoint uses [LangChainGo](https://github.com/tmc/langchaingo) to communicate with the configured model provider (e.g., OpenAI-compatible services or a local Ollama instance). Ensure the server configuration includes the proper token or local server URL.
|
||||
- **Body Parameters (JSON):**
|
||||
- `question` – Question text.
|
||||
**Configuration:** In `server/config/server.yaml` the `models` section selects the LLM and embedding providers.
|
||||
**Configuration:** In `rag-server/config/server.yaml` the `models` section selects the LLM and embedding providers.
|
||||
For local debugging with HuggingFace and Ollama:
|
||||
|
||||
```yaml
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
接口示例:
|
||||
|
||||
```go
|
||||
// server/api.go
|
||||
// rag-server/api.go
|
||||
func RegisterRoutes(r *gin.Engine, db *pgx.Conn) {
|
||||
r.POST("/sync", SyncHandler)
|
||||
r.POST("/ask", AskHandler)
|
||||
|
||||
@ -75,7 +75,7 @@ CREATE EXTENSION IF NOT EXISTS vector;
|
||||
-- 启用中文分词扩展(可选)
|
||||
CREATE EXTENSION IF NOT EXISTS zhparser;
|
||||
|
||||
导入初始化 SQL(例如 docs/init.sql) psql -h 127.0.0.1 -U shenlan -d shenlan -f docs/init.sql
|
||||
导入初始化 SQL(例如 rag-server/sql/schema.sql) psql -h 127.0.0.1 -U shenlan -d shenlan -f rag-server/sql/schema.sql
|
||||
验证扩展与表结构
|
||||
|
||||
psql -h 127.0.0.1 -U shenlan -d shenlan -c "\d+ documents"
|
||||
@ -129,11 +129,11 @@ GRANT ALL PRIVILEGES ON DATABASE shenlan TO shenlan;
|
||||
```
|
||||
6. 导入项目提供的初始化脚本
|
||||
|
||||
项目在 `docs/init.sql` 中提供了建表及索引脚本,可通过 `psql` 导入:
|
||||
项目在 `rag-server/sql/schema.sql` 中提供了建表及索引脚本,可通过 `psql` 导入:
|
||||
```bash
|
||||
退出再用 shenlan 用户运行 init.sql
|
||||
|
||||
psql -h 127.0.0.1 -U shenlan -d shenlan -f docs/init.sql
|
||||
psql -h 127.0.0.1 -U shenlan -d shenlan -f rag-server/sql/schema.sql
|
||||
```
|
||||
该脚本会:
|
||||
- 创建 `vector` 和 `zhparser` 扩展(如未启用)。
|
||||
@ -182,7 +182,7 @@ psql postgres://shenlan:<密码>@127.0.0.1:5432/mydb -c "\d+ documents"
|
||||
|
||||
## 6. 配置嵌入服务
|
||||
|
||||
在 `server/config/server.yaml` 中新增 `embedding` 配置,使服务端能够对问题进行向量化检索:
|
||||
在 `rag-server/config/server.yaml` 中新增 `embedding` 配置,使服务端能够对问题进行向量化检索:
|
||||
|
||||
```yaml
|
||||
global:
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"xcontrol/internal/rag/embed"
|
||||
"xcontrol/internal/rag/store"
|
||||
rsync "xcontrol/internal/rag/sync"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
// Options control ingestion behaviour.
|
||||
|
||||
@ -3,9 +3,12 @@ MAIN_FILE := ../cmd/xcontrol-server/main.go
|
||||
MODULE := xcontrol
|
||||
PORT := 8090
|
||||
OS := $(shell uname -s)
|
||||
DB_URL ?= postgres://shenlan:password@127.0.0.1:5432/xserver?sslmode=disable
|
||||
SCHEMA_FILE := sql/schema.sql
|
||||
PSQL := psql "$(DB_URL)" -v ON_ERROR_STOP=1
|
||||
export PATH := /usr/local/go/bin:$(PATH)
|
||||
|
||||
.PHONY: all build start stop restart clean init help dev test
|
||||
.PHONY: all build start stop restart clean init help dev test init-db reinit-db drop-db
|
||||
|
||||
all: build
|
||||
|
||||
@ -66,17 +69,30 @@ dev:
|
||||
fi
|
||||
|
||||
clean:
|
||||
@echo ">>> 清理构建产物"
|
||||
rm -f $(APP_NAME)
|
||||
@echo ">>> 清理构建产物"
|
||||
rm -f $(APP_NAME)
|
||||
|
||||
init-db:
|
||||
@echo ">>> 初始化 RAG schema ($(SCHEMA_FILE))"
|
||||
@$(PSQL) -f $(SCHEMA_FILE)
|
||||
|
||||
drop-db:
|
||||
@echo ">>> 删除 RAG schema 对象"
|
||||
@$(PSQL) -c "DROP TABLE IF EXISTS public.documents CASCADE;"
|
||||
|
||||
reinit-db: drop-db init-db
|
||||
|
||||
help:
|
||||
@echo " XControl Server Makefile"
|
||||
@echo ""
|
||||
@echo "make build 编译 server 可执行文件"
|
||||
@echo "make start 后台运行 server (默认端口: $(PORT))"
|
||||
@echo "make stop 停止运行 server"
|
||||
@echo "make restart 重启 server"
|
||||
@echo "make test 运行单元测试"
|
||||
@echo "make dev 开发模式运行 (自动检测 air,如无则用 go run)"
|
||||
@echo "make init 初始化依赖(自动选择国内/默认 Go 模块代理,air 可选)"
|
||||
@echo "make clean 清理构建产物"
|
||||
@echo " XControl Server Makefile"
|
||||
@echo ""
|
||||
@echo "make build 编译 server 可执行文件"
|
||||
@echo "make start 后台运行 server (默认端口: $(PORT))"
|
||||
@echo "make stop 停止运行 server"
|
||||
@echo "make restart 重启 server"
|
||||
@echo "make test 运行单元测试"
|
||||
@echo "make dev 开发模式运行 (自动检测 air,如无则用 go run)"
|
||||
@echo "make init 初始化依赖(自动选择国内/默认 Go 模块代理,air 可选)"
|
||||
@echo "make clean 清理构建产物"
|
||||
@echo "make init-db 初始化数据库 schema ($(SCHEMA_FILE))"
|
||||
@echo "make drop-db 删除 RAG 相关数据库对象"
|
||||
@echo "make reinit-db 重置数据库 schema (drop + init)"
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"xcontrol/server/internal/service"
|
||||
"xcontrol/rag-server/internal/service"
|
||||
)
|
||||
|
||||
var allowedRoles = map[string]struct{}{
|
||||
@ -11,8 +11,8 @@ import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"xcontrol/server/internal/model"
|
||||
"xcontrol/server/internal/service"
|
||||
"xcontrol/rag-server/internal/model"
|
||||
"xcontrol/rag-server/internal/service"
|
||||
)
|
||||
|
||||
func setupAdminSettingsTestRouter(t *testing.T) *gin.Engine {
|
||||
@ -3,7 +3,7 @@ package api
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"xcontrol/server/internal/service"
|
||||
"xcontrol/rag-server/internal/service"
|
||||
)
|
||||
|
||||
// ConfigureServiceDB configures the internal service database connection.
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
rsync "xcontrol/internal/rag/sync"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
// registerKnowledgeRoutes sets up knowledge base endpoints.
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"xcontrol/server/internal/service"
|
||||
"xcontrol/rag-server/internal/service"
|
||||
)
|
||||
|
||||
// registerNodeRoutes sets up node related endpoints.
|
||||
@ -12,7 +12,7 @@ import (
|
||||
rconfig "xcontrol/internal/rag/config"
|
||||
ragembed "xcontrol/internal/rag/embed"
|
||||
"xcontrol/internal/rag/store"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
// ragService defines methods used by the RAG API. It allows tests to supply a
|
||||
@ -3,7 +3,7 @@ package api
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"xcontrol/server"
|
||||
"xcontrol/rag-server"
|
||||
)
|
||||
|
||||
// RegisterRoutes returns a server.Registrar that registers all API routes.
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"xcontrol/server/internal/service"
|
||||
"xcontrol/rag-server/internal/service"
|
||||
)
|
||||
|
||||
// registerUserRoutes sets up user related endpoints.
|
||||
@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "xcontrol/server/internal/agent"
|
||||
import "xcontrol/rag-server/internal/agent"
|
||||
|
||||
func main() {
|
||||
agent.Run()
|
||||
@ -20,7 +20,7 @@ import (
|
||||
"xcontrol/internal/rag/ingest"
|
||||
"xcontrol/internal/rag/store"
|
||||
rsync "xcontrol/internal/rag/sync"
|
||||
"xcontrol/server/proxy"
|
||||
"xcontrol/rag-server/proxy"
|
||||
)
|
||||
|
||||
// main synchronizes configured repositories and ingests markdown files.
|
||||
@ -175,9 +175,9 @@ type Config struct {
|
||||
}
|
||||
|
||||
// Load reads the configuration file at the provided path. When path is empty,
|
||||
// it defaults to server/config/server.yaml.
|
||||
// it defaults to rag-server/config/server.yaml.
|
||||
func Load(path ...string) (*Config, error) {
|
||||
p := filepath.Join("server", "config", "server.yaml")
|
||||
p := filepath.Join("rag-server", "config", "server.yaml")
|
||||
if len(path) > 0 && path[0] != "" {
|
||||
p = path[0]
|
||||
}
|
||||
@ -13,7 +13,7 @@ func TestLoad(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("getwd: %v", err)
|
||||
}
|
||||
// change to repository root so Load can read server/config/server.yaml
|
||||
// change to repository root so Load can read rag-server/config/server.yaml
|
||||
if err := os.Chdir("../.."); err != nil {
|
||||
t.Fatalf("chdir: %v", err)
|
||||
}
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"xcontrol/server/config"
|
||||
"xcontrol/rag-server/config"
|
||||
)
|
||||
|
||||
// UseCORS applies a restrictive CORS policy to the provided gin engine based on the
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"xcontrol/server/internal/model"
|
||||
"xcontrol/rag-server/internal/model"
|
||||
)
|
||||
|
||||
// ErrServiceDBNotInitialized indicates the service database has not been configured.
|
||||
@ -3,7 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"xcontrol/server/internal/model"
|
||||
"xcontrol/rag-server/internal/model"
|
||||
)
|
||||
|
||||
func ListNodes(ctx context.Context) ([]model.Node, error) {
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"xcontrol/server/internal/model"
|
||||
"xcontrol/rag-server/internal/model"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
Loading…
Reference in New Issue
Block a user