@@ -19,114 +19,125 @@ 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 .Dialect (cfg .Database .Dialect ),
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 ) messages.HashingTaskConfig {
73- return messages.HashingTaskConfig {
74- Interval : time .Duration (cfg .Tasks .Hashing .IntervalSeconds ) * time .Second ,
75- }
76- }),
77- fx .Provide (func (cfg Config ) auth.Config {
78- return auth.Config {
79- Mode : auth .Mode (cfg .Gateway .Mode ),
80- PrivateToken : cfg .Gateway .PrivateToken ,
81- }
82- }),
83- fx .Provide (func (cfg Config ) handlers.Config {
84- // Default and normalize API path/host
85- if cfg .HTTP .API .Host == "" {
86- cfg .HTTP .API .Path = "/api"
87- }
88- // Ensure leading slash and trim trailing slash (except root)
89- if ! strings .HasPrefix (cfg .HTTP .API .Path , "/" ) {
90- cfg .HTTP .API .Path = "/" + cfg .HTTP .API .Path
91- }
92- if cfg .HTTP .API .Path != "/" && strings .HasSuffix (cfg .HTTP .API .Path , "/" ) {
93- cfg .HTTP .API .Path = strings .TrimRight (cfg .HTTP .API .Path , "/" )
94- }
95- // Guard against misconfigured scheme in host (accept "host[:port]" only)
96- 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 .Dialect (cfg .Database .Dialect ),
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 ,
58+ MaxOpenConns : cfg .Database .MaxOpenConns ,
59+ MaxIdleConns : cfg .Database .MaxIdleConns ,
9760
98- return handlers.Config {
99- PublicHost : cfg .HTTP .API .Host ,
100- PublicPath : cfg .HTTP .API .Path ,
101- UpstreamEnabled : cfg .Gateway .Mode == GatewayModePublic ,
102- OpenAPIEnabled : cfg .HTTP .OpenAPI .Enabled ,
103- }
104- }),
105- fx .Provide (func (cfg Config ) messages.Config {
106- return messages.Config {
107- ProcessedLifetime : time .Duration (cfg .Messages .ProcessedLifetimeHours ) * time .Hour ,
108- CacheTTL : time .Duration (cfg .Messages .CacheTTLSeconds ) * time .Second ,
109- }
110- }),
111- fx .Provide (func (cfg Config ) devices.Config {
112- return devices.Config {
113- UnusedLifetime : 365 * 24 * time .Hour , //TODO: make it configurable
114- }
115- }),
116- fx .Provide (func (cfg Config ) sse.Config {
117- return sse .NewConfig (
118- sse .WithKeepAlivePeriod (time .Duration (cfg .SSE .KeepAlivePeriodSeconds ) * time .Second ),
119- )
120- }),
121- fx .Provide (func (cfg Config ) cache.Config {
122- return cache.Config {
123- URL : cfg .Cache .URL ,
124- }
125- }),
126- fx .Provide (func (cfg Config ) pubsub.Config {
127- return pubsub.Config {
128- URL : cfg .PubSub .URL ,
129- BufferSize : 128 ,
130- }
131- }),
132- )
61+ DSN : "" ,
62+ ConnMaxIdleTime : 0 ,
63+ ConnMaxLifetime : 0 ,
64+ }
65+ }),
66+ fx .Provide (func (cfg Config ) push.Config {
67+ mode := push .ModeFCM
68+ if cfg .Gateway .Mode == GatewayModePrivate {
69+ mode = push .ModeUpstream
70+ }
71+
72+ return push.Config {
73+ Mode : mode ,
74+ ClientOptions : map [string ]string {
75+ "credentials" : cfg .FCM .CredentialsJSON ,
76+ },
77+ Debounce : time .Duration (cfg .FCM .DebounceSeconds ) * time .Second ,
78+ Timeout : time .Duration (cfg .FCM .TimeoutSeconds ) * time .Second ,
79+ }
80+ }),
81+ fx .Provide (func (cfg Config ) messages.HashingTaskConfig {
82+ return messages.HashingTaskConfig {
83+ Interval : time .Duration (cfg .Tasks .Hashing .IntervalSeconds ) * time .Second ,
84+ }
85+ }),
86+ fx .Provide (func (cfg Config ) auth.Config {
87+ return auth.Config {
88+ Mode : auth .Mode (cfg .Gateway .Mode ),
89+ PrivateToken : cfg .Gateway .PrivateToken ,
90+ }
91+ }),
92+ fx .Provide (func (cfg Config ) handlers.Config {
93+ // Default and normalize API path/host
94+ if cfg .HTTP .API .Host == "" {
95+ cfg .HTTP .API .Path = "/api"
96+ }
97+ // Ensure leading slash and trim trailing slash (except root)
98+ if ! strings .HasPrefix (cfg .HTTP .API .Path , "/" ) {
99+ cfg .HTTP .API .Path = "/" + cfg .HTTP .API .Path
100+ }
101+ if cfg .HTTP .API .Path != "/" && strings .HasSuffix (cfg .HTTP .API .Path , "/" ) {
102+ cfg .HTTP .API .Path = strings .TrimRight (cfg .HTTP .API .Path , "/" )
103+ }
104+ // Guard against misconfigured scheme in host (accept "host[:port]" only)
105+ cfg .HTTP .API .Host = strings .TrimPrefix (strings .TrimPrefix (cfg .HTTP .API .Host , "https://" ), "http://" )
106+
107+ return handlers.Config {
108+ PublicHost : cfg .HTTP .API .Host ,
109+ PublicPath : cfg .HTTP .API .Path ,
110+ UpstreamEnabled : cfg .Gateway .Mode == GatewayModePublic ,
111+ OpenAPIEnabled : cfg .HTTP .OpenAPI .Enabled ,
112+ }
113+ }),
114+ fx .Provide (func (cfg Config ) messages.Config {
115+ return messages.Config {
116+ ProcessedLifetime : time .Duration (cfg .Messages .ProcessedLifetimeHours ) * time .Hour ,
117+ CacheTTL : time .Duration (cfg .Messages .CacheTTLSeconds ) * time .Second ,
118+ }
119+ }),
120+ fx .Provide (func (_ Config ) devices.Config {
121+ return devices.Config {
122+ UnusedLifetime : 365 * 24 * time .Hour , //TODO: make it configurable
123+ }
124+ }),
125+ fx .Provide (func (cfg Config ) sse.Config {
126+ return sse .NewConfig (
127+ sse .WithKeepAlivePeriod (time .Duration (cfg .SSE .KeepAlivePeriodSeconds ) * time .Second ),
128+ )
129+ }),
130+ fx .Provide (func (cfg Config ) cache.Config {
131+ return cache.Config {
132+ URL : cfg .Cache .URL ,
133+ }
134+ }),
135+ fx .Provide (func (cfg Config ) pubsub.Config {
136+ const bufferSize = 128
137+ return pubsub.Config {
138+ URL : cfg .PubSub .URL ,
139+ BufferSize : bufferSize ,
140+ }
141+ }),
142+ )
143+ }
0 commit comments