Skip to content

go-flexible/example-order-system

Repository files navigation

marp theme _class title
true
gaia
invert
go flexible

bg 50% A collection of packages for building Go services.


Goals

  • Provide flexible primitives to build upon.
  • Simplify process lifecycle and graceful shutdown.
  • No third party dependencies allowed within flex itself.
  • All published packages must be tested.

Primitive: Runner

A Runner represents anything which can "run" itself. For example, an HTTP server.

// Runner represents the behaviour for running a service worker.
type Runner interface {
        // Run should run start processing the worker and be a blocking operation.
        Run(context.Context) error
}

Primitive: Halter

A Halter represents anything which can "halt" itself. For example a Kafka broker.

// Halter represents the behaviour for stopping a service worker.
type Halter interface {
        // Halt should tell the worker to stop doing work.
        Halt(context.Context) error
}

Primitive: Worker

A Worker represents anything that can both "run" and "halt" itself. For example, a cron job.

// Worker represents the behaviour for a service worker.
type Worker interface {
        Runner
        Halter
}

In Practice

bg contain


Optics

  • Consider io.Reader and io.Writer.
  • Both single method interfaces.
  • Both result in incredibly varried uses.

The goal of flex is to provide a similar experience, for building k8s native applications.


The flexhttp plugin

type Server struct{ *http.Server }

func NewHTTPServer(s *http.Server) *Server {
        return &Server{Server: s}
}
func (s *Server) Run(_ context.Context) error {
        log.Printf("serving on: http://localhost%s\n", s.Addr)
        return s.ListenAndServe()
}
func (s *Server) Halt(ctx context.Context) error {
        return s.Shutdown(ctx)
}

The flexmetrics plugin

flexmetrics exposes prometheus and pprof metrics on an http server. It's Worker is implemented by the concrete Server type.

type Server struct {
        Path   string
        Server *http.Server
}

Simple yet complete

// Run will start the metrics server.
func (s *Server) Run(_ context.Context) error {
        /* abbreviated... */
        mux := http.NewServeMux()
        mux.Handle(s.Path, promhttp.Handler())
        mux.HandleFunc("/debug/pprof/", pprof.Index)
        mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
        mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
        mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
        mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
        /* abbreviated... */
}

More than meets the eye

  • Environment variables for configuration
  • Configurable port-bindings
  • Configurable http server
  • Sane defaults if you choose to use them

Live demo

Goals

  • Build a non-trivial order system (think e-commerce).
  • Demonstrate how flex eliminates a lot of complicated setup.
  • Provide working example usecases for future reference.

About

An example ordering system using flex features

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages