udemy-go-microservices/mail-service/cmd/api/main.go

57 lines
977 B
Go
Raw Normal View History

2024-09-02 18:48:30 +00:00
package main
import (
"fmt"
"log"
"net/http"
2024-09-03 19:09:34 +00:00
"os"
"strconv"
2024-09-02 18:48:30 +00:00
"github.com/tsawler/toolbox"
)
const webPort = "80"
type Config struct {
Host string
Port int
UserName string
Password string
Encryption string
Tools toolbox.Tools
2024-09-03 19:09:34 +00:00
Mailer Mail
2024-09-02 18:48:30 +00:00
}
func main() {
app := Config{
2024-09-03 19:09:34 +00:00
Mailer: createMail(),
2024-09-02 18:48:30 +00:00
}
log.Printf("Starting mail service on port %s\n", webPort)
// define http server
srv := &http.Server{
Addr: fmt.Sprintf(":%s", webPort),
Handler: app.routes(),
}
err := srv.ListenAndServe()
if err != nil {
log.Panic(err)
}
}
2024-09-03 19:09:34 +00:00
func createMail() Mail {
port, _ := strconv.Atoi(os.Getenv("MAIL_PORT"))
return Mail{
Domain: os.Getenv("MAIL_DOMAIN"),
Host: os.Getenv("MAIL_HOST"),
Port: port,
Username: os.Getenv("MAIL_USERNAME"),
Password: os.Getenv("MAIL_PASSWORD"),
Encryption: os.Getenv("MAIL_ENCRYPTION"),
FromName: os.Getenv("FROM_NAME"),
FromAddr: os.Getenv("FROM_ADDR"),
}
}