Call another app
Let one app call Go functions of another, on behalf of the viewer, with the runner and both apps checking each call. The example is the contacts tab of the people example, which asks the contacts app for a user's phones and emails.
In the app that serves the call
-
Put the function in the app's
rpc/folder, packagerpc, with the shapefunc Name(ctx context.Context, in In) (Out, error), whereInandOutare structs ofrpc/, and an//ssr:accessline in its doc comment:// ContactsIn names a person by login. type ContactsIn struct { Login string }// ContactsOut holds a person's phone numbers and email addresses. type ContactsOut struct { Phones []string Emails []string }// Contacts returns the contacts of a person to that person and to hr. The people app checks the // same rule in its Guard; this app checks it again, since it holds the data. // //ssr:access caller=people role=staff func Contacts(ctx context.Context, in ContactsIn) (ContactsOut, error) { if !allowed(auth.Viewer(ctx), in.Login) { return ContactsOut{}, frameworkrpc.Error(frameworkrpc.PermissionDenied, "only the person and hr may read these contacts") } phones, emails, err := deps.Contacts(ctx, in.Login) if errors.Is(err, deps.ErrNoPerson) { return ContactsOut{}, frameworkrpc.Error(frameworkrpc.NotFound, "no such person") } if err != nil { return ContactsOut{}, err } return ContactsOut{Phones: phones, Emails: emails}, nil }caller=peoplenames the apps that may call the function, by theapp:line of their permission lists.role=staffis the role a viewer needs for a call made on their behalf.- Return
rpc.Errorwith a code, such asrpc.PermissionDeniedorrpc.NotFound, to tell the caller why a call failed. Any other error reaches the caller asinternal error. - The folder's own package is
rpc, so the framework'srpcpackage is imported under another name.
-
Serve the functions from
main.go:// Contacts serves the phone numbers and email addresses of people to the people app. package main import ( "aicoded.dev/framework/app" "contacts/deps" "contacts/rpc" ) func main() { app.Main(app.Options{RPC: rpc.Server(), OnStart: deps.Start}) } -
Run
aicoded generate. It writes the server,rpc/server_gen.go, the snapshot of the functions,.aicoded/rpc.json, and theservicessection of the permission list. Never edit them.
In the app that calls
-
In the calling app's folder, run
aicoded rpc add contacts. It finds the app named contacts in the folder that holds this app's folder, or below it, and copies its snapshot to.aicoded/services/contacts.json. Then it generates this app, which writes the client,services/contacts/client_gen.go, and printsaicoded rpc add: call the functions of contacts through the package in services/contacts. Run it again to pick up new functions. -
Call the client's function of the same name with the context of the request, so that the call goes on behalf of the viewer, and turn the codes into answers of the page:
// Data asks the contacts app for the user's phones and emails, on behalf of the viewer. func (p *DP) Data(ctx context.Context, r *web.Request, _ web.ResponseWriter, data *RouteData) error { out, err := contacts.Contacts(ctx, contacts.ContactsIn{Login: r.URLParam("login")}) switch rpc.CodeOf(err) { case 0: case rpc.PermissionDenied: return web.Forbidden() case rpc.NotFound: return web.NotFound() default: return err } data.Phones, data.Emails = out.Phones, out.Emails return nil } -
aicoded generateadds the function to theservicessection of the calling app's permission list, as"contacts": ["Contacts"]undercalls. The runner lets through only the calls that both permission lists name.
Check it
aicoded checkin the folder that holds both apps generates, builds, vets, lints and tests them, and checks the snapshot people holds against contacts' functions.aicoded devin that folder starts contacts first, since people calls it, and printsaicoded dev: contacts (calls only).- Open
http://people.localhost:8080/users/alice/contactsas alice, a persona with thestaffrole. The dev UI's Traces page shows the request as one trace across both apps, with people'srpc.callspan.