SSE with NATS

NATS and SSE Recently Synaida has released their preview of their HTTP Gateway. This allows for clients to use NATS who otherwise wouldn’t have the ability. This is a cool idea and I figured I’d take a stab at building a small PoC that would offer similar capabilities. Right now, Synadia’s HTTP Gateway supports KV and pub/sub. I wanted to take this a step further and leverage Jetstream under the hood....

July 2, 2024 · 6 min · 1129 words · John Hooks

NATS Micro Handlers

Micro Handlers The Synadia team has a nice package for creating micro services. In the Go client, this is the micro package. The beautiful thing about this setup is it follows the same pattern as the std library HTTP handlers. The micro package has an interface defined as Handler(micro.Request). Functions can be wrapped with micro.HandlerFunc() just like you can wrap an HTTP handler with http.HandlerFunc(). This allows for the same midleware layouts as you normally have in HTTP servers....

March 18, 2024 · 4 min · 703 words · John Hooks

Rego & Go

Rego is a declarative policy language (also a logic language) that is used in OPA. It’s a general purpose language that works in many scenarios that aren’t necessarily just for policies. One pattern I’ve recently come to like is using Rego inside of my Go project if the business logic becomes more complicated while leveraging the power of Go’s HTTP libraries. We can either embed our Rego or dynamically load it in if we decide we need to easily make changes....

January 31, 2024 · 9 min · 1738 words · John Hooks

NATS & Graphql

I’ve been using gqlgen at work for a few services and while I don’t normally like code generators, it does a decent job of staying out of the way. One thing I had hoped for was the ability to use the resolvers it generates with NATS instead of needing HTTP. I found an issue referencing this, and the gqlgen team mentioned they didn’t want to specifically support NATS because the resolvers were agnostic....

October 6, 2023 · 5 min · 930 words · John Hooks

Custom JSON Marshaling in Go

Go’s standard library has a convenient way of handling JSON verification and default values through the Marshaler and Unmarshaler interfaces. This means we don’t necessarily need separate methods to handle this data verification/manipulation. Unmarshaler Let’s pretend our system can’t allow users that are under the age 13 and we need a default timezone. var ( ErrTooYoung = fmt.Errorf("too young") ErrTZNotFound = fmt.Errorf("invalid timezone, must be one of %v", timezones) ) type TimeZone string var timezones = [....

February 19, 2023 · 4 min · 717 words · John Hooks

Custom Http Handlers Part 2

In the last post I covered a way to pass data to http handlers without using context.WithValue(). I saw another interesting way to do a type of dependency injection in a package from Jeremy called Mixer. If you own the server implementation and don’t want to import a 3rd party package you can do something similar. This doesn’t rely on generics so it is backwards compatible with older versions of Go....

October 8, 2022 · 6 min · 1108 words · John Hooks

Custom Http Handlers and context.WithValue()

One thing I try to avoid when I can is using the context.WithValue(). It’s a black box map of map[interface{}]interface{}. This is obviously flexible because anything can be stored here but there are pitfalls. The documentation states you need to create your own types for keys: The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using context....

September 28, 2022 · 6 min · 1119 words · John Hooks