serv

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2020 License: BSD-3-Clause Imports: 4 Imported by: 0

README

go-http

Coverage Status Release Go Report Card Build Status

How to use it

Web server

Set route version for request: Accept: application/vnd.v1+json

Set sign for request: Signature: keyId="<sign id>",algorithm="hmac-sha256",signature="<sign hash>"

package main

import (
	"net/http"
	"time"

	"github.com/deweppro/go-http/web"
	"github.com/deweppro/go-logger"
)

func main() {

	srv := web.NewCustomServer(web.ConfigItem{Addr: "localhost:8080"}, logger.Default())
	if err := srv.Up(); err != nil {
		panic(err)
	}

	srv.Router().AddRoutes(
		web.Handler{
			Method: []string{http.MethodGet},
			Path:   "/",
			Call: web.VerCaller{
				// version 1 for route /
				web.DefaultVersion: func(ctx *web.Context) error {
					return ctx.Write(200, []byte("hello"), web.Headers{"x-trace-id": "999-999-999"})
				},
			},
		},
	)

	<-time.After(time.Minute)

	if err := srv.Down(); err != nil {
		panic(err)
	}
}

Debug server (pprof)

package main

import (
	"time"

	"github.com/deweppro/go-http/web"
	"github.com/deweppro/go-logger"
)

func main() {

	debug := web.NewCustomDebug(web.ConfigItem{Addr: "localhost:8090"}, logger.Default())
	if err := debug.Up(); err != nil {
		panic(err)
	}

	<-time.After(time.Minute)

	if err := debug.Down(); err != nil {
		panic(err)
	}
}

TCP server (epoll)

package main

import (
	"io"
	"time"

	"github.com/deweppro/go-http/epoll"
	"github.com/deweppro/go-logger"
)

func main() {

	srv := epoll.NewCustomServer(epoll.ConfigItem{Addr: "localhost:8080"}, logger.Default())
	srv.Handler(func(bytes []byte, writer io.Writer) error {
		_, err := writer.Write(bytes)
		return err
	})
	if err := srv.Up(); err != nil {
		panic(err)
	}

	<-time.After(time.Minute)

	if err := srv.Down(); err != nil {
		panic(err)
	}
}

Web server + websocket

package main

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

	"github.com/deweppro/go-http/web"
	"github.com/deweppro/go-http/ws"
	"github.com/deweppro/go-logger"
)

func main() {

	wsock := ws.NewServer(10, 128, logger.Default())

	srv := web.NewCustomServer(web.ConfigItem{Addr: "localhost:8080"}, logger.Default())
	if err := srv.Up(); err != nil {
		panic(err)
	}

	srv.Router().AddRoutes(
		web.Handler{
			Method: []string{http.MethodGet},
			Path:   "/",
			Call: web.VerCaller{
				web.DefaultVersion: func(ctx *web.Context) error {
					return wsock.Handler(ctx.Writer, ctx.Reader,
						func(out chan<- *ws.Message, in <-chan *ws.Message, ctx context.Context, cncl context.CancelFunc) {
							i := 0
							for {
								select {
								case <-ctx.Done():
									return
								case msg := <-in:
									out <- msg
									if i == 3 {
										cncl()
									}
									i++
								}
							}
						},
					)
				},
			},
		})

	<-time.After(time.Minute)

	if err := srv.Down(); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServAlreadyRunning = errors.New("server is already running")
	ErrEpollEmptyEvents   = errors.New("epoll events is empty")
)

Functions

func GetFD

func GetFD(c net.Conn) int

GetFD ...

func RandomPort

func RandomPort(host string) (string, error)

RandomPort ...

Types

This section is empty.

Directories

Path Synopsis
example
debug command
epoll command
web command
websocket command
web

Jump to

Keyboard shortcuts

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