Bladeren bron

Service provider refactoring

Dima 3 maanden geleden
bovenliggende
commit
50085a8f4d
4 gewijzigde bestanden met toevoegingen van 102 en 102 verwijderingen
  1. BIN
      build/app/app
  2. 1 1
      cmd/app/main.go
  3. 88 88
      internal/fiber/fiber.go
  4. 13 13
      internal/service_provider/sp.go

BIN
build/app/app


+ 1 - 1
cmd/app/main.go

@@ -26,7 +26,7 @@ func main() {
 		log.Fatal(err)
 	}
 
-	if err = fiberApp.Listen(":" + serviceProvider.GetEnvService().AppPort()); err != nil {
+	if err = fiberApp.Listen(":" + serviceProvider.EnvService().AppPort()); err != nil {
 		log.Fatal(err)
 	}
 }

+ 88 - 88
internal/fiber/fiber.go

@@ -36,19 +36,19 @@ const (
 
 type (
 	ServiceProvider interface {
-		GetEnvService() *envService.Service
-		GetMailerService() *mailerService.Service
-		GetAuthService() *authService.Service
-		GetUserRepository() *repositories.UserRepository
-		GetPlaceRepository() *repositories.PlaceRepository
-		GetThingRepository() *repositories.ThingRepository
-		GetTagRepository() *repositories.TagRepository
-		GetPlaceThingRepository() *repositories.PlaceThingRepository
-		GetPlaceImageRepository() *repositories.PlaceImageRepository
-		GetThingImageRepository() *repositories.ThingImageRepository
-		GetThingTagRepository() *repositories.ThingTagRepository
-		GetThingNotificationRepository() *repositories.ThingNotificationRepository
-		GetFileRepository() *repositories.FileRepository
+		EnvService() *envService.Service
+		MailerService() *mailerService.Service
+		AuthService() *authService.Service
+		UserRepository() *repositories.UserRepository
+		PlaceRepository() *repositories.PlaceRepository
+		ThingRepository() *repositories.ThingRepository
+		TagRepository() *repositories.TagRepository
+		PlaceThingRepository() *repositories.PlaceThingRepository
+		PlaceImageRepository() *repositories.PlaceImageRepository
+		ThingImageRepository() *repositories.ThingImageRepository
+		ThingTagRepository() *repositories.ThingTagRepository
+		ThingNotificationRepository() *repositories.ThingNotificationRepository
+		FileRepository() *repositories.FileRepository
 	}
 )
 
@@ -73,7 +73,7 @@ func Init(sp ServiceProvider) (*fiber.App, error) {
 	// Configure Basic auth
 	basicAuth := basicauth.New(basicauth.Config{
 		Users: map[string]string{
-			sp.GetEnvService().BasicAuthUser(): sp.GetEnvService().BasicAuthPassword(),
+			sp.EnvService().BasicAuthUser(): sp.EnvService().BasicAuthPassword(),
 		},
 	})
 
@@ -107,12 +107,12 @@ func getErrorHandler(sp ServiceProvider) fiber.ErrorHandler {
 		}
 
 		if err.Error() != "" {
-			errorsEmail := sp.GetEnvService().ErrorsEmail()
+			errorsEmail := sp.EnvService().ErrorsEmail()
 
 			if errCode == fiber.StatusInternalServerError && errorsEmail != "" {
 				log.Println(err)
 				// nolint
-				sp.GetMailerService().Send(
+				sp.MailerService().Send(
 					errorsEmail,
 					"AUTO - Homethings error",
 					err.Error(),
@@ -129,7 +129,7 @@ func getErrorHandler(sp ServiceProvider) fiber.ErrorHandler {
 // nolint
 func getJWTConfig(sp ServiceProvider) fiberJwt.Config {
 	return fiberJwt.Config{
-		SigningKey: []byte(sp.GetEnvService().JWTSecretKey()),
+		SigningKey: []byte(sp.EnvService().JWTSecretKey()),
 		ErrorHandler: func(fctx *fiber.Ctx, err error) error {
 			return fiber.NewError(fiber.StatusForbidden, err.Error())
 		},
@@ -159,8 +159,8 @@ func getMetricsConfig() monitor.Config {
 
 func getCORSConfig(sp ServiceProvider) cors.Config {
 	return cors.Config{
-		AllowOrigins: sp.GetEnvService().CORSAllowOrigins(),
-		AllowMethods: sp.GetEnvService().CORSAllowMethods(),
+		AllowOrigins: sp.EnvService().CORSAllowOrigins(),
+		AllowMethods: sp.EnvService().CORSAllowMethods(),
 	}
 }
 
@@ -169,8 +169,8 @@ func registerHandlers(r fiber.Router, sp ServiceProvider) {
 	r.Post(
 		"/v1/auth/login",
 		authAPI.LoginHandler(
-			sp.GetAuthService(),
-			sp.GetUserRepository(),
+			sp.AuthService(),
+			sp.UserRepository(),
 		),
 	)
 
@@ -178,219 +178,219 @@ func registerHandlers(r fiber.Router, sp ServiceProvider) {
 	r.Get(
 		"/v1/auth/check",
 		authAPI.CheckAuthHandler(
-			sp.GetAuthService(),
-			sp.GetUserRepository(),
+			sp.AuthService(),
+			sp.UserRepository(),
 		),
 	)
 
 	r.Get(
 		"/v1/places",
-		placeAPI.GetPlacesHandler(sp.GetPlaceRepository()),
+		placeAPI.GetPlacesHandler(sp.PlaceRepository()),
 	)
 	r.Get(
 		"/v1/places/tree",
-		placeAPI.GetPlaceTreeHandler(sp.GetPlaceRepository()),
+		placeAPI.GetPlaceTreeHandler(sp.PlaceRepository()),
 	)
 	r.Get(
 		"/v1/places/:placeId<int>",
-		placeAPI.GetPlaceHandler(sp.GetPlaceRepository()),
+		placeAPI.GetPlaceHandler(sp.PlaceRepository()),
 	)
 	r.Get(
 		"/v1/places/:parentPlaceId<int>/nested",
-		placeAPI.GetNestedPlacesHandler(sp.GetPlaceRepository()),
+		placeAPI.GetNestedPlacesHandler(sp.PlaceRepository()),
 	)
 	r.Post(
 		"/v1/places",
-		placeAPI.AddPlaceHandler(sp.GetPlaceRepository()),
+		placeAPI.AddPlaceHandler(sp.PlaceRepository()),
 	)
 	r.Put(
 		"/v1/places/:placeId<int>",
-		placeAPI.UpdatePlaceHandler(sp.GetPlaceRepository()),
+		placeAPI.UpdatePlaceHandler(sp.PlaceRepository()),
 	)
 	r.Delete(
 		"/v1/places/:placeId<int>",
 		placeAPI.DeletePlaceHandler(
-			sp.GetPlaceRepository(),
-			sp.GetThingRepository(),
-			sp.GetPlaceImageRepository(),
-			sp.GetThingImageRepository(),
-			sp.GetPlaceThingRepository(),
-			sp.GetThingTagRepository(),
-			sp.GetThingImageRepository(),
-			sp.GetFileRepository(),
+			sp.PlaceRepository(),
+			sp.ThingRepository(),
+			sp.PlaceImageRepository(),
+			sp.ThingImageRepository(),
+			sp.PlaceThingRepository(),
+			sp.ThingTagRepository(),
+			sp.ThingImageRepository(),
+			sp.FileRepository(),
 		),
 	)
 
 	r.Get(
 		"/v1/things/:thingId<int>",
-		thingAPI.GetThingHandler(sp.GetThingRepository()),
+		thingAPI.GetThingHandler(sp.ThingRepository()),
 	)
 	r.Get(
 		"/v1/things/search/:search",
-		thingAPI.SearchThingHandler(sp.GetThingRepository()),
+		thingAPI.SearchThingHandler(sp.ThingRepository()),
 	)
 	r.Get(
 		"/v1/things/place/:placeId<int>",
 		thingAPI.GetPlaceThingsHandler(
-			sp.GetThingRepository(),
-			sp.GetThingTagRepository(),
+			sp.ThingRepository(),
+			sp.ThingTagRepository(),
 		),
 	)
 	r.Post(
 		"/v1/things",
 		thingAPI.AddThingHandler(
-			sp.GetThingRepository(),
-			sp.GetPlaceThingRepository(),
+			sp.ThingRepository(),
+			sp.PlaceThingRepository(),
 		),
 	)
 	r.Put(
 		"/v1/things/:thingId<int>",
 		thingAPI.UpdateThingHandler(
-			sp.GetThingRepository(),
-			sp.GetPlaceThingRepository(),
+			sp.ThingRepository(),
+			sp.PlaceThingRepository(),
 		),
 	)
 	r.Delete(
 		"/v1/things/:thingId<int>",
 		thingAPI.DeleteThingHandler(
-			sp.GetThingRepository(),
-			sp.GetThingTagRepository(),
-			sp.GetPlaceThingRepository(),
-			sp.GetThingImageRepository(),
-			sp.GetThingNotificationRepository(),
-			sp.GetFileRepository(),
+			sp.ThingRepository(),
+			sp.ThingTagRepository(),
+			sp.PlaceThingRepository(),
+			sp.ThingImageRepository(),
+			sp.ThingNotificationRepository(),
+			sp.FileRepository(),
 		),
 	)
 
 	r.Get(
 		"/v1/images/place/:placeId<int>",
 		imageAPI.GetPlaceImagesHandler(
-			sp.GetThingImageRepository(),
-			sp.GetPlaceImageRepository(),
+			sp.ThingImageRepository(),
+			sp.PlaceImageRepository(),
 		),
 	)
 	r.Get(
 		"/v1/images/thing/:thingId<int>",
-		imageAPI.GetThingImagesHandler(sp.GetThingImageRepository()))
+		imageAPI.GetThingImagesHandler(sp.ThingImageRepository()))
 	r.Post(
 		"/v1/images",
 		imageAPI.AddImageHandler(
-			sp.GetFileRepository(),
-			sp.GetThingImageRepository(),
-			sp.GetPlaceImageRepository(),
+			sp.FileRepository(),
+			sp.ThingImageRepository(),
+			sp.PlaceImageRepository(),
 		),
 	)
 	r.Delete(
 		"/v1/images/place/:imageId<int>",
 		imageAPI.DeletePlaceImageHandler(
-			sp.GetFileRepository(),
-			sp.GetPlaceImageRepository(),
+			sp.FileRepository(),
+			sp.PlaceImageRepository(),
 		),
 	)
 	r.Delete(
 		"/v1/images/thing/:imageId<int>",
 		imageAPI.DeleteThingImageHandler(
-			sp.GetFileRepository(),
-			sp.GetThingImageRepository(),
+			sp.FileRepository(),
+			sp.ThingImageRepository(),
 		),
 	)
 
 	r.Get(
 		"/v1/tags",
-		tagAPI.GetTagsHandler(sp.GetTagRepository()),
+		tagAPI.GetTagsHandler(sp.TagRepository()),
 	)
 	r.Get(
 		"/v1/tags/:tagId<int>",
-		tagAPI.GetTagHandler(sp.GetTagRepository()),
+		tagAPI.GetTagHandler(sp.TagRepository()),
 	)
 	r.Get(
 		"/v1/tags/thing/:thingId<int>",
-		tagAPI.GetThingTagsHandler(sp.GetTagRepository()),
+		tagAPI.GetThingTagsHandler(sp.TagRepository()),
 	)
 	r.Post(
 		"/v1/tags",
-		tagAPI.AddTagHandler(sp.GetTagRepository()),
+		tagAPI.AddTagHandler(sp.TagRepository()),
 	)
 	r.Post(
 		"/v1/tags/:tagId<int>/thing/:thingId<int>",
 		tagAPI.AddThingTagHandler(
-			sp.GetTagRepository(),
-			sp.GetThingRepository(),
-			sp.GetThingTagRepository(),
+			sp.TagRepository(),
+			sp.ThingRepository(),
+			sp.ThingTagRepository(),
 		),
 	)
 	r.Put(
 		"/v1/tags/:tagId<int>",
-		tagAPI.UpdateTagHandler(sp.GetTagRepository()),
+		tagAPI.UpdateTagHandler(sp.TagRepository()),
 	)
 	r.Delete(
 		"/v1/tags/:tagId<int>",
 		tagAPI.DeleteTagHandler(
-			sp.GetTagRepository(),
-			sp.GetThingTagRepository(),
+			sp.TagRepository(),
+			sp.ThingTagRepository(),
 		),
 	)
 	r.Delete(
 		"/v1/tags/:tagId<int>/thing/:thingId<int>",
 		tagAPI.DeleteThingTagHandler(
-			sp.GetTagRepository(),
-			sp.GetThingRepository(),
-			sp.GetThingTagRepository(),
+			sp.TagRepository(),
+			sp.ThingRepository(),
+			sp.ThingTagRepository(),
 		),
 	)
 	r.Get(
 		"/v1/tags/:id<int>",
-		tagAPI.GetTagHandler(sp.GetTagRepository()),
+		tagAPI.GetTagHandler(sp.TagRepository()),
 	)
 	r.Post(
 		"/v1/tags",
-		tagAPI.AddTagHandler(sp.GetTagRepository()),
+		tagAPI.AddTagHandler(sp.TagRepository()),
 	)
 	r.Put(
 		"/v1/tags/:id<int>",
-		tagAPI.UpdateTagHandler(sp.GetTagRepository()),
+		tagAPI.UpdateTagHandler(sp.TagRepository()),
 	)
 	r.Delete(
 		"/v1/tags/:id<int>",
 		tagAPI.DeleteTagHandler(
-			sp.GetTagRepository(),
-			sp.GetThingTagRepository(),
+			sp.TagRepository(),
+			sp.ThingTagRepository(),
 		),
 	)
 
 	r.Get(
 		"/v1/things/notifications/:thingId<int>",
-		notificationAPI.GetThingNotificationHandler(sp.GetThingNotificationRepository()),
+		notificationAPI.GetThingNotificationHandler(sp.ThingNotificationRepository()),
 	)
 	r.Get(
 		"/v1/things/notifications/expired",
-		notificationAPI.GetExpiredThingNotificationsHandler(sp.GetThingNotificationRepository()),
+		notificationAPI.GetExpiredThingNotificationsHandler(sp.ThingNotificationRepository()),
 	)
 	r.Post(
 		"/v1/things/notifications",
-		notificationAPI.AddThingNotificationHandler(sp.GetThingNotificationRepository()),
+		notificationAPI.AddThingNotificationHandler(sp.ThingNotificationRepository()),
 	)
 	r.Put(
 		"/v1/things/notifications/:thingId<int>",
-		notificationAPI.UpdateThingNotificationHandler(sp.GetThingNotificationRepository()),
+		notificationAPI.UpdateThingNotificationHandler(sp.ThingNotificationRepository()),
 	)
 	r.Delete(
 		"/v1/things/notifications/:thingId<int>",
-		notificationAPI.DeleteThingNotificationHandler(sp.GetThingNotificationRepository()),
+		notificationAPI.DeleteThingNotificationHandler(sp.ThingNotificationRepository()),
 	)
 
 	r.Post(
 		"/v1/users",
 		userAPI.AddUserHandler(
-			sp.GetAuthService(),
-			sp.GetUserRepository(),
+			sp.AuthService(),
+			sp.UserRepository(),
 		),
 	)
 	r.Put(
 		"/v1/users",
 		userAPI.UpdateUserHandler(
-			sp.GetAuthService(),
-			sp.GetUserRepository(),
+			sp.AuthService(),
+			sp.UserRepository(),
 		),
 	)
 }

+ 13 - 13
internal/service_provider/sp.go

@@ -65,54 +65,54 @@ func Init() (*ServiceProvider, error) {
 	return sp, nil
 }
 
-func (sp *ServiceProvider) GetEnvService() *envService.Service {
+func (sp *ServiceProvider) EnvService() *envService.Service {
 	return sp.env
 }
 
-func (sp *ServiceProvider) GetAuthService() *authService.Service {
+func (sp *ServiceProvider) AuthService() *authService.Service {
 	return sp.auth
 }
 
-func (sp *ServiceProvider) GetMailerService() *mailerService.Service {
+func (sp *ServiceProvider) MailerService() *mailerService.Service {
 	return sp.mailer
 }
 
-func (sp *ServiceProvider) GetPlaceRepository() *repositories.PlaceRepository {
+func (sp *ServiceProvider) PlaceRepository() *repositories.PlaceRepository {
 	return sp.placeRepository
 }
 
-func (sp *ServiceProvider) GetThingRepository() *repositories.ThingRepository {
+func (sp *ServiceProvider) ThingRepository() *repositories.ThingRepository {
 	return sp.thingRepository
 }
 
-func (sp *ServiceProvider) GetTagRepository() *repositories.TagRepository {
+func (sp *ServiceProvider) TagRepository() *repositories.TagRepository {
 	return sp.tagRepository
 }
 
-func (sp *ServiceProvider) GetPlaceThingRepository() *repositories.PlaceThingRepository {
+func (sp *ServiceProvider) PlaceThingRepository() *repositories.PlaceThingRepository {
 	return sp.placeThingRepository
 }
 
-func (sp *ServiceProvider) GetPlaceImageRepository() *repositories.PlaceImageRepository {
+func (sp *ServiceProvider) PlaceImageRepository() *repositories.PlaceImageRepository {
 	return sp.placeImageRepository
 }
 
-func (sp *ServiceProvider) GetThingImageRepository() *repositories.ThingImageRepository {
+func (sp *ServiceProvider) ThingImageRepository() *repositories.ThingImageRepository {
 	return sp.thingImageRepository
 }
 
-func (sp *ServiceProvider) GetThingTagRepository() *repositories.ThingTagRepository {
+func (sp *ServiceProvider) ThingTagRepository() *repositories.ThingTagRepository {
 	return sp.thingTagRepository
 }
 
-func (sp *ServiceProvider) GetThingNotificationRepository() *repositories.ThingNotificationRepository {
+func (sp *ServiceProvider) ThingNotificationRepository() *repositories.ThingNotificationRepository {
 	return sp.thingNotificationRepository
 }
 
-func (sp *ServiceProvider) GetUserRepository() *repositories.UserRepository {
+func (sp *ServiceProvider) UserRepository() *repositories.UserRepository {
 	return sp.userRepository
 }
 
-func (sp *ServiceProvider) GetFileRepository() *repositories.FileRepository {
+func (sp *ServiceProvider) FileRepository() *repositories.FileRepository {
 	return sp.fileRepository
 }