http

package module
v3.10.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 25 Imported by: 10

README

HTTP Server

The HTTP Server is a go-micro.Server. It's a partial implementation which strips out codecs, transports, etc but enables you to create a HTTP Server that could potentially be used for REST based API services.

Usage

import (
	"net/http"

	"github.com/unistack-org/micro/v3/server"
	httpServer "github.com/unistack-org/micro-server-http"
)

func main() {
	srv := httpServer.NewServer(
		server.Name("helloworld"),
	)

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`hello world`))
	})

	hd := srv.NewHandler(mux)

	srv.Handle(hd)
	srv.Start()
	srv.Register()
}

Or as part of a service

import (
	"net/http"

	"github.com/unistack-org/micro/v3"
	"github.com/unistack-org/micro/v3/server"
	httpServer "github.com/unistack-org/micro-server-http"
)

func main() {
	srv := httpServer.NewServer(
		server.Name("helloworld"),
	)

	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`hello world`))
	})

	hd := srv.NewHandler(mux)

	srv.Handle(hd)

	service := micro.NewService(
		micro.Server(srv),
	)
	service.Init()
	service.Run()
}

Documentation

Overview

Package http implements a go-micro.Server

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) {
		w.WriteHeader(status)
		if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
			logger.DefaultLogger.Errorf(ctx, "write failed: %v", cerr)
		}
	}
	DefaultContentType = "application/json"
)

Functions

func ErrorHandler

func ErrorHandler(fn func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)) server.Option

ErrorHandler specifies handler for errors

func FillRequest

func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption) error

func GetError

func GetError(err interface{}) interface{}

GetError return underline error

func GetRspCode

func GetRspCode(ctx context.Context) int

GetRspCode used internally by generated http server handler

func Middleware

func Middleware(mw ...func(http.Handler) http.Handler) server.Option

Middleware passes http middlewares

func NewServer

func NewServer(opts ...server.Option) *httpServer

func PathHandler

func PathHandler(method, path string, handler http.HandlerFunc) server.Option

PathHandler specifies http handler for path regexp

func RegisterRPCHandler

func RegisterRPCHandler(b bool) server.Option

RegisterRPCHandler registers compatibility endpoints with /ServiceName.ServiceEndpoint method POST

func Server

func Server(hs *http.Server) server.Option

Server provide ability to pass *http.Server

func SetError

func SetError(err interface{}) error

SetError pass error to caller

func SetRspCode

func SetRspCode(ctx context.Context, code int)

SetRspCode saves response code in context, must be used by handler to specify http code

Types

type Error

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

Error struct holds error

func (*Error) Error

func (err *Error) Error() string

Error func for error interface

type FillRequestOption

type FillRequestOption func(*handlerOptions)
func Cookie(cookies ...string) FillRequestOption
func Header(headers ...string) FillRequestOption

Jump to

Keyboard shortcuts

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