httpserver

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

README

httpserver

server

TODO

errorresp

统一的错误定义,避免代码中出现大量的 plain string

定义错误

注意的是定义抽象的大类错误(比如:创建应用失败、部署失败):

internal/demo/services/apierrors/errors.go

var (
	ErrCreateDemo = errorresp.New(errorresp.WithInitMessage("ErrCreateDemo", "创建失败例子"))
)
返回错误

services 中返回 error:

internal/demo/services/foobar/foobar.go

func (f *Foobar) CreateDemo() error {
	return apierrors.ErrCreateDemo.InvalidState("not ready for creation")
}

endpoints 中将 services 返回的错误封装成 ErrResp:

internal/demo/endpoints/info.go

err := e.foobar.CreateDemo()
if err != nil {
    return errorresp.ErrResp(err)
}

endpoints 中直接返回错误:

internal/demo/endpoints/info.go

return apierrors.ErrCreateDemo.InvalidParameter("demo is error").ToResp(), nil

Documentation

Overview

Package httpserver httpserver统一封装,使API注册更容易

Index

Constants

View Source
const (
	ContentTypeJSON          = "application/json"
	ResponseWriter           = "responseWriter"
	Base64EncodedRequestBody = "base64-encoded-request-body"
	TraceID                  = "dice-trace-id"
)

Variables

This section is empty.

Functions

func WriteData

func WriteData(w http.ResponseWriter, v interface{})

func WriteErr

func WriteErr(w http.ResponseWriter, code, errMsg string)

func WriteJSON

func WriteJSON(w http.ResponseWriter, v interface{})

func WriteYAML

func WriteYAML(w http.ResponseWriter, v string)

WriteYAML 响应yaml结构

Types

type Endpoint

type Endpoint struct {
	Path           string
	Method         string
	Handler        func(context.Context, *http.Request, map[string]string) (Responser, error)
	WriterHandler  func(context.Context, http.ResponseWriter, *http.Request, map[string]string) error
	ReverseHandler func(context.Context, *http.Request, map[string]string) error
}

Endpoint contains URL path and corresponding handler

type HTTPResponse

type HTTPResponse struct {
	Error   ierror.IAPIError
	Status  int
	Content interface{}
}

HTTPResponse is a struct contains status code and content body

func (HTTPResponse) GetContent

func (r HTTPResponse) GetContent() interface{}

GetContent returns http content body

func (HTTPResponse) GetLocaledResp

func (r HTTPResponse) GetLocaledResp(locale *i18n.LocaleResource) HTTPResponse

GetLocaledResp

func (HTTPResponse) GetStatus

func (r HTTPResponse) GetStatus() int

GetStatus returns http status code.

type Resp

type Resp struct {
	Success bool                     `json:"success"`
	Data    interface{}              `json:"data,omitempty"`
	Err     apistructs.ErrorResponse `json:"err,omitempty"`
	UserIDs []string                 `json:"userIDs,omitempty"`
}

Resp dice平台http body返回结构

type Responser

type Responser interface {
	GetLocaledResp(locale *i18n.LocaleResource) HTTPResponse
	GetStatus() int
	GetContent() interface{}
}

Responser is an interface for http response

func ErrResp

func ErrResp(statusCode int, code, errMsg string) (Responser, error)

ErrResp 采用httpserver框架时异常返回结果封装

func OkResp

func OkResp(data interface{}, userIDs ...[]string) (Responser, error)

OkResp 采用httpserver框架时正常返回结果封装

在 `userIDs` 中设置需要由 openapi 注入的用户信息的 ID 列表

type Server

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

Server provides ability to run a http server and handler registered URL paths.

func New

func New(addr string) *Server

New create an http server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe boot the server to lisen and accept requests.

func (*Server) RegisterEndpoint

func (s *Server) RegisterEndpoint(endpoints []Endpoint)

RegisterEndpoint match URL path to corresponding handler

func (*Server) Router

func (s *Server) Router() *mux.Router

Router 返回 server's router.

func (*Server) WithLocaleLoader

func (s *Server) WithLocaleLoader(loader *i18n.LocaleResourceLoader)

WithLocaleLoader

Directories

Path Synopsis
Package errorresp 封装了错误定义
Package errorresp 封装了错误定义

Jump to

Keyboard shortcuts

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