Skip to content

Commit f66879e

Browse files
refactor brick service (#524)
1 parent 3c75b40 commit f66879e

File tree

9 files changed

+160
-156
lines changed

9 files changed

+160
-156
lines changed

cmd/arduino-app-cli/brick/details.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
1111
"github.com/bcmi-labs/orchestrator/cmd/feedback"
12-
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
12+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricks"
1313
)
1414

1515
func newBricksDetailsCmd() *cobra.Command {
@@ -24,13 +24,9 @@ func newBricksDetailsCmd() *cobra.Command {
2424
}
2525

2626
func bricksDetailsHandler(id string) {
27-
res, err := orchestrator.BricksDetails(
28-
servicelocator.GetStaticStore(),
29-
servicelocator.GetBricksIndex(),
30-
id,
31-
)
27+
res, err := servicelocator.GetBrickService().BricksDetails(id)
3228
if err != nil {
33-
if errors.Is(err, orchestrator.ErrBrickNotFound) {
29+
if errors.Is(err, bricks.ErrBrickNotFound) {
3430
feedback.Fatal(err.Error(), feedback.ErrBadArgument)
3531
} else {
3632
feedback.Fatal(err.Error(), feedback.ErrGeneric)
@@ -43,18 +39,18 @@ func bricksDetailsHandler(id string) {
4339
}
4440

4541
type brickDetailsResult struct {
46-
BrickDetailsResult orchestrator.BrickDetailsResult
42+
BrickDetailsResult bricks.BrickDetailsResult
4743
}
4844

4945
func (r brickDetailsResult) String() string {
5046
b := &strings.Builder{}
5147

52-
fmt.Fprintf(b, "Name: %s\n", r.BrickDetailsResult.Name)
53-
fmt.Fprintf(b, "ID: %s\n", r.BrickDetailsResult.ID)
54-
fmt.Fprintf(b, "Author: %s\n", r.BrickDetailsResult.Author)
55-
fmt.Fprintf(b, "Category: %s\n", r.BrickDetailsResult.Category)
56-
fmt.Fprintf(b, "Status: %s\n", r.BrickDetailsResult.Status)
57-
fmt.Fprintf(b, "\nDescription:\n%s\n", r.BrickDetailsResult.Description)
48+
b.WriteString("Name: " + r.BrickDetailsResult.Name + "\n")
49+
b.WriteString("ID: " + r.BrickDetailsResult.ID + "\n")
50+
b.WriteString("Author: " + r.BrickDetailsResult.Author + "\n")
51+
b.WriteString("Category: " + r.BrickDetailsResult.Category + "\n")
52+
b.WriteString("Status: " + r.BrickDetailsResult.Status + "\n")
53+
b.WriteString("\nDescription:\n" + r.BrickDetailsResult.Description + "\n")
5854

5955
if len(r.BrickDetailsResult.Variables) > 0 {
6056
b.WriteString("\nVariables:\n")

cmd/arduino-app-cli/brick/list.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
88
"github.com/bcmi-labs/orchestrator/cmd/feedback"
9-
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
9+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricks"
1010
"github.com/bcmi-labs/orchestrator/pkg/tablestyle"
1111
)
1212

@@ -20,18 +20,15 @@ func newBricksListCmd() *cobra.Command {
2020
}
2121
}
2222
func bricksListHandler() {
23-
res, err := orchestrator.BricksList(
24-
servicelocator.GetModelsIndex(),
25-
servicelocator.GetBricksIndex(),
26-
)
23+
res, err := servicelocator.GetBrickService().List()
2724
if err != nil {
2825
feedback.Fatal(err.Error(), feedback.ErrGeneric)
2926
}
3027
feedback.PrintResult(brickListResult{Bricks: res.Bricks})
3128
}
3229

3330
type brickListResult struct {
34-
Bricks []orchestrator.BrickListItem `json:"bricks"`
31+
Bricks []bricks.BrickListItem `json:"bricks"`
3532
}
3633

3734
func (r brickListResult) String() string {

cmd/arduino-app-cli/daemon/daemon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func httpHandler(ctx context.Context, daemonPort, version string) {
6363
servicelocator.GetStaticStore(),
6464
servicelocator.GetModelsIndex(),
6565
servicelocator.GetBricksIndex(),
66+
servicelocator.GetBrickService(),
6667
)
6768

6869
corsMiddlware, err := cors.NewMiddleware(

cmd/arduino-app-cli/internal/servicelocator/servicelocator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go.bug.st/f"
1515

1616
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
17+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricks"
1718
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
1819
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
1920
"github.com/bcmi-labs/orchestrator/internal/store"
@@ -93,6 +94,14 @@ var (
9394
GetStaticStore = sync.OnceValue(func() *store.StaticStore {
9495
return store.NewStaticStore(RunnerVersion)
9596
})
97+
98+
GetBrickService = sync.OnceValue(func() *bricks.Service {
99+
return bricks.NewService(
100+
GetModelsIndex(),
101+
GetBricksIndex(),
102+
GetStaticStore(),
103+
)
104+
})
96105
)
97106

98107
func getPythonImageAndTag() (string, string) {

cmd/gendoc/docs.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/bcmi-labs/orchestrator/internal/api/handlers"
1818
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
19+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricks"
1920
"github.com/bcmi-labs/orchestrator/internal/update"
2021
)
2122

@@ -527,7 +528,7 @@ Contains a JSON object with the details of an error.
527528
})(nil),
528529
CustomSuccessResponse: &CustomResponseDef{
529530
ContentType: "application/json",
530-
DataStructure: orchestrator.BrickDetailsResult{},
531+
DataStructure: bricks.BrickDetailsResult{},
531532
Description: "Successful response",
532533
StatusCode: http.StatusOK,
533534
},
@@ -546,7 +547,7 @@ Contains a JSON object with the details of an error.
546547
Request: nil,
547548
CustomSuccessResponse: &CustomResponseDef{
548549
ContentType: "application/json",
549-
DataStructure: orchestrator.BrickListResult{},
550+
DataStructure: bricks.BrickListResult{},
550551
Description: "Successful response",
551552
StatusCode: http.StatusOK,
552553
},
@@ -756,7 +757,7 @@ Contains a JSON object with the details of an error.
756757
})(nil),
757758
CustomSuccessResponse: &CustomResponseDef{
758759
ContentType: "application/json",
759-
DataStructure: orchestrator.AppBrickInstancesResult{},
760+
DataStructure: bricks.AppBrickInstancesResult{},
760761
Description: "Successful response",
761762
StatusCode: http.StatusOK,
762763
},
@@ -778,7 +779,7 @@ Contains a JSON object with the details of an error.
778779
})(nil),
779780
CustomSuccessResponse: &CustomResponseDef{
780781
ContentType: "application/json",
781-
DataStructure: orchestrator.BrickInstance{},
782+
DataStructure: bricks.BrickInstance{},
782783
Description: "Successful response",
783784
StatusCode: http.StatusOK,
784785
},
@@ -799,7 +800,7 @@ Contains a JSON object with the details of an error.
799800
ID string `path:"appID" description:"application identifier."`
800801
BrickID string `path:"brickID" description:"brick identifier."`
801802
})(nil),
802-
Request: orchestrator.BrickCreateUpdateRequest{},
803+
Request: bricks.BrickCreateUpdateRequest{},
803804
CustomSuccessResponse: &CustomResponseDef{
804805
Description: "Successful response",
805806
StatusCode: http.StatusOK,
@@ -821,7 +822,7 @@ Contains a JSON object with the details of an error.
821822
ID string `path:"appID" description:"application identifier."`
822823
BrickID string `path:"brickID" description:"brick identifier."`
823824
})(nil),
824-
Request: orchestrator.BrickCreateUpdateRequest{},
825+
Request: bricks.BrickCreateUpdateRequest{},
825826
CustomSuccessResponse: &CustomResponseDef{
826827
Description: "Successful response",
827828
StatusCode: http.StatusOK,

internal/api/api.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/bcmi-labs/orchestrator/internal/api/handlers"
88
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
9+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricks"
910
"github.com/bcmi-labs/orchestrator/internal/orchestrator/bricksindex"
1011
"github.com/bcmi-labs/orchestrator/internal/orchestrator/modelsindex"
1112
"github.com/bcmi-labs/orchestrator/internal/store"
@@ -27,14 +28,15 @@ func NewHTTPRouter(
2728
staticStore *store.StaticStore,
2829
modelsIndex *modelsindex.ModelsIndex,
2930
bricksIndex *bricksindex.BricksIndex,
31+
brickService *bricks.Service,
3032
) http.Handler {
3133
mux := http.NewServeMux()
3234
mux.Handle("GET /debug/", http.DefaultServeMux) // pprof endpoints
3335

3436
mux.Handle("GET /v1/version", handlers.HandlerVersion(version))
3537
mux.Handle("GET /v1/config", handlers.HandleConfig())
36-
mux.Handle("GET /v1/bricks", handlers.HandleBrickList(modelsIndex, bricksIndex))
37-
mux.Handle("GET /v1/bricks/{brickID}", handlers.HandleBrickDetails(staticStore, bricksIndex))
38+
mux.Handle("GET /v1/bricks", handlers.HandleBrickList(brickService))
39+
mux.Handle("GET /v1/bricks/{brickID}", handlers.HandleBrickDetails(brickService))
3840

3941
mux.Handle("GET /v1/system/update/check", handlers.HandleCheckUpgradable(updater))
4042
mux.Handle("GET /v1/system/update/events", handlers.HandleUpdateEvents(updater))
@@ -56,11 +58,11 @@ func NewHTTPRouter(
5658
mux.Handle("DELETE /v1/apps/{appID}", handlers.HandleAppDelete())
5759
mux.Handle("GET /v1/apps/{appID}/exposed-ports", handlers.HandleAppPorts(bricksIndex))
5860

59-
mux.Handle("GET /v1/apps/{appID}/bricks", handlers.HandleAppBrickInstancesList(bricksIndex))
60-
mux.Handle("GET /v1/apps/{appID}/bricks/{brickID}", handlers.HandleAppBrickInstanceDetails(bricksIndex))
61-
mux.Handle("PUT /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickCreate(modelsIndex, bricksIndex))
62-
mux.Handle("PATCH /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickUpdates(modelsIndex, bricksIndex))
63-
mux.Handle("DELETE /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickDelete(bricksIndex))
61+
mux.Handle("GET /v1/apps/{appID}/bricks", handlers.HandleAppBrickInstancesList(brickService))
62+
mux.Handle("GET /v1/apps/{appID}/bricks/{brickID}", handlers.HandleAppBrickInstanceDetails(brickService))
63+
mux.Handle("PUT /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickCreate(brickService))
64+
mux.Handle("PATCH /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickUpdates(brickService))
65+
mux.Handle("DELETE /v1/apps/{appID}/bricks/{brickID}", handlers.HandleBrickDelete(brickService))
6466

6567
mux.Handle("GET /v1/docs/", http.StripPrefix("/v1/docs/", handlers.DocsServer(docsFS)))
6668

0 commit comments

Comments
 (0)