{"modulePath":"github.com/erda-project/erda","version":"v1.0.1","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver httpserver统一封装，使API注册更容易\n\n## Constants\n\n```go\nconst (\n\tContentTypeJSON          = \"application/json\"\n\tResponseWriter           = \"responseWriter\"\n\tBase64EncodedRequestBody = \"base64-encoded-request-body\"\n\tTraceID                  = \"dice-trace-id\"\n)\n```\n\n## Functions\n\n```go\nfunc WriteData(w http.ResponseWriter, v interface{})\n```\n\n```go\nfunc WriteErr(w http.ResponseWriter, code, errMsg string)\n```\n\n```go\nfunc WriteJSON(w http.ResponseWriter, v interface{})\n```\n\n```go\nfunc WriteYAML(w http.ResponseWriter, v string)\n```\nWriteYAML 响应yaml结构\n\n## Types\n\n```go\ntype Endpoint struct {\n\tPath           string\n\tMethod         string\n\tHandler        func(context.Context, *http.Request, map[string]string) (Responser, error)\n\tWriterHandler  func(context.Context, http.ResponseWriter, *http.Request, map[string]string) error\n\tReverseHandler func(context.Context, *http.Request, map[string]string) error\n}\n```\nEndpoint contains URL path and corresponding handler\n\n```go\ntype HTTPResponse struct {\n\tError   ierror.IAPIError\n\tStatus  int\n\tContent interface{}\n}\n```\nHTTPResponse is a struct contains status code and content body\n\n```go\nfunc (r HTTPResponse) GetContent() interface{}\n```\nGetContent returns http content body\n\n```go\nfunc (r HTTPResponse) GetLocaledResp(locale *i18n.LocaleResource) HTTPResponse\n```\nGetLocaledResp\n\n```go\nfunc (r HTTPResponse) GetStatus() int\n```\nGetStatus returns http status code.\n\n```go\ntype Resp struct {\n\tSuccess bool                     `json:\"success\"`\n\tData    interface{}              `json:\"data,omitempty\"`\n\tErr     apistructs.ErrorResponse `json:\"err,omitempty\"`\n\tUserIDs []string                 `json:\"userIDs,omitempty\"`\n}\n```\nResp dice平台http body返回结构\n\n```go\ntype Responser interface {\n\tGetLocaledResp(locale *i18n.LocaleResource) HTTPResponse\n\tGetStatus() int\n\tGetContent() interface{}\n}\n```\nResponser is an interface for http response\n\n```go\nfunc ErrResp(statusCode int, code, errMsg string) (Responser, error)\n```\nErrResp 采用httpserver框架时异常返回结果封装\n\n```go\nfunc OkResp(data interface{}, userIDs ...[]string) (Responser, error)\n```\nOkResp 采用httpserver框架时正常返回结果封装\n\n在 \\`userIDs\\` 中设置需要由 openapi 注入的用户信息的 ID 列表\n\n```go\ntype Server struct {\n\t// contains filtered or unexported fields\n}\n```\nServer provides ability to run a http server and handler registered URL paths.\n\n```go\nfunc New(addr string) *Server\n```\nNew create an http server.\n\n```go\nfunc (s *Server) ListenAndServe() error\n```\nListenAndServe boot the server to lisen and accept requests.\n\n```go\nfunc (s *Server) RegisterEndpoint(endpoints []Endpoint)\n```\nRegisterEndpoint match URL path to corresponding handler\n\n```go\nfunc (s *Server) Router() *mux.Router\n```\nRouter 返回 server's router.\n\n```go\nfunc (s *Server) WithLocaleLoader(loader *i18n.LocaleResourceLoader)\n```\nWithLocaleLoader\n\n","path":"github.com/erda-project/erda/pkg/httpserver","name":"httpserver","synopsis":"Package httpserver httpserver统一封装，使API注册更容易","isRedistributable":true}
