Skip to content

Commit

Permalink
router refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Sep 10, 2023
1 parent e5c67c0 commit d6d79f2
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,22 @@ type Router struct {
func NewRouter(about types.About) Router {
gin.SetMode(gin.ReleaseMode)

router := Router{}

r := gin.New()
r.Use(cors.Default())
r.Use(ginutils.Logger("MAIN"))
r.Use(gin.Recovery())
r.Use(middleware.ErrorMiddleware())
r.Use(static.Serve("/", static.LocalFile(path.Join(".", storage.Path, "client", "dist"), true)))
r.GET("/ping", handlePing)

runnerDockerAdapter = adapter.NewRunnerDockerAdapter()
runnerFSAdapter = adapter.NewRunnerFSAdapter()
instanceFSAdapter = adapter.NewInstanceFSAdapter()
instanceLogsFSAdapter = adapter.NewInstanceLogsFSAdapter()
eventInMemoryAdapter = adapter.NewEventInMemoryAdapter()
packageFSAdapter = adapter.NewPackageFSAdapter(nil)
serviceFSAdapter = adapter.NewServiceFSAdapter(nil)
proxyFSAdapter = adapter.NewProxyFSAdapter(nil)
settingsFSAdapter = adapter.NewSettingsFSAdapter(nil)

proxyService = services.NewProxyService(proxyFSAdapter)
notificationsService = services.NewNotificationsService(settingsFSAdapter, eventInMemoryAdapter, instanceFSAdapter)
instanceService = services.NewInstanceService(serviceFSAdapter, instanceFSAdapter, runnerDockerAdapter, runnerFSAdapter, instanceLogsFSAdapter, eventInMemoryAdapter)
packageService = services.NewPackageService(packageFSAdapter)
serviceService = services.NewServiceService(serviceFSAdapter)
updateService = services.NewUpdateDependenciesService(about.Version)
settingsService = services.NewSettingsService(settingsFSAdapter)

api := r.Group("/api")
api.GET("/ping", handlePing)
api.GET("/about", func(c *gin.Context) {
c.JSON(http.StatusOK, about)
})
r := Router{
engine: gin.New(),
}

addServicesRoutes(api.Group("/services"))
addInstancesRoutes(api.Group("/instances"))
addInstanceRoutes(api.Group("/instance/:instance_uuid"))
addPackagesRoutes(api.Group("/packages"))
addProxyRoutes(api.Group("/proxy"))
addUpdatesRoutes(api.Group("/updates"))
addSettingsRoutes(api.Group("/settings"))
r.engine.Use(cors.Default())
r.engine.Use(ginutils.Logger("MAIN"))
r.engine.Use(gin.Recovery())
r.engine.Use(middleware.ErrorMiddleware())
r.engine.Use(static.Serve("/", static.LocalFile(path.Join(".", storage.Path, "client", "dist"), true)))
r.engine.GET("/ping", handlePing)

router.engine = r
r.initAdapters()
r.initServices(about)
r.initAPIRoutes(about)

return router
return r
}

func (r *Router) Start(addr string) {
Expand Down Expand Up @@ -158,6 +129,44 @@ func (r *Router) handleSignals() {
}()
}

func (r *Router) initAdapters() {
runnerDockerAdapter = adapter.NewRunnerDockerAdapter()
runnerFSAdapter = adapter.NewRunnerFSAdapter()
instanceFSAdapter = adapter.NewInstanceFSAdapter()
instanceLogsFSAdapter = adapter.NewInstanceLogsFSAdapter()
eventInMemoryAdapter = adapter.NewEventInMemoryAdapter()
packageFSAdapter = adapter.NewPackageFSAdapter(nil)
serviceFSAdapter = adapter.NewServiceFSAdapter(nil)
proxyFSAdapter = adapter.NewProxyFSAdapter(nil)
settingsFSAdapter = adapter.NewSettingsFSAdapter(nil)
}

func (r *Router) initServices(about types.About) {
proxyService = services.NewProxyService(proxyFSAdapter)
notificationsService = services.NewNotificationsService(settingsFSAdapter, eventInMemoryAdapter, instanceFSAdapter)
instanceService = services.NewInstanceService(serviceFSAdapter, instanceFSAdapter, runnerDockerAdapter, runnerFSAdapter, instanceLogsFSAdapter, eventInMemoryAdapter)
packageService = services.NewPackageService(packageFSAdapter)
serviceService = services.NewServiceService(serviceFSAdapter)
updateService = services.NewUpdateDependenciesService(about.Version)
settingsService = services.NewSettingsService(settingsFSAdapter)
}

func (r *Router) initAPIRoutes(about types.About) {
api := r.engine.Group("/api")
api.GET("/ping", handlePing)
api.GET("/about", func(c *gin.Context) {
c.JSON(http.StatusOK, about)
})

addServicesRoutes(api.Group("/services"))
addInstancesRoutes(api.Group("/instances"))
addInstanceRoutes(api.Group("/instance/:instance_uuid"))
addPackagesRoutes(api.Group("/packages"))
addProxyRoutes(api.Group("/proxy"))
addUpdatesRoutes(api.Group("/updates"))
addSettingsRoutes(api.Group("/settings"))
}

func headersSSE(c *gin.Context) {
c.Writer.Header().Set("Content-Type", sse.ContentType)
c.Writer.Header().Set("Cache-Control", "no-cache")
Expand Down

0 comments on commit d6d79f2

Please sign in to comment.