http

package module
v3.11.26 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 29 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 errorHandler) 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 HTTPServer added in v3.11.0

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

HTTPServer provide ability to pass *http.Server

func HandlerEndpoints added in v3.10.11

func HandlerEndpoints(md []EndpointMetadata) server.HandlerOption

func Middleware

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

Middleware passes http middlewares

func PathHandler

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

PathHandler specifies http handler for path regexp

func RegisterCORSHandler added in v3.11.16

func RegisterCORSHandler(b bool) server.HandlerOption

RegisterCORSHandler registers cors endpoints with /ServiceName.ServiceEndpoint method POPTIONSOST

func RegisterRPCHandler

func RegisterRPCHandler(b bool) server.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 added in v3.10.7

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

SetRspHeader add response headers

Types

type EndpointMetadata added in v3.10.11

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 ...server.Option) *Server

func (*Server) Deregister added in v3.11.0

func (h *Server) Deregister() error

func (*Server) HTTPHandlerFunc added in v3.11.0

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

func (*Server) Handle added in v3.11.0

func (h *Server) Handle(handler server.Handler) error

func (*Server) Init added in v3.11.0

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

func (*Server) Name added in v3.11.0

func (h *Server) Name() string

func (*Server) NewHandler added in v3.11.0

func (h *Server) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler

func (*Server) NewSubscriber added in v3.11.0

func (h *Server) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber

func (*Server) Options added in v3.11.0

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

func (*Server) Register added in v3.11.0

func (h *Server) Register() error

func (*Server) ServeHTTP added in v3.11.0

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

func (*Server) Start added in v3.11.0

func (h *Server) Start() error

func (*Server) Stop added in v3.11.0

func (h *Server) Stop() error

func (*Server) String added in v3.11.0

func (h *Server) String() string

func (*Server) Subscribe added in v3.11.0

func (h *Server) Subscribe(sb server.Subscriber) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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