mttp

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package mttp implements simple HTTP server routing. Package mttp aims to provide a simple, and effective method of creating a HTTP router, all the while keeping the configuration clear and concise.

The mttp package leans on the builder pattern to exhibit a declarative way of configuring your HTTP server and it's routes.

To get started, you can simply define a simple server such as the example below

import (
	"net/http"

	"github.com/alexbathome/mttp/mttp"
)

func webHandler(rw http.ResponseWriter, r *http.Request) {
	rw.Write([]byte("Hello from WEB"))
}

var server = mttp.NewServer("myapi", "127.0.0.1", "8080").
	WithMetrics("9090").
	WithRoutes(
		mttp.NewRoute("/web").
			AcceptMethods(http.MethodGet).
			RespondedToBy(webHandler),
	)

func main() {
	s, err := server.Build()
	if err != nil {
		panic(err)
	}
	err = s.Start()
	if err != nil {
		panic(err)
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RouteBuilder

type RouteBuilder interface {
	// AcceptMethods is accepts a list of HTTP Methods that are allowed
	// to be used against this route
	//
	// by default, all HTTP Methods are allowed. Except for HTTP Connect.
	AcceptMethods(...string) RouteBuilder

	// RespondedToBy takes a http.HandlerFunc, this maps the route with a
	// given handlerFunc to run against that route
	RespondedToBy(http.HandlerFunc) RouteBuilder

	// Build requires the mttp's http.ServerMux to be passed in.
	// it builds the route, and adds it to the server's multiplexer.
	Build(mux *http.ServeMux)
	// contains filtered or unexported methods
}

func NewRoute

func NewRoute(routePath string) RouteBuilder

type RouteValidationError

type RouteValidationError error

type Server

type Server interface {
	// Start starts the mttp server, and begins listening to HTTP traffic
	// on the server's provided address
	Start() error
}

Server is the mttp.Server interface

type ServerBuilder

type ServerBuilder interface {
	// WithRoutes is the builder pattern implementation to provide the mttp server
	// with routes to serve
	WithRoutes(...RouteBuilder) ServerBuilder
	// WithMetrics enables the MTTP library's middle-ware.
	//
	// By default, this middleware captures HTTP status codes from your
	// handlers, and publishes them with a prometheus server that runs
	// on the given port.
	WithMetrics(metricsPort string) ServerBuilder

	// Build constructs the mttp server, it returns an implementation of the mttp.Server
	// interface, and an error.
	//
	// Build internally wraps the route's handler funcs with mttp's middleware if mttp.WithMetrics()
	// has been called on the ServerBuilder
	Build() (Server, error)
}

func NewServer

func NewServer(name, address, port string) ServerBuilder

NewServer is the entry point to the mttp library, this creates a ServerBuilder interface which can be used to configure the mttp server.

type ServerValidationError

type ServerValidationError error

Directories

Path Synopsis
internal
middleware
middleware is mttp's internal middleware package it is responsible for mttp's http handler implementation as well as it's own http.ReponseWriter implementation
middleware is mttp's internal middleware package it is responsible for mttp's http handler implementation as well as it's own http.ReponseWriter implementation

Jump to

Keyboard shortcuts

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