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

Piggy Bank

Overview Piggy Bank was born from a desire to easily store accessible secrets anywhere and not needing to expose HTTP ports for things like Vault. This project is no where near as robust as Vault (hence the name Piggy Bank) but it is desirable to be able to store secrets in the NATS KV store and have secrets be accessible to services using the bus. Authentication Authentication and authorization is done by using normal NATS auth....

August 29, 2022 · 3 min · 431 words · John Hooks