From 0e463576907209f6efe687643d16312ffd6524d9 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 6 Aug 2024 14:13:33 +0200 Subject: [PATCH] clean up --- internal/auth/service.go | 10 +++++++-- internal/calendar/service.go | 1 - internal/server/server.go | 41 ++++++++++++------------------------ main.go | 6 +++--- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/internal/auth/service.go b/internal/auth/service.go index fa902a4..d01a0ba 100644 --- a/internal/auth/service.go +++ b/internal/auth/service.go @@ -6,6 +6,7 @@ type Service struct { secret string admin string adminSecret string + apiKey string } type ProviderIndex struct { @@ -13,17 +14,22 @@ type ProviderIndex struct { Providers []string } -func NewService(secret, admin, adminSecret string) (*Service, error) { - if secret == "" || admin == "" || adminSecret == "" { +func NewService(secret, admin, adminSecret, apiKey string) (*Service, error) { + if secret == "" || admin == "" || adminSecret == "" || apiKey == "" { return nil, errors.New("error building Auth service. Verify your env variables") } return &Service{ secret, admin, adminSecret, + apiKey, }, nil } func (as *Service) Authenticate(email, password string) bool { return email == as.admin && password == as.adminSecret } + +func (as *Service) ValidateApiKey(key string) bool { + return key == as.apiKey +} diff --git a/internal/calendar/service.go b/internal/calendar/service.go index 8dd1794..2b3f3c2 100644 --- a/internal/calendar/service.go +++ b/internal/calendar/service.go @@ -52,7 +52,6 @@ func NewService(ctx context.Context, credJson string, opts ...Option) (*Service, return &Service{Service: srv}, nil } -// TODO: implement create event, list events in a period, delete event func (s *Service) Create(from, to time.Time) (*calendar.Event, error) { l := s.CalendarList.List() r, e := l.Do() diff --git a/internal/server/server.go b/internal/server/server.go index 2f196ee..9de24cd 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -25,16 +25,13 @@ import ( ) type Server struct { - Router *echo.Echo - bs *booking.Service - as *auth.Service - ps *pdf.PdfService - cs *calendar.Service - hc *config.Host - addr string - secretKey string - apiKey string - Health *HealthHandler + Router *echo.Echo + bs *booking.Service + as *auth.Service + ps *pdf.PdfService + cs *calendar.Service + hc *config.Host + addr string } type options struct { @@ -42,7 +39,6 @@ type options struct { fs *embed.FS debug *bool secretKey *string - apiKey *string origins []string } @@ -86,13 +82,6 @@ func WithOrigins(origins []string) Option { } } -func WithApiKey(apiKey string) Option { - return func(o *options) error { - o.apiKey = &apiKey - return nil - } -} - func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar.Service, hc *config.Host, opts ...Option) (*Server, error) { option := new(options) for _, opt := range opts { @@ -103,15 +92,13 @@ func New(bs *booking.Service, as *auth.Service, ps *pdf.PdfService, cs *calendar } s := &Server{ - Router: NewRouter(*option.fs, *option.debug, *option.secretKey, option.origins), - bs: bs, - as: as, - ps: ps, - cs: cs, - hc: hc, - addr: fmt.Sprintf("0.0.0.0:%d", *option.port), - secretKey: *option.secretKey, - apiKey: *option.apiKey, + Router: NewRouter(*option.fs, *option.debug, *option.secretKey, option.origins), + bs: bs, + as: as, + ps: ps, + cs: cs, + hc: hc, + addr: fmt.Sprintf("0.0.0.0:%d", *option.port), } s.MountHandlers() diff --git a/main.go b/main.go index b4a0cd8..1261e85 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func run(c context.Context, getEnv func(string) string) error { getEnv("SESSION_SECRET"), getEnv("ADMIN"), getEnv("ADMIN_SECRET"), + getEnv("API_KEY"), ) if err != nil { return fmt.Errorf("error starting auth service %s", err) @@ -105,16 +106,15 @@ func run(c context.Context, getEnv func(string) string) error { origins := strings.Split(ogs, ",") srv, err := server.New( - booking.NewService(db), + booking.NewService(db), // TODO: should validate the booking service building as, ps, cs, - config.NewHost(), + config.NewHost(), // TODO: move to the database at some point server.WithPort(port), server.WithFileSystem(static), server.WithDebug(strings.ToLower(getEnv("DEBUG")) == "true"), server.WithSecretKey(getEnv("SECRET_KEY")), - server.WithApiKey(getEnv("API_KEY")), server.WithOrigins(origins), ) if err != nil {