Skip to content

Commit ee31fa6

Browse files
committed
[internal] fix lint issues
1 parent 102f957 commit ee31fa6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1657
-1241
lines changed

api/requests.http

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ Content-Type: application/json
133133

134134
{
135135
"id": "MYofX8bTd5Bov0wWFZLRP",
136-
"deviceId": "C0ZGtCNf7-sXTbCtF6JXm",
137136
"url": "https://webhook.site/280a6655-eb68-40b9-b857-af5be37c5303",
138137
"event": "sms:received"
139138
}

cmd/sms-gateway/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/android-sms-gateway/server/internal/worker"
88
)
99

10+
const (
11+
cmdWorker = "worker"
12+
)
13+
1014
// @securitydefinitions.basic ApiAuth
1115
// @description User authentication
1216

@@ -36,15 +40,15 @@ import (
3640
// @host api.sms-gate.app
3741
// @schemes https
3842
//
39-
// SMSGate Backend
43+
// SMSGate Backend.
4044
func main() {
4145
args := os.Args[1:]
4246
cmd := "start"
43-
if len(args) > 0 && args[0] == "worker" {
44-
cmd = "worker"
47+
if len(args) > 0 && args[0] == cmdWorker {
48+
cmd = cmdWorker
4549
}
4650

47-
if cmd == "worker" {
51+
if cmd == cmdWorker {
4852
worker.Run()
4953
} else {
5054
smsgateway.Run()

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ require (
2525
go.uber.org/fx v1.24.0
2626
go.uber.org/zap v1.27.0
2727
golang.org/x/crypto v0.45.0
28-
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d
2928
google.golang.org/api v0.148.0
3029
gopkg.in/yaml.v3 v3.0.1
3130
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
343343
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
344344
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
345345
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
346-
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
347-
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
348346
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
349347
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
350348
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

internal/config/config.go

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Gateway struct {
2525
}
2626

2727
type HTTP struct {
28-
Listen string `yaml:"listen" envconfig:"HTTP__LISTEN"` // listen address
28+
Listen string `yaml:"listen" envconfig:"HTTP__LISTEN"` // listen address
2929
Proxies []string `yaml:"proxies" envconfig:"HTTP__PROXIES"` // proxies
3030

3131
API API `yaml:"api"`
@@ -73,7 +73,7 @@ type SSE struct {
7373
}
7474

7575
type Messages struct {
76-
CacheTTLSeconds uint16 `yaml:"cache_ttl_seconds" envconfig:"MESSAGES__CACHE_TTL_SECONDS"` // cache ttl in seconds
76+
CacheTTLSeconds uint16 `yaml:"cache_ttl_seconds" envconfig:"MESSAGES__CACHE_TTL_SECONDS"` // cache ttl in seconds
7777
HashingIntervalSeconds uint16 `yaml:"hashing_interval_seconds" envconfig:"MESSAGES__HASHING_INTERVAL_SECONDS"` // hashing interval in seconds
7878
}
7979

@@ -82,41 +82,46 @@ type Cache struct {
8282
}
8383

8484
type PubSub struct {
85-
URL string `yaml:"url" envconfig:"PUBSUB__URL"`
85+
URL string `yaml:"url" envconfig:"PUBSUB__URL"`
86+
BufferSize uint `yaml:"buffer_size" envconfig:"PUBSUB__BUFFER_SIZE"`
8687
}
8788

88-
var defaultConfig = Config{
89-
Gateway: Gateway{Mode: GatewayModePublic},
90-
HTTP: HTTP{
91-
Listen: ":3000",
92-
},
93-
Database: Database{
94-
Host: "localhost",
95-
Port: 3306,
96-
User: "sms",
97-
Password: "sms",
98-
Database: "sms",
99-
Timezone: "UTC",
100-
Debug: false,
101-
MaxOpenConns: 0,
102-
MaxIdleConns: 0,
103-
},
104-
FCM: FCMConfig{
105-
CredentialsJSON: "",
106-
DebounceSeconds: 5,
107-
TimeoutSeconds: 1,
108-
},
109-
SSE: SSE{
110-
KeepAlivePeriodSeconds: 15,
111-
},
112-
Messages: Messages{
113-
CacheTTLSeconds: 300, // 5 minutes
114-
HashingIntervalSeconds: 60,
115-
},
116-
Cache: Cache{
117-
URL: "memory://",
118-
},
119-
PubSub: PubSub{
120-
URL: "memory://",
121-
},
89+
func Default() Config {
90+
//nolint:exhaustruct,mnd // default values
91+
return Config{
92+
Gateway: Gateway{Mode: GatewayModePublic},
93+
HTTP: HTTP{
94+
Listen: ":3000",
95+
},
96+
Database: Database{
97+
Host: "localhost",
98+
Port: 3306,
99+
User: "sms",
100+
Password: "sms",
101+
Database: "sms",
102+
Timezone: "UTC",
103+
},
104+
FCM: FCMConfig{
105+
CredentialsJSON: "",
106+
},
107+
Tasks: Tasks{
108+
Hashing: HashingTask{
109+
IntervalSeconds: uint16(15 * 60),
110+
},
111+
},
112+
SSE: SSE{
113+
KeepAlivePeriodSeconds: 15,
114+
},
115+
Messages: Messages{
116+
CacheTTLSeconds: 300, // 5 minutes
117+
HashingIntervalSeconds: 60,
118+
},
119+
Cache: Cache{
120+
URL: "memory://",
121+
},
122+
PubSub: PubSub{
123+
URL: "memory://",
124+
BufferSize: 128,
125+
},
126+
}
122127
}

internal/config/module.go

Lines changed: 114 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)