Hi there 馃憢

I do stuff with Go, Kubernetes, NATS, and other cloud native tools.

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鈥檛 have the ability. This is a cool idea and I figured I鈥檇 take a stab at building a small PoC that would offer similar capabilities. Right now, Synadia鈥檚 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鈥檚 a general purpose language that works in many scenarios that aren鈥檛 necessarily just for policies. One pattern I鈥檝e 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鈥檚 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鈥檝e been using gqlgen at work for a few services and while I don鈥檛 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鈥檛 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鈥檚 standard library has a convenient way of handling JSON verification and default values through the Marshaler and Unmarshaler interfaces. This means we don鈥檛 necessarily need separate methods to handle this data verification/manipulation. Unmarshaler Let鈥檚 pretend our system can鈥檛 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鈥檛 want to import a 3rd party package you can do something similar. This doesn鈥檛 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鈥檚 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