Skip to content

Commit 30d3aa0

Browse files
committed
fix
1 parent ab52985 commit 30d3aa0

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

emails/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *Session) Logout() error {
9090

9191
func CreateSMTPServer(
9292
host string,
93-
port string,
93+
port int,
9494
username string,
9595
password string,
9696
queueSizeString string,
@@ -105,7 +105,7 @@ func CreateSMTPServer(
105105
Emails: make(chan Email, queueSize),
106106
}
107107
s := smtp.NewServer(be)
108-
s.Addr = host + ":" + port
108+
s.Addr = host + ":" + strconv.Itoa(port)
109109
s.Domain = host
110110
s.WriteTimeout = 10 * time.Second
111111
s.ReadTimeout = 10 * time.Second

main.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"log"
55
"os"
6+
"strconv"
67
"sync"
78

89
"github.com/Voldemat/go-smtp-mock/server"
@@ -17,8 +18,12 @@ func main() {
1718
if smtpHost == "" {
1819
log.Fatal("SMTP_HOST is not defined")
1920
}
20-
smtpPort := os.Getenv("SMTP_PORT")
21-
if smtpPort == "" {
21+
smtpPortString := os.Getenv("SMTP_PORT")
22+
if smtpPortString == "" {
23+
log.Fatal("SMTP_PORT is not defined")
24+
}
25+
smtpPort, err := strconv.ParseInt(smtpPortString, 10, 64)
26+
if err != nil {
2227
log.Fatal("SMTP_PORT is not defined")
2328
}
2429
smtpUser := os.Getenv("SMTP_USER")
@@ -29,18 +34,27 @@ func main() {
2934
if smtpPassword == "" {
3035
log.Fatal("SMTP_PASSWORD is not defined")
3136
}
37+
httpPortString := os.Getenv("SMTP_HTTP_PORT")
38+
if httpPortString == "" {
39+
log.Fatal("SMTP_HTTP_PORT is not defined")
40+
}
41+
httpPort, err := strconv.ParseInt(httpPortString, 10, 64)
42+
if err != nil {
43+
log.Fatal("SMTP_HTTP_PORT is not defined")
44+
}
3245
var wg sync.WaitGroup
3346
wg.Add(2)
3447

35-
server.CreateServerRoutines(
36-
&wg,
37-
queueSize,
38-
smtpHost,
39-
smtpPort,
40-
smtpUser,
41-
smtpPassword,
42-
os.Getenv("HTTP_HOST"),
43-
os.Getenv("HTTP_PORT"),
44-
)
45-
wg.Wait()
48+
server.CreateServerRoutines(server.CreateServerRoutinesArgs{
49+
Wg: &wg,
50+
SmtpHost: smtpHost,
51+
SmtpPort: int(smtpPort),
52+
SmtpUser: smtpUser,
53+
SmtpPassword: smtpPassword,
54+
HttpHost: os.Getenv("HTTP_HOST"),
55+
HttpPort: int(httpPort),
56+
QueueSize: queueSize,
57+
},
58+
)
59+
wg.Wait()
4660
}

server/server.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,45 @@ package server
33
import (
44
"log"
55
"net/http"
6+
"strconv"
67
"sync"
78

89
"github.com/Voldemat/go-smtp-mock/emails"
910
)
1011

12+
type CreateServerRoutinesArgs struct {
13+
Wg *sync.WaitGroup
14+
SmtpHost string
15+
SmtpPort int
16+
SmtpUser string
17+
SmtpPassword string
18+
HttpHost string
19+
HttpPort int
20+
QueueSize string
21+
}
22+
1123
func CreateServerRoutines(
12-
wg *sync.WaitGroup,
13-
smtpHost string,
14-
smtpUser string,
15-
smtpPort string,
16-
smtpPassword string,
17-
queueSize string,
18-
httpHost string,
19-
httpPort string,
24+
args CreateServerRoutinesArgs,
2025
) *emails.Backend {
2126
backend, server := emails.CreateSMTPServer(
22-
smtpHost,
23-
smtpPort,
24-
smtpUser,
25-
smtpPassword,
26-
queueSize,
27+
args.SmtpHost,
28+
args.SmtpPort,
29+
args.SmtpUser,
30+
args.SmtpPassword,
31+
args.QueueSize,
2732
)
2833
mux := CreateHTTPServer(backend)
29-
30-
wg.Add(2)
31-
34+
args.Wg.Add(2)
3235
go func() {
33-
defer wg.Done()
36+
defer args.Wg.Done()
3437
http.ListenAndServe(
35-
httpHost + ":" + httpPort,
38+
args.HttpHost + ":" + strconv.Itoa(args.HttpPort),
3639
mux,
3740
)
3841
}()
3942

4043
go func() {
41-
defer wg.Done()
44+
defer args.Wg.Done()
4245
err := server.ListenAndServe()
4346
if err != nil {
4447
log.Fatal(err)

0 commit comments

Comments
 (0)