service

package
v0.0.0-...-7f35424 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package service provides a way to create a web service using other packages in Alya framework.

It provides a way to inject dependencies into the service.

It can can be used to build a RESTful API server where each resource can be developed as a service. To help with that it supports creation of route groups and sub-groups. Each group can have its own routes and middleware. It allows grouping of routes by functionality.

Example

Example demonstrates how to create a new service and register routes.

package main

import ()

func main() {
	// // Shared router
	// router := gin.Default()

	// // UserService with authentication middleware
	// userService := NewService(router).WithLogger(logger).WithDatabase(db)

	// // Version 1 of UserService
	// userGroupV1 := userService.CreateGroup("/v1/user")
	// userGroupV1.Group.Use(authMiddleware) // Apply auth middleware to userGroupV1

	// // Register routes for version 1 of UserService
	// userGroupV1.RegisterRoute(http.MethodGet, "/profile", userProfileHandlerV1)         // Endpoint: GET /v1/user/profile
	// userGroupV1.RegisterRoute(http.MethodPut, "/profile", userUpdateProfileHandlerV1)   // Endpoint: PUT /v1/user/profile
	// userGroupV1.RegisterRoute(http.MethodGet, "/settings", userSettingsHandlerV1)       // Endpoint: GET /v1/user/settings
	// userGroupV1.RegisterRoute(http.MethodPut, "/settings", userUpdateSettingsHandlerV1) // Endpoint: PUT /v1/user/settings

	// // Version 2 of UserService
	// userGroupV2 := userService.CreateGroup("/v2/user")
	// userGroupV2.Group.Use(authMiddleware) // Apply auth middleware to userGroupV2

	// // Register routes for version 2 of UserService
	// userGroupV2.RegisterRoute(http.MethodGet, "/profile", userProfileHandlerV2)         // Endpoint: GET /v2/user/profile
	// userGroupV2.RegisterRoute(http.MethodPut, "/profile", userUpdateProfileHandlerV2)   // Endpoint: PUT /v2/user/profile
	// userGroupV2.RegisterRoute(http.MethodGet, "/settings", userSettingsHandlerV2)       // Endpoint: GET /v2/user/settings
	// userGroupV2.RegisterRoute(http.MethodPut, "/settings", userUpdateSettingsHandlerV2) // Endpoint: PUT /v2/user/settings

	// // BlogService without authentication middleware
	// blogService := NewService(router).WithLogger(logger).WithDatabase(db)

	// // BlogService group
	// blogGroup := blogService.CreateGroup("/blog")

	// // Register routes for BlogService
	// blogGroup.RegisterRoute(http.MethodGet, "/posts", blogPostsHandler)       // Endpoint: GET /blog/posts
	// blogGroup.RegisterRoute(http.MethodPost, "/posts", blogCreatePostHandler) // Endpoint: POST /blog/posts

	// // Run the server on port 8080
	// router.Run(":8080")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dependencies

type Dependencies map[string]any

Dependencies is a map to hold arbitrary dependencies.

type HandlerFunc

type HandlerFunc func(*gin.Context, *Service)

HandlerFunc is a function that handles a request. It takes a *gin.Context and a *Service as parameters.

type RouteGroup

type RouteGroup struct {
	Group *gin.RouterGroup
}

RouteGroup represents a group of routes.

func (*RouteGroup) CreateSubGroup

func (g *RouteGroup) CreateSubGroup(path string) *RouteGroup

CreateSubGroup creates a new sub-group within the current group.

func (*RouteGroup) RegisterRoute

func (g *RouteGroup) RegisterRoute(method, path string, handler gin.HandlerFunc)

RegisterRoute allows for the registration of a single route to the route group.

type Service

type Service struct {
	Config       config.Config
	RigelConfig  *rigel.Rigel
	Router       *gin.Engine
	Logger       logger.Logger
	LogHarbour   *logharbour.Logger
	Database     any
	Dependencies Dependencies
}

Service is the core struct for a web service, holding essential components and optional dependencies. It also provides a map to hold arbitrary dependencies. It allows injecttion of any additional components that a service might need. Note: Assert the type of the dependency before using it because the value is of type any.

Example:

  redisClient := // create Redis client
  s := NewService(cfg, router).WithDependency("redis", redisClient)
  value, ok := s.Dependencies["redis"]
	 if !ok {
		 Handle missing Redis client
	 }

The Service struct also provides a set of With... methods to inject specific dependencies.

Example:

s := NewService(router).WithLogger(logger).WithDatabase(db)

func NewService

func NewService(r *gin.Engine) *Service

NewService constructs a new Service with the given configuration, router, and options.

func (*Service) CreateGroup

func (s *Service) CreateGroup(path string) *RouteGroup

CreateGroup creates a new route group with the given path.

func (*Service) RegisterRoute

func (s *Service) RegisterRoute(method, path string, handler HandlerFunc)

RegisterRoute allows for the registration of a single route directly on the service's engine.

func (*Service) WithConfig

func (s *Service) WithConfig(c config.Config) *Service

WithConfig is a method to inject a alya.Config dependency into the Service.

func (*Service) WithDatabase

func (s *Service) WithDatabase(db any) *Service

WithDatabase is a method to inject a database dependency into the Service.

func (*Service) WithDependency

func (s *Service) WithDependency(key string, value any) *Service

WithDependency is a method to inject an arbitrary dependency into the Service.

func (*Service) WithLogHarbour

func (s *Service) WithLogHarbour(l *logharbour.Logger) *Service

WithLogHarbour is a method to inject a logharbour (github.com/remiges-tech/logharbour) dependency into the Service.

func (*Service) WithLogger

func (s *Service) WithLogger(l logger.Logger) *Service

WithLogger is a method to inject a logger dependency into the Service.

func (*Service) WithRigelConfig

func (s *Service) WithRigelConfig(c *rigel.Rigel) *Service

WithConfig is a method to inject a rigel (github.com/remiges-tech/rigel) dependency into the Service.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL