@@ -19,109 +19,122 @@ import (
1919 "go.uber.org/zap"
2020)
2121
22- var Module = fx . Module (
23- "appconfig" ,
24- fx .Provide (
25- func ( log * zap. Logger ) Config {
26- if err := config . LoadConfig ( & defaultConfig ); err != nil {
27- log . Error ( "Error loading config" , zap .Error ( err ))
28- }
22+ //nolint:funlen // long function
23+ func Module () fx. Option {
24+ return fx .Module (
25+ "appconfig" ,
26+ fx . Provide (
27+ func ( log * zap.Logger ) Config {
28+ defaultConfig := Default ()
2929
30- return defaultConfig
31- },
32- fx .Private ,
33- ),
34- fx .Provide (func (cfg Config ) http.Config {
35- return http.Config {
36- Listen : cfg .HTTP .Listen ,
37- Proxies : cfg .HTTP .Proxies ,
30+ if err := config .LoadConfig (& defaultConfig ); err != nil {
31+ log .Error ("Error loading config" , zap .Error (err ))
32+ }
3833
39- WriteTimeout : 30 * time .Minute , // SSE requires longer timeout
40- }
41- }),
42- fx .Provide (func (cfg Config ) db.Config {
43- return db.Config {
44- Dialect : db .DialectMySQL ,
45- Host : cfg .Database .Host ,
46- Port : cfg .Database .Port ,
47- User : cfg .Database .User ,
48- Password : cfg .Database .Password ,
49- Database : cfg .Database .Database ,
50- Timezone : cfg .Database .Timezone ,
51- Debug : cfg .Database .Debug ,
34+ return defaultConfig
35+ },
36+ fx .Private ,
37+ ),
38+ fx .Provide (func (cfg Config ) http.Config {
39+ const writeTimeout = 30 * time .Minute
5240
53- MaxOpenConns : cfg .Database .MaxOpenConns ,
54- MaxIdleConns : cfg .Database .MaxIdleConns ,
55- }
56- }),
57- fx .Provide (func (cfg Config ) push.Config {
58- mode := push .ModeFCM
59- if cfg .Gateway .Mode == GatewayModePrivate {
60- mode = push .ModeUpstream
61- }
41+ return http.Config {
42+ Listen : cfg .HTTP .Listen ,
43+ Proxies : cfg .HTTP .Proxies ,
6244
63- return push.Config {
64- Mode : mode ,
65- ClientOptions : map [string ]string {
66- "credentials" : cfg .FCM .CredentialsJSON ,
67- },
68- Debounce : time .Duration (cfg .FCM .DebounceSeconds ) * time .Second ,
69- Timeout : time .Duration (cfg .FCM .TimeoutSeconds ) * time .Second ,
70- }
71- }),
72- fx .Provide (func (cfg Config ) auth.Config {
73- return auth.Config {
74- Mode : auth .Mode (cfg .Gateway .Mode ),
75- PrivateToken : cfg .Gateway .PrivateToken ,
76- }
77- }),
78- fx .Provide (func (cfg Config ) handlers.Config {
79- // Default and normalize API path/host
80- if cfg .HTTP .API .Host == "" {
81- cfg .HTTP .API .Path = "/api"
82- }
83- // Ensure leading slash and trim trailing slash (except root)
84- if ! strings .HasPrefix (cfg .HTTP .API .Path , "/" ) {
85- cfg .HTTP .API .Path = "/" + cfg .HTTP .API .Path
86- }
87- if cfg .HTTP .API .Path != "/" && strings .HasSuffix (cfg .HTTP .API .Path , "/" ) {
88- cfg .HTTP .API .Path = strings .TrimRight (cfg .HTTP .API .Path , "/" )
89- }
90- // Guard against misconfigured scheme in host (accept "host[:port]" only)
91- cfg .HTTP .API .Host = strings .TrimPrefix (strings .TrimPrefix (cfg .HTTP .API .Host , "https://" ), "http://" )
45+ WriteTimeout : writeTimeout , // SSE requires longer timeout
46+ }
47+ }),
48+ fx .Provide (func (cfg Config ) db.Config {
49+ return db.Config {
50+ Dialect : db .DialectMySQL ,
51+ Host : cfg .Database .Host ,
52+ Port : cfg .Database .Port ,
53+ User : cfg .Database .User ,
54+ Password : cfg .Database .Password ,
55+ Database : cfg .Database .Database ,
56+ Timezone : cfg .Database .Timezone ,
57+ Debug : cfg .Database .Debug ,
9258
93- return handlers.Config {
94- PublicHost : cfg .HTTP .API .Host ,
95- PublicPath : cfg .HTTP .API .Path ,
96- UpstreamEnabled : cfg .Gateway .Mode == GatewayModePublic ,
97- OpenAPIEnabled : cfg .HTTP .OpenAPI .Enabled ,
98- }
99- }),
100- fx .Provide (func (cfg Config ) messages.Config {
101- return messages.Config {
102- CacheTTL : time .Duration (cfg .Messages .CacheTTLSeconds ) * time .Second ,
103- HashingInterval : time .Duration (max (cfg .Tasks .Hashing .IntervalSeconds , cfg .Messages .HashingIntervalSeconds )) * time .Second ,
104- }
105- }),
106- fx .Provide (func (cfg Config ) devices.Config {
107- return devices.Config {
108- UnusedLifetime : 365 * 24 * time .Hour , //TODO: make it configurable
109- }
110- }),
111- fx .Provide (func (cfg Config ) sse.Config {
112- return sse .NewConfig (
113- sse .WithKeepAlivePeriod (time .Duration (cfg .SSE .KeepAlivePeriodSeconds ) * time .Second ),
114- )
115- }),
116- fx .Provide (func (cfg Config ) cache.Config {
117- return cache.Config {
118- URL : cfg .Cache .URL ,
119- }
120- }),
121- fx .Provide (func (cfg Config ) pubsub.Config {
122- return pubsub.Config {
123- URL : cfg .PubSub .URL ,
124- BufferSize : 128 ,
125- }
126- }),
127- )
59+ MaxOpenConns : cfg .Database .MaxOpenConns ,
60+ MaxIdleConns : cfg .Database .MaxIdleConns ,
61+
62+ DSN : "" ,
63+ ConnMaxIdleTime : 0 ,
64+ ConnMaxLifetime : 0 ,
65+ }
66+ }),
67+ fx .Provide (func (cfg Config ) push.Config {
68+ mode := push .ModeFCM
69+ if cfg .Gateway .Mode == GatewayModePrivate {
70+ mode = push .ModeUpstream
71+ }
72+
73+ return push.Config {
74+ Mode : mode ,
75+ ClientOptions : map [string ]string {
76+ "credentials" : cfg .FCM .CredentialsJSON ,
77+ },
78+ Debounce : time .Duration (cfg .FCM .DebounceSeconds ) * time .Second ,
79+ Timeout : time .Duration (cfg .FCM .TimeoutSeconds ) * time .Second ,
80+ }
81+ }),
82+ fx .Provide (func (cfg Config ) auth.Config {
83+ return auth.Config {
84+ Mode : auth .Mode (cfg .Gateway .Mode ),
85+ PrivateToken : cfg .Gateway .PrivateToken ,
86+ }
87+ }),
88+ fx .Provide (func (cfg Config ) handlers.Config {
89+ // Default and normalize API path/host
90+ if cfg .HTTP .API .Host == "" {
91+ cfg .HTTP .API .Path = "/api"
92+ }
93+ // Ensure leading slash and trim trailing slash (except root)
94+ if ! strings .HasPrefix (cfg .HTTP .API .Path , "/" ) {
95+ cfg .HTTP .API .Path = "/" + cfg .HTTP .API .Path
96+ }
97+ if cfg .HTTP .API .Path != "/" && strings .HasSuffix (cfg .HTTP .API .Path , "/" ) {
98+ cfg .HTTP .API .Path = strings .TrimRight (cfg .HTTP .API .Path , "/" )
99+ }
100+ // Guard against misconfigured scheme in host (accept "host[:port]" only)
101+ cfg .HTTP .API .Host = strings .TrimPrefix (strings .TrimPrefix (cfg .HTTP .API .Host , "https://" ), "http://" )
102+
103+ return handlers.Config {
104+ PublicHost : cfg .HTTP .API .Host ,
105+ PublicPath : cfg .HTTP .API .Path ,
106+ UpstreamEnabled : cfg .Gateway .Mode == GatewayModePublic ,
107+ OpenAPIEnabled : cfg .HTTP .OpenAPI .Enabled ,
108+ }
109+ }),
110+ fx .Provide (func (cfg Config ) messages.Config {
111+ return messages.Config {
112+ CacheTTL : time .Duration (cfg .Messages .CacheTTLSeconds ) * time .Second ,
113+ HashingInterval : time .Duration (
114+ max (cfg .Tasks .Hashing .IntervalSeconds , cfg .Messages .HashingIntervalSeconds ),
115+ ) * time .Second ,
116+ }
117+ }),
118+ fx .Provide (func (_ Config ) devices.Config {
119+ return devices.Config {
120+ UnusedLifetime : 365 * 24 * time .Hour , //TODO: make it configurable
121+ }
122+ }),
123+ fx .Provide (func (cfg Config ) sse.Config {
124+ return sse .NewConfig (
125+ sse .WithKeepAlivePeriod (time .Duration (cfg .SSE .KeepAlivePeriodSeconds ) * time .Second ),
126+ )
127+ }),
128+ fx .Provide (func (cfg Config ) cache.Config {
129+ return cache.Config {
130+ URL : cfg .Cache .URL ,
131+ }
132+ }),
133+ fx .Provide (func (cfg Config ) pubsub.Config {
134+ return pubsub.Config {
135+ URL : cfg .PubSub .URL ,
136+ BufferSize : cfg .PubSub .BufferSize ,
137+ }
138+ }),
139+ )
140+ }
0 commit comments