// NewServer creates new instance of the application.
NewServer creates new instance of the application.
// NewServer creates new instance of the application .
func NewServer(c *ServerConfig) (*Server, error) {
	registryConfig := &service.RegistryConfig{
		MgoSession:    c.MgoSession,
		HydraAdminApi: c.HydraAdminApi,
		MfaService:    c.MfaService,
		RedisClient:   c.RedisClient,
		Mailer:        service.NewMailer(c.Mailer),
	}
	server := &Server{
		Echo:          echo.New(),
		RedisHandler:  c.RedisClient,
		ServerConfig:  c.ApiConfig,
		SessionConfig: c.SessionConfig,
		HydraConfig:   c.HydraConfig,
		Registry:      service.NewRegistryBase(registryConfig),
	}
	t := &Template{
		templates: template.Must(template.ParseGlob("public/templates/*.html")),
	}
	server.Echo.Renderer = t
	server.Echo.HTTPErrorHandler = helper.ErrorHandler
	server.Echo.Use(ZapLogger(zap.L()))
	server.Echo.Use(middleware.Recover())
	server.Echo.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowHeaders:     []string{echo.HeaderAuthorization, echo.HeaderContentType, echo.HeaderOrigin, echo.HeaderAccept},
		AllowOrigins:     c.ApiConfig.AllowOrigins,
		AllowCredentials: c.ApiConfig.AllowCredentials,
		AllowMethods:     []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete, http.MethodOptions},
	}))
	server.Echo.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
		TokenLookup:    "header:X-XSRF-TOKEN",
		CookieName:     "_csrf",
		Skipper:        csrfSkipper,
		CookieSameSite: http.SameSiteNoneMode,
	}))
	server.Echo.Use(session.Middleware(c.SessionStore))
	server.Echo.Use(middleware.RequestID())
	server.Echo.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(ctx echo.Context) error {
			db := c.MgoSession.Copy()
			defer db.Close()
			ctx.Set("database", db)
			logger := zap.L().With(
				zap.String(
					echo.HeaderXRequestID,
					ctx.Response().Header().Get(echo.HeaderXRequestID),
				),
			)
			ctx.Set("logger", logger)
			return next(ctx)
		}
	})
	registerCustomValidator(server.Echo)
	if err := server.setupRoutes(); err != nil {
		zap.L().Fatal("Setup routes failed", zap.Error(err))
	}
	return server, nil
}
func NewServer ( c * ServerConfig ) ( * Server , error ) { registryConfig := & service . RegistryConfig { MgoSession : c . MgoSession , HydraAdminApi : c . HydraAdminApi , MfaService : c . MfaService , RedisClient : c . RedisClient , Mailer : service . NewMailer ( c . Mailer ) , } server := & Server { Echo : echo . New ( ) , RedisHandler : c . RedisClient , ServerConfig : c . ApiConfig , SessionConfig : c . SessionConfig , HydraConfig : c . HydraConfig , Registry : service . NewRegistryBase ( registryConfig ) , } t := & Template { templates : template . Must ( template . ParseGlob ( " " ) ) , } server . Echo . Renderer = t server . Echo . HTTPErrorHandler = helper . ErrorHandler server . Echo . Use ( ZapLogger ( zap . L ( ) ) ) server . Echo . Use ( middleware . Recover ( ) ) server . Echo . Use ( middleware . CORSWithConfig ( middleware . CORSConfig { AllowHeaders : [ ] string { echo . HeaderAuthorization , echo . HeaderContentType , echo . HeaderOrigin , echo . HeaderAccept } , AllowOrigins : c . ApiConfig . AllowOrigins , AllowCredentials : c . ApiConfig . AllowCredentials , AllowMethods : [ ] string { http . MethodGet , http . MethodHead , http . MethodPut , http . MethodPatch , http . MethodPost , http . MethodDelete , http . MethodOptions } , } ) ) server . Echo . Use ( middleware . CSRFWithConfig ( middleware . CSRFConfig { TokenLookup : " " , CookieName : " " , Skipper : csrfSkipper , CookieSameSite : http . SameSiteNoneMode , } ) ) server . Echo . Use ( session . Middleware ( c . SessionStore ) ) server . Echo . Use ( middleware . RequestID ( ) ) server . Echo . Use ( func ( next echo . HandlerFunc ) echo . HandlerFunc { return func ( ctx echo . Context ) error { db := c . MgoSession . Copy ( ) defer db . Close ( ) ctx . Set ( " " , db ) logger := zap . L ( ) . With ( zap . String ( echo . HeaderXRequestID , ctx . Response ( ) . Header ( ) . Get ( echo . HeaderXRequestID ) , ) , ) ctx . Set ( " " , logger ) return next ( ctx ) } } ) registerCustomValidator ( server . Echo ) if err := server . setupRoutes ( ) ; err != nil { zap . L ( ) . Fatal ( " " , zap . Error ( err ) ) } return server , nil }
NewServer creates new instance of the application.
NewServer creates new instance of the application .
// TODO: Validate origins for each application by settings