httpserver

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: MIT Imports: 8 Imported by: 1

README

httpserver

Another HTTP Server use handler with context & Response Render.

Quick Start

package main

import (
	"context"
	"log"
	"net/http"
	"time"

	"github.com/x-mod/httpserver"
	"github.com/x-mod/routine"
)

func main() {
	srv := httpserver.NewServer(
		httpserver.Address(":8080"),
	)
	srv.Route(
		httpserver.Pattern("/hello"),
		httpserver.Handler(Hello),
	)

	err := routine.Main(
		routine.ExecutorFunc(srv.Serve),
		routine.Context(context.WithValue(context.TODO(), "x", "y")),
		routine.Interrupts(routine.DefaultCancelInterruptors...),
		routine.Cleanup(
			routine.ExecutorFunc(func(ctx context.Context) error {
				//graceful shutdown MaxTime 15s
				tmctx, cancel := context.WithTimeout(ctx, 3*time.Second)
				defer cancel()
				return srv.Shutdown(tmctx)
			})),
	)
	if err != nil {
		log.Println("failed: ", err)
	}
}

func Hello(ctx context.Context, wr http.ResponseWriter, req *http.Request) {
	log.Println("hello handler ... ok", ctx.Value("x"))
	wr.WriteHeader(http.StatusOK)
	_, _ = wr.Write([]byte("I'm OK"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextHandler

type ContextHandler func(context.Context, http.ResponseWriter, *http.Request)

type RouteCfg

type RouteCfg struct {
	// contains filtered or unexported fields
}

type RouteOpt

type RouteOpt func(cf *RouteCfg)

func Handler

func Handler(h ContextHandler) RouteOpt
func Header(headers ...string) RouteOpt

func Host

func Host(host string) RouteOpt

func Method

func Method(methods ...string) RouteOpt

func Pattern

func Pattern(pattern string) RouteOpt

func Prefix

func Prefix(prefix string) RouteOpt

func Query

func Query(queries ...string) RouteOpt

func Schema

func Schema(schemas ...string) RouteOpt

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ...ServerOpt) *Server

func (*Server) Route

func (srv *Server) Route(opts ...RouteOpt)

func (*Server) Serve

func (srv *Server) Serve(ctx context.Context) error

func (*Server) Shutdown added in v0.4.0

func (srv *Server) Shutdown(ctx context.Context) error

type ServerOpt

type ServerOpt func(*Server)

func Address added in v0.3.1

func Address(addr string) ServerOpt

func TLSConfig added in v0.3.0

func TLSConfig(cf *tls.Config) ServerOpt

Directories

Path Synopsis
example
simple command

Jump to

Keyboard shortcuts

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