wabservar

package module
v0.0.0-...-47c0f0d Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2019 License: BSD-3-Clause Imports: 14 Imported by: 0

README

WABSERVAR

Build Status coverage PRs Welcome

WABSERVAR is an enterpise not ready HTTP web server on TCP sockets created during a hackathon.

Features:

  • no tests
  • no stability
  • no guarantees
  • open source
  • go modules
  • cool name
  • yolo

Additional Thank you to the Go team and net/http package 🎉

Example
package main

import (
	"errors"
	"strconv"
	"time"

	"github.com/cristaloleg/wabservar"
)

func main() {
	index := func(req *wabservar.Request) ([]byte, int, error) {
		req.Header.Add("Location", "http://www.wabservar.enterprise.com/index.asp")
		return nil, 301, nil
	}

	ping := func(req *wabservar.Request) ([]byte, int, error) {
		return nil, 200, nil
	}

	echo := func(req *wabservar.Request) ([]byte, int, error) {
		value, ok := req.Header.Get("X-Delay-Ms")
		if ok {
			v, _ := strconv.Atoi(value)
			time.Sleep(time.Duration(v) * time.Millisecond)
		}
		return []byte(req.Body), 200, nil
	}

	notFound := func(req *wabservar.Request) ([]byte, int, error) {
		return nil, 404, errors.New("well, path, not found")
	}

	m := wabservar.NewMux(notFound)
	m.AddRoute("GET", "/", index)
	m.AddRoute("POST", "/", index)
	m.AddRoute("GET", "/ping", ping)
	m.AddRoute("POST", "/echo", echo)

	s, _ := wabservar.NewServer(":31337", m)

	s.Run()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyReadAfterClose = errors.New("http: invalid Read on closed Body")

ErrBodyReadAfterClose is returned when reading a Request or Response Body after the body has been closed. This typically happens when the body is read after an HTTP Handler calls WriteHeader or Write on its ResponseWriter.

Functions

This section is empty.

Types

type Handler

type Handler func(req *Request) (body []byte, statusCode int, err error)

Handler represents an HTTP handler

type Headers

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

Headers represents HTTP headers key and multiple values.

func (*Headers) Add

func (h *Headers) Add(name, value string)

Add a new header with a single value.

func (*Headers) Get

func (h *Headers) Get(name string) (string, bool)

Get returns a first value for a name.

func (Headers) String

func (h Headers) String() string

type Mux

type Mux struct {
	http.ServeMux
	// contains filtered or unexported fields
}

Mux is an HTTP request multiplexer.

func NewMux

func NewMux(notFound Handler) *Mux

NewMux instantiates a new Mux.

func (*Mux) AddRoute

func (m *Mux) AddRoute(method, path string, h Handler)

AddRoute will add a method-path pair to the multiplexer

func (*Mux) Serve

func (m *Mux) Serve(r *Request) Handler

Serve will process a given request.

type Request

type Request struct {
	Method string
	URL    *url.URL
	Proto  string
	Header Headers
	Body   string

	ContentLength int64
	Close         bool
	Host          string
	RequestURI    string
	// contains filtered or unexported fields
}

Request represents an HTTP request.

type Server

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

Server representa a HTTP WABSERVER.

func NewServer

func NewServer(port string, m *Mux) (*Server, error)

NewServer instantiates a new HTTP WABSERVER.

func (*Server) Close

func (s *Server) Close()

Close will ungracefuly stop a server.

func (*Server) Run

func (s *Server) Run()

Run will start a HTTP server.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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