clean main function

This commit is contained in:
Ruidy 2024-06-03 11:21:46 +02:00
parent 8c4a1554ca
commit eda38b52a0
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
2 changed files with 27 additions and 6 deletions

View file

@ -1,5 +1,7 @@
package auth
import "errors"
type Service struct {
secret string
admin string
@ -11,12 +13,15 @@ type ProviderIndex struct {
Providers []string
}
func NewService(secret, admin, adminSecret string) *Service {
func NewService(secret, admin, adminSecret string) (*Service, error) {
if secret == "" || admin == "" || adminSecret == "" {
return nil, errors.New("error building Auth service. Verify your env variables")
}
return &Service{
secret,
admin,
adminSecret,
}
}, nil
}
func (as *Service) Authenticate(email, password string) bool {

24
main.go
View file

@ -57,12 +57,20 @@ func run(ctx context.Context, getEnv func(string) string) error {
return fmt.Errorf("error migrating the database %s", err)
}
ps, err := pdf.NewPdfService(getEnv("HTMLDOCS_PROJECT_ID"), getEnv("HTMLDOCS_REPORT_PROJECT_ID"), getEnv("HTMLDOCS_URL"), getEnv("HTMLDOCS_KEY"))
ps, err := pdf.NewPdfService(
getEnv("HTMLDOCS_PROJECT_ID"),
getEnv("HTMLDOCS_REPORT_PROJECT_ID"),
getEnv("HTMLDOCS_URL"),
getEnv("HTMLDOCS_KEY"),
)
if err != nil {
return fmt.Errorf("error starting pdf service %s", err)
}
as := auth.NewService(getEnv("SESSION_SECRET"), getEnv("ADMIN"), getEnv("ADMIN_SECRET"))
as, err := auth.NewService(getEnv("SESSION_SECRET"), getEnv("ADMIN"), getEnv("ADMIN_SECRET"))
if err != nil {
return fmt.Errorf("error starting auth service %s", err)
}
p := getEnv("PORT")
port, err := strconv.Atoi(p)
@ -74,8 +82,16 @@ func run(ctx context.Context, getEnv func(string) string) error {
origins := strings.Split(ogs, ",")
srv, err := server.New(
booking.NewService(db), as, ps, config.NewHost(),
server.WithPort(port), server.WithFileSystem(static), server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"), server.WithSecretKey(getEnv("SECRET_KEY")), server.WithOrigins(origins))
booking.NewService(db),
as,
ps,
config.NewHost(),
server.WithPort(port),
server.WithFileSystem(static),
server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"),
server.WithSecretKey(getEnv("SECRET_KEY")),
server.WithOrigins(origins),
)
if err != nil {
return fmt.Errorf("error starting server %s", err)
}