迁移 PostgreSQL 并新增 Python API 服务
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -46,7 +46,6 @@ COPY --from=api --chown=app:app /out/web /app/web
|
||||
|
||||
ENV TZ=Asia/Shanghai \
|
||||
LISTEN_ADDR=:8080 \
|
||||
FUTURES_DB_PATH=/app/data/futures.db \
|
||||
AUTH_DB_PATH=/app/auth/auth.db
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
@@ -5,6 +5,7 @@ go 1.25.8
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.27.0
|
||||
modernc.org/sqlite v1.32.0
|
||||
)
|
||||
|
||||
@@ -7,21 +7,24 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ListenAddr string
|
||||
FuturesDBPath string
|
||||
AuthDBPath string
|
||||
JWTSecret []byte
|
||||
AdminUser string
|
||||
AdminPass string
|
||||
ListenAddr string
|
||||
DatabaseURL string
|
||||
AuthDBPath string
|
||||
JWTSecret []byte
|
||||
AdminUser string
|
||||
AdminPass string
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
cfg := &Config{
|
||||
ListenAddr: getenv("LISTEN_ADDR", ":8080"),
|
||||
FuturesDBPath: getenv("FUTURES_DB_PATH", "/app/data/futures.db"),
|
||||
AuthDBPath: getenv("AUTH_DB_PATH", "/app/auth/auth.db"),
|
||||
AdminUser: strings.TrimSpace(os.Getenv("ADMIN_USER")),
|
||||
AdminPass: os.Getenv("ADMIN_PASS"),
|
||||
ListenAddr: getenv("LISTEN_ADDR", ":8080"),
|
||||
DatabaseURL: os.Getenv("DATABASE_URL"),
|
||||
AuthDBPath: getenv("AUTH_DB_PATH", "/app/auth/auth.db"),
|
||||
AdminUser: strings.TrimSpace(os.Getenv("ADMIN_USER")),
|
||||
AdminPass: os.Getenv("ADMIN_PASS"),
|
||||
}
|
||||
if cfg.DatabaseURL == "" {
|
||||
return nil, fmt.Errorf("DATABASE_URL 环境变量未设置")
|
||||
}
|
||||
secret := strings.TrimSpace(os.Getenv("JWT_SECRET"))
|
||||
if len(secret) < 16 {
|
||||
|
||||
@@ -6,21 +6,22 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var ErrMissingTsCode = errors.New("ts_code 必填")
|
||||
|
||||
type FuturesStore struct{ db *sql.DB }
|
||||
|
||||
func OpenFutures(path string) (*FuturesStore, error) {
|
||||
dsn := fmt.Sprintf("file:%s?mode=ro&_pragma=query_only(true)", path)
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
func OpenFutures(databaseURL string) (*FuturesStore, error) {
|
||||
db, err := sql.Open("postgres", databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open futures.db: %w", err)
|
||||
return nil, fmt.Errorf("open futures db: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(4)
|
||||
db.SetMaxOpenConns(8)
|
||||
if err := db.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("ping futures.db: %w", err)
|
||||
return nil, fmt.Errorf("ping futures db: %w", err)
|
||||
}
|
||||
return &FuturesStore{db: db}, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func main() {
|
||||
log.Fatalf("config: %v", err)
|
||||
}
|
||||
|
||||
futures, err := store.OpenFutures(cfg.FuturesDBPath)
|
||||
futures, err := store.OpenFutures(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
log.Fatalf("open futures: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user