-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http handlers for greetings example
- Loading branch information
Showing
42 changed files
with
1,362 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
# Greetings | ||
|
||
This is an example of implementing: | ||
The greetings extension is a pointless extension that gives greetings to people. | ||
|
||
The extension was developed to not speak with strangers and only recognises a few common Finnish names. | ||
|
||
You can run an action (stateless, synchronous) or create an object (stateful, asynchronous) to get a greeting. | ||
|
||
The example includes implementing: | ||
|
||
1. A [controller](./controller.go) (with a `Reconciler` and `Validator`) | ||
2. An [actor](./actor.go) | ||
3. A [portal](./portal.go). | ||
|
||
> [!WARNING] | ||
> The greetings extension currently does not have a portal endpoint (i.e. no http/nats service to return HTML). So there is nothing to browse in the UI. | ||
## Running the example | ||
|
||
1. Start a Horizon server (e.g. `go run ./cmd/horizon/main.go`) | ||
2. Generate NATS user credentials (TODO: document how) | ||
3. Run the greetings extention: `go run ./examples/cmd/main.go` | ||
|
||
Once you start the server and create an account you should see the "Greetings" portal on the left: | ||
|
||
![greetings-screenshot](./greetings-example-screenshot.png) | ||
|
||
From there you can click around and greet some people. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/verifa/horizon/examples/greetings" | ||
"github.com/verifa/horizon/pkg/hz" | ||
) | ||
|
||
func main() { | ||
// TODO: get NATS credentials and server information. | ||
if err := run(); err != nil { | ||
slog.Error("running", "error", err) | ||
os.Exit(1) | ||
|
||
} | ||
} | ||
|
||
func run() error { | ||
conn, err := nats.Connect( | ||
nats.DefaultURL, | ||
nats.UserCredentials("nats.creds"), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("connect: %w", err) | ||
} | ||
ctx := context.Background() | ||
|
||
ctx, stop := signal.NotifyContext(ctx, os.Interrupt) | ||
defer stop() | ||
|
||
actor, err := hz.StartActor( | ||
ctx, | ||
conn, | ||
hz.WithActorActioner( | ||
greetings.GreetingsHelloAction{}, | ||
), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("start actor: %w", err) | ||
} | ||
defer func() { | ||
_ = actor.Stop() | ||
}() | ||
|
||
validator := greetings.GreetingValidator{} | ||
|
||
reconciler := greetings.GreetingReconciler{ | ||
GreetingClient: hz.ObjectClient[greetings.Greeting]{ | ||
Client: hz.NewClient( | ||
conn, | ||
hz.WithClientInternal(true), | ||
hz.WithClientManager("ctlr-greetings"), | ||
), | ||
}, | ||
} | ||
ctlr, err := hz.StartController( | ||
ctx, | ||
conn, | ||
hz.WithControllerFor(greetings.Greeting{}), | ||
hz.WithControllerReconciler(&reconciler), | ||
hz.WithControllerValidator(&validator), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("start controller: %w", err) | ||
} | ||
defer func() { | ||
_ = ctlr.Stop() | ||
}() | ||
|
||
portalHandler := greetings.PortalHandler{ | ||
Conn: conn, | ||
} | ||
router := portalHandler.Router() | ||
portal, err := hz.StartPortal(ctx, conn, greetings.Portal, router) | ||
if err != nil { | ||
return fmt.Errorf("start portal: %w", err) | ||
} | ||
defer func() { | ||
_ = portal.Stop() | ||
}() | ||
|
||
<-ctx.Done() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.