Skip to content

Commit

Permalink
add router package to group all routes
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Gustavo S. Barreto <gustavo@ossystems.com.br>
  • Loading branch information
gustavosbarreto committed Apr 12, 2019
1 parent 7b477c5 commit c0468c0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
File renamed without changes.
14 changes: 14 additions & 0 deletions api/router/agentapi/routes.go
@@ -0,0 +1,14 @@
package agentapi

import (
"github.com/asdine/storm"
"github.com/labstack/echo"
)

func SetupRoutes(e *echo.Group, db *storm.DB) {
api := NewAgentAPI(db)

e.POST(GetRolloutForDeviceUrl, api.GetRolloutForDevice)
e.POST(ReportDeviceStateUrl, api.ReportDeviceState)
e.GET(GetObjectFromPackageUrl, api.GetObjectFromPackage)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions api/router/webapi/routes.go
@@ -0,0 +1,27 @@
package webapi

import (
"github.com/asdine/storm"
"github.com/labstack/echo"
"github.com/spf13/viper"
)

func SetupRoutes(e *echo.Group, db *storm.DB) {
devicesEndpoint := NewDevicesAPI(db)
e.GET(GetAllDevicesUrl, devicesEndpoint.GetAllDevices)
e.GET(GetDeviceUrl, devicesEndpoint.GetDevice)
e.GET(GetDeviceRolloutReportsUrl, devicesEndpoint.GetDeviceRolloutReports)

packagesEndpoint := NewPackagesAPI(db, viper.GetString("dir"))
e.GET(GetAllPackagesUrl, packagesEndpoint.GetAllPackages)
e.GET(GetPackageUrl, packagesEndpoint.GetPackage)
e.POST(UploadPackageUrl, packagesEndpoint.UploadPackage)

rolloutsEndpoint := NewRolloutsAPI(db)
e.GET(GetAllRolloutsUrl, rolloutsEndpoint.GetAllRollouts)
e.GET(GetRolloutUrl, rolloutsEndpoint.GetRollout)
e.GET(GetRolloutStatisticsUrl, rolloutsEndpoint.GetRolloutStatistics)
e.GET(GetRolloutDevicesUrl, rolloutsEndpoint.GetRolloutDevices)
e.POST(CreateRolloutUrl, rolloutsEndpoint.CreateRollout)
e.PUT(StopRolloutUrl, rolloutsEndpoint.StopRollout)
}
28 changes: 4 additions & 24 deletions main.go
Expand Up @@ -16,8 +16,8 @@ import (
"syscall"
"time"

"github.com/UpdateHub/updatehub-ce/api/agentapi"
"github.com/UpdateHub/updatehub-ce/api/webapi"
"github.com/UpdateHub/updatehub-ce/api/router/agentapi"
"github.com/UpdateHub/updatehub-ce/api/router/webapi"
"github.com/asdine/storm"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gobuffalo/packr"
Expand Down Expand Up @@ -102,31 +102,11 @@ func execute(cmd *cobra.Command, args []string) {
return echo.ErrUnauthorized
})

agentApi := agentapi.NewAgentAPI(db)
e.POST(agentapi.GetRolloutForDeviceUrl, agentApi.GetRolloutForDevice)
e.POST(agentapi.ReportDeviceStateUrl, agentApi.ReportDeviceState)
e.GET(agentapi.GetObjectFromPackageUrl, agentApi.GetObjectFromPackage)

api := e.Group("/api")
api.Use(middleware.JWT([]byte(viper.GetString("secret"))))

devicesEndpoint := webapi.NewDevicesAPI(db)
api.GET(webapi.GetAllDevicesUrl, devicesEndpoint.GetAllDevices)
api.GET(webapi.GetDeviceUrl, devicesEndpoint.GetDevice)
api.GET(webapi.GetDeviceRolloutReportsUrl, devicesEndpoint.GetDeviceRolloutReports)

packagesEndpoint := webapi.NewPackagesAPI(db, viper.GetString("dir"))
api.GET(webapi.GetAllPackagesUrl, packagesEndpoint.GetAllPackages)
api.GET(webapi.GetPackageUrl, packagesEndpoint.GetPackage)
api.POST(webapi.UploadPackageUrl, packagesEndpoint.UploadPackage)

rolloutsEndpoint := webapi.NewRolloutsAPI(db)
api.GET(webapi.GetAllRolloutsUrl, rolloutsEndpoint.GetAllRollouts)
api.GET(webapi.GetRolloutUrl, rolloutsEndpoint.GetRollout)
api.GET(webapi.GetRolloutStatisticsUrl, rolloutsEndpoint.GetRolloutStatistics)
api.GET(webapi.GetRolloutDevicesUrl, rolloutsEndpoint.GetRolloutDevices)
api.POST(webapi.CreateRolloutUrl, rolloutsEndpoint.CreateRollout)
api.PUT(webapi.StopRolloutUrl, rolloutsEndpoint.StopRollout)
agentapi.SetupRoutes(e.Group(""), db)
webapi.SetupRoutes(api, db)

if os.Getenv("ENV") == "development" {
ui, _ := url.Parse("http://localhost:1314/")
Expand Down

0 comments on commit c0468c0

Please sign in to comment.