http

package module
v4.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: Apache-2.0 Imports: 23 Imported by: 2

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"

	"go.unistack.org/micro/v4/server"
	httpServer "go.unistack.org/micro-server-http/v4"
)

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"

	"go.unistack.org/micro/v4"
	"go.unistack.org/micro/v4/server"
	httpServer "go.unistack.org/micro-server-http/v4"
)

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 interface{}, w http.ResponseWriter, r *http.Request, err error, status int) {
		w.WriteHeader(status)
		if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
			logger.DefaultLogger.Error(ctx, fmt.Sprintf("write failed: %v", cerr))
		}
	}
	DefaultContentType = "application/json"
)

Functions

func ErrorHandler

func ErrorHandler(fn errorHandler) options.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 HTTPServer added in v4.0.5

func HTTPServer(hs *http.Server) options.Option

HTTPServer provide ability to pass *http.Server

func HandlerEndpoints

func HandlerEndpoints(md []EndpointMetadata) options.Option

func Middleware

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

Middleware passes http middlewares

func PathHandler

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

PathHandler specifies http handler for path regexp

func RegisterRPCHandler

func RegisterRPCHandler(b bool) options.Option

RegisterRPCHandler registers compatibility endpoints with /ServiceName.ServiceEndpoint method POST

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

func SetRspHeader

func SetRspHeader(ctx context.Context, h http.Header)

SetRspHeader add response headers

Types

type EndpointMetadata

type EndpointMetadata struct {
	Name   string
	Path   string
	Method string
	Body   string
	Stream bool
}

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

type Server

type Server struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ...options.Option) *Server

func (*Server) Deregister added in v4.0.3

func (h *Server) Deregister() error

func (*Server) HTTPHandlerFunc added in v4.0.4

func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error)

func (*Server) Handle added in v4.0.3

func (h *Server) Handle(handler interface{}, opts ...options.Option) error

func (*Server) Init added in v4.0.3

func (h *Server) Init(opts ...options.Option) error

func (*Server) Name added in v4.0.3

func (h *Server) Name() string

func (*Server) Options added in v4.0.3

func (h *Server) Options() server.Options

func (*Server) Register added in v4.0.3

func (h *Server) Register() error

func (*Server) ServeHTTP added in v4.0.4

func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) Start added in v4.0.3

func (h *Server) Start() error

func (*Server) Stop added in v4.0.3

func (h *Server) Stop() error

func (*Server) String added in v4.0.3

func (h *Server) String() string

Directories

Path Synopsis
spa

Jump to

Keyboard shortcuts

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