11package main
2+
23import (
4+ "encoding/json"
35 "fmt"
6+ "io/ioutil"
7+ "log"
48 "net/url"
59 "os"
10+ "strconv"
611)
712
13+ type Database struct {
14+ Type string `json:"type" env:"DB_TYPE"`
15+ Host string `json:"host" env:"DB_HOST"`
16+ Port int `json:"port" env:"DB_PORT"`
17+ User string `json:"user" env:"DB_USER"`
18+ Password string `json:"password" env:"DB_PASS"`
19+ Name string `json:"name" env:"DB_NAME"`
20+ DBFile string `json:"db_file" env:"DB_FILE"`
21+ TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"`
22+ SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"`
23+ }
24+
25+ type Scheme struct {
26+ Https bool `json:"https" env:"HTTPS"`
27+ CertFile string `json:"cert_file" env:"CERT_FILE"`
28+ KeyFile string `json:"key_file" env:"KEY_FILE"`
29+ }
30+
31+ type CacheConfig struct {
32+ Expiration int64 `json:"expiration" env:"CACHE_EXPIRATION"`
33+ CleanupInterval int64 `json:"cleanup_interval" env:"CLEANUP_INTERVAL"`
34+ }
35+
36+ type Config struct {
37+ Force bool `json:"force"`
38+ Address string `json:"address" env:"ADDR"`
39+ Port int `json:"port" env:"PORT"`
40+ Assets string `json:"assets" env:"ASSETS"`
41+ Database Database `json:"database"`
42+ Scheme Scheme `json:"scheme"`
43+ Cache CacheConfig `json:"cache"`
44+ TempDir string `json:"temp_dir" env:"TEMP_DIR"`
45+ }
46+
847func main () {
9- DatabaseUrl := os .Getenv ("DATABASE_URL" )
10- fmt .Println ("DatabaseUrl" ,DatabaseUrl )
11- // DATABASE_URL = postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398
12- u , err := url .Parse (DatabaseUrl )
48+ DATABASE_URL := os .Getenv ("DATABASE_URL" )
49+ fmt .Println ("DatabaseUrl" , DATABASE_URL )
50+ DATABASE_URL = " postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398"
51+ u , err := url .Parse (DATABASE_URL )
1352 if err != nil {
1453 fmt .Println (err )
1554 }
1655 user := u .User .Username ()
1756 pass , _ := u .User .Password ()
1857 host := u .Hostname ()
19- port := u .Port ()
58+ port , _ := strconv . Atoi ( u .Port () )
2059 name := u .Path [1 :]
21- _ = os .Setenv ("DB_HOST" , host )
22- _ = os .Setenv ("DB_PORT" , port )
23- _ = os .Setenv ("DB_USER" , user )
24- _ = os .Setenv ("DB_PASS" , pass )
25- _ = os .Setenv ("DB_NAME" , name )
26- }
60+ config := Config {
61+ Address : "0.0.0.0" ,
62+ TempDir : "data/temp" ,
63+ Database : Database {
64+ User : user ,
65+ Password : pass ,
66+ Host : host ,
67+ Port : port ,
68+ Name : name ,
69+ },
70+ }
71+ confBody , err := json .MarshalIndent (config , "" , " " )
72+ if err != nil {
73+ log .Fatalf ("failed marshal json: %s" , err .Error ())
74+ }
75+ err = ioutil .WriteFile ("/opt/alist/data/config.json" , confBody , 0777 )
76+ if err != nil {
77+ log .Fatalf ("failed write json: %s" , err .Error ())
78+ }
79+ }
0 commit comments