registerable

package module
v0.0.0-...-e0b09f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2022 License: MIT Imports: 2 Imported by: 0

README

go-regsiterable

Go Reference

Just another way to help compartmentalize your go code.

Why

Started playing with this idea of organizing my code because I got tired of the distance between my http handler functions and their addition to the multiplexer (along with various other settings such as authorization). Just wanted everything to be bundled to make it easy to spin up new routes. Can be made to work with whatever framework you may already be using could even be used for things other than setting up http handlers.

If what you are registering requires a certain ordering, checkout OrderedRegisterable.

How

The code for this is fairly short. All it does is use reflection to look for any methods will return a Registration on you struct. It runs those methods, then pipes it to your Register function.

Example


// our 'Registerable'
type Router struct {
	*http.ServeMux

	// any other dependencies such as a database connection
}

// does all the boilerplate work
func (self Router) Register(r HandlerRegistration) {
	if r.HandlerFunc == nil {
		panic("no handler set")
	}

	if r.HandlerFunc != nil {
		self.ServeMux.HandleFunc(r.Path, r.HandlerFunc)
	}
}

func NewRouter() *Router {
        // instantiate
	r := &Router{http.NewServeMux()}

        // register methods
	RegisterMethods[HandlerRegistration](r)

	return r
}


// our 'Registration'
type HandlerRegistration struct {
	// path the endpoint is registered at
	Path string

	// your http handler func
	HandlerFunc http.HandlerFunc

	// any other settings you need setup inside your Register
}

func (self Router) ServeFoo() Registration {
	return HandlerRegistration{
		Path: "/foo",
		HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "fighters")
		},
	}
}

func (self Router) ServeBar() Registration {
	return HandlerRegistration{
		Path: "/bar",
		HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "stool")
		},
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterMethods

func RegisterMethods[H Registration, T Registerable[H]](registerable T)

RegisterMethods Register methods on Registerable T to be ran. Uses reflection to find methods that return Registration H. That Registration is then passed to and called by Registerable T's Register method.

func RegisterOrderedMethods

func RegisterOrderedMethods[H Registration, T Registerable[H]](registerable T)

RegisterOrderedMethods Same as RegisterMethods uses an OrderedRegistration to order register calls.

Types

type OrderedRegisterFn

type OrderedRegisterFn func() (int, Registration)

OrderedRegisterFn signature of an OrderedRegisterable method.

type RegisterFn

type RegisterFn func() Registration

RegisterFn signature of a Registerable method.

type Registerable

type Registerable[H Registration] interface {
	Register(H)
}

Registerable make a method Registerable. Good place to store any dependencies for use within your Registerable methods.

type Registration

type Registration interface{}

Registration holds info to be registered and used within your code. Good place to store any information requied by the Registerable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL