@@ -12,6 +12,7 @@ import (
1212 "os"
1313 "path/filepath"
1414 "strings"
15+ "time"
1516
1617 configaccess "github.com/router-for-me/CLIProxyAPI/v6/internal/access/config_access"
1718 "github.com/router-for-me/CLIProxyAPI/v6/internal/cmd"
@@ -101,6 +102,12 @@ func main() {
101102 var cfg * config.Config
102103 var isCloudDeploy bool
103104 var (
105+ usePostgresStore bool
106+ pgStoreDSN string
107+ pgStoreSchema string
108+ pgStoreConfigKey string
109+ pgStoreCacheDir string
110+ pgStoreInst * store.PostgresStore
104111 gitStoreLocalPath string
105112 useGitStore bool
106113 gitStoreRemoteURL string
@@ -125,6 +132,22 @@ func main() {
125132 }
126133 return "" , false
127134 }
135+ if value , ok := lookupEnv ("PGSTORE_DSN" , "pgstore_dsn" ); ok {
136+ usePostgresStore = true
137+ pgStoreDSN = value
138+ }
139+ if usePostgresStore {
140+ if value , ok := lookupEnv ("PGSTORE_SCHEMA" , "pgstore_schema" ); ok {
141+ pgStoreSchema = value
142+ }
143+ if value , ok := lookupEnv ("PGSTORE_CONFIG_KEY" , "pgstore_config_key" ); ok {
144+ pgStoreConfigKey = value
145+ }
146+ if value , ok := lookupEnv ("PGSTORE_CACHE_DIR" , "pgstore_cache_dir" ); ok {
147+ pgStoreCacheDir = value
148+ }
149+ useGitStore = false
150+ }
128151 if value , ok := lookupEnv ("GITSTORE_GIT_URL" , "gitstore_git_url" ); ok {
129152 useGitStore = true
130153 gitStoreRemoteURL = value
@@ -147,13 +170,42 @@ func main() {
147170 }
148171
149172 // Determine and load the configuration file.
150- // If gitstore is configured, load from the cloned repository; otherwise use the provided path or default .
173+ // Prefer the Postgres store when configured, otherwise fallback to git or local files .
151174 var configFilePath string
152- if useGitStore {
175+ if usePostgresStore {
176+ if pgStoreCacheDir == "" {
177+ pgStoreCacheDir = wd
178+ }
179+ pgStoreCacheDir = filepath .Join (pgStoreCacheDir , "pgstore" )
180+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
181+ pgStoreInst , err = store .NewPostgresStore (ctx , store.PostgresStoreConfig {
182+ DSN : pgStoreDSN ,
183+ Schema : pgStoreSchema ,
184+ ConfigKey : pgStoreConfigKey ,
185+ SpoolDir : pgStoreCacheDir ,
186+ })
187+ cancel ()
188+ if err != nil {
189+ log .Fatalf ("failed to initialize postgres token store: %v" , err )
190+ }
191+ examplePath := filepath .Join (wd , "config.example.yaml" )
192+ ctx , cancel = context .WithTimeout (context .Background (), 30 * time .Second )
193+ if errBootstrap := pgStoreInst .Bootstrap (ctx , examplePath ); errBootstrap != nil {
194+ cancel ()
195+ log .Fatalf ("failed to bootstrap postgres-backed config: %v" , errBootstrap )
196+ }
197+ cancel ()
198+ configFilePath = pgStoreInst .ConfigPath ()
199+ cfg , err = config .LoadConfigOptional (configFilePath , isCloudDeploy )
200+ if err == nil {
201+ cfg .AuthDir = pgStoreInst .AuthDir ()
202+ log .Infof ("postgres-backed token store enabled, workspace path: %s" , pgStoreInst .WorkDir ())
203+ }
204+ } else if useGitStore {
153205 if gitStoreLocalPath == "" {
154206 gitStoreLocalPath = wd
155207 }
156- gitStoreRoot = filepath .Join (gitStoreLocalPath , "remote " )
208+ gitStoreRoot = filepath .Join (gitStoreLocalPath , "gitstore " )
157209 authDir := filepath .Join (gitStoreRoot , "auths" )
158210 gitStoreInst = store .NewGitTokenStore (gitStoreRemoteURL , gitStoreUser , gitStorePassword )
159211 gitStoreInst .SetBaseDir (authDir )
@@ -172,7 +224,7 @@ func main() {
172224 if errCopy := misc .CopyConfigTemplate (examplePath , configFilePath ); errCopy != nil {
173225 log .Fatalf ("failed to bootstrap git-backed config: %v" , errCopy )
174226 }
175- if errCommit := gitStoreInst .CommitConfig (context .Background ()); errCommit != nil {
227+ if errCommit := gitStoreInst .PersistConfig (context .Background ()); errCommit != nil {
176228 log .Fatalf ("failed to commit initial git-backed config: %v" , errCommit )
177229 }
178230 log .Infof ("git-backed config initialized from template: %s" , configFilePath )
@@ -245,7 +297,9 @@ func main() {
245297 }
246298
247299 // Register the shared token store once so all components use the same persistence backend.
248- if useGitStore {
300+ if usePostgresStore {
301+ sdkAuth .RegisterTokenStore (pgStoreInst )
302+ } else if useGitStore {
249303 sdkAuth .RegisterTokenStore (gitStoreInst )
250304 } else {
251305 sdkAuth .RegisterTokenStore (sdkAuth .NewFileTokenStore ())
0 commit comments