Skip to content

Commit

Permalink
start making services
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Sep 10, 2024
1 parent 498464c commit 22ab669
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
29 changes: 29 additions & 0 deletions languages/go/go-server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,32 @@ func RegisterDef[TContent any](app *App[TContent], input any) {
Value: *def,
})
}

type Service struct {
Name string
Procedures map[string]RpcDef
Definitions map[string]TypeDef
rawProcedures []any
}

func NewService(name string) Service {
return Service{
Name: name,
Procedures: map[string]RpcDef{},
Definitions: map[string]TypeDef{},
rawProcedures: []any{},
}
}

func ServiceRpc[TParams, TResponse, TContext any](service *Service, handler func(TParams, TContext) (TResponse, RpcError)) {
if service.Procedures == nil {
service.Procedures = make(map[string]RpcDef)
}
def, defErr := ToRpcDef(handler, ArriHttpRpcOptions{})
if defErr != nil {
panic(defErr)
}
service.rawProcedures = append(service.rawProcedures, handler)
rpcName := rpcNameFromFunctionName(GetFunctionName(handler))
service.Procedures[rpcName] = *def
}
18 changes: 13 additions & 5 deletions playground/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ func main() {
}),
)
// register an RPC
arri.RpcWithOptions(&app, arri.RpcOptions{Method: arri.HttpMethodGet}, GetUser)
arri.RpcWithOptions(
&app,
arri.RpcOptions{Method: arri.HttpMethodGet},
GetUser,
)
arri.Rpc(&app, DeleteUser)
// register an RPC with a custom HTTP method and path
arri.RpcWithOptions(&app, arri.RpcOptions{
Method: arri.HttpMethodPatch,
Path: "/update-user",
}, UpdateUser)
arri.RpcWithOptions(
&app,
arri.RpcOptions{
Method: arri.HttpMethodPatch,
Path: "/update-user",
},
UpdateUser,
)
appErr := app.Run(arri.RunOptions{})
if appErr != nil {
log.Fatal(appErr)
Expand Down

0 comments on commit 22ab669

Please sign in to comment.