EricServer

package module
v1.1.13 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

http请求框架

v1.0 => 单请求服务

v1.1 => 可同时注册多请求服务,满足https,http 同时构建

http请求框架

  1. 自定多路由注册解析
  2. html模板解析

获取

go get -u  gitee.com/tym_hmm/eric-http

使用

/**
静态打包处理需开启
**/
//go:embed Asset
var publicAsset embed.FS

func TestHttp(t *testing.T)  {

 server :=  EricServer.NewEricServer(&publicAsset)
 server.
    //设置模板
    SetTemplate("Asset/Template", ".html").
    //设置json响应
    SetResponseKey("code", "message", "data").
    //设置静态资源加载 js css ,会打包到二进制包中
    SetStaticDir("/static/", "/Asset/Static").
    //多个静态资源加载设置
    //SetStaticDir("/static2/", "/Asset/Static2").
    //设置资源上传及访问目录
    SetUploadDIr("/upload", "/Store").
    //设置路由
    RegisterHttpRoute(ApiRouteNew, HttpRouteNew).
    Start("0.0.0.0", 8081, 1, EricServer.MODE_CONST_DEV)
}


/**
http 路由注册
*/
func HttpRouteNew(e *Route.Engine) {
    e.Get("/", RequestMiddleware(), func(c *Route.Context) {
      //多模板解析
      //c.TemplateMultiple()
      //单模板解析
      //c.Template()
      _, _ = c.Writer.Write([]byte("这是是ttp路由"))
    })
}

/**
api 路由注册
*/
func ApiRouteNew(e *Route.Engine) {

    v1:=e.Group("/v1")
    v1.UseMiddleware(RequestMiddleware())
    {
      v1.Get("/api", func(c *Route.Context) {
        c.JsonResponse(100, "成功", nil)
      })
    }
}

/**
请求日志拦截
*/
func RequestMiddleware()  Route.HttpFunc {
  return func(c *Route.Context) {
    var param interface{}
    if strings.ToLower(c.Request.Method) == "post"{
      param=c.Request.Form
    }else{
      param=c.Request.URL.Query()
    }
    body, _ := ioutil.ReadAll(c.Request.Body)
    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
    LogHelper.LoggerStd().Debug("request\r\nurl:%s\r\naddr: %s\r\nheader: %s\r\ncooke:%s\r\nmethod: %s\r\nparam:%s\r\nbody:%s",c.Request.URL, c.Request.RemoteAddr, c.Request.Header,c.Request.Cookies(), c.Request.Method, param, body)
		//Log.LogsFile("request.log", "request\r\nurl:%s\r\naddr: %s\r\nheader: %s\r\ncooke:%s\r\nmethod: %s\r\nparam:%s\r\nbody:%s",c.Request.URL, c.Request.RemoteAddr, c.Request.Header,c.Request.Cookies(), c.Request.Method, param, body)
    }
}

Documentation

Index

Constants

View Source
const (
	DEFAULT_CODE_KEY = "code"
	DEFAULT_MSG_KEY  = "message"
	DEFAULT_DATA_KEY = "data"
)

Variables

This section is empty.

Functions

func NewHttpServer

func NewHttpServer(publicAsset *embed.FS) *httpServer

func NewHttpServerMode

func NewHttpServerMode(publicAsset *embed.FS, isDebug bool) *httpServer

* 静态声明

Types

type EricServer

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

func NewEricServer

func NewEricServer(publicAsset *embed.FS) *EricServer

func (*EricServer) RegisterHttpRoute

func (s *EricServer) RegisterHttpRoute(httpRoute ...HttpRouteHandle) *EricServer

* 注册http路由

func (*EricServer) RegisterServerBuilder

func (s *EricServer) RegisterServerBuilder(builder ...*EricServerBuilder) *EricServer

* 注册多个服务

func (*EricServer) SetResponseKey

func (s *EricServer) SetResponseKey(codeKey, messageKey, dataKey string) *EricServer

* 设置json响应key @param string codeKey 响应code @param string messageKey 响应message @param string dataKey 响应data

func (*EricServer) SetStaticDir

func (s *EricServer) SetStaticDir(httpDir, sourceDir string) *EricServer

* 设置静态目录 @param httpDir string js css 等http访问 @param sourceDir string js css 绝对路径

func (*EricServer) SetStaticDirs

func (s *EricServer) SetStaticDirs(staticSource ...*StaticStaticResources) *EricServer

注册静态资源访问

func (*EricServer) SetTemplate

func (s *EricServer) SetTemplate(sourceDir, subFix string) *EricServer

* 设置模板 @param sourceDir string 模板存放目录 @param subFix string 模板后缀

func (*EricServer) SetTlsConf

func (s *EricServer) SetTlsConf(tlsConf *HttpTlsConf)

设置https 配置

func (*EricServer) SetUploadDir

func (s *EricServer) SetUploadDir(httpDIr, sourceDIr string) *EricServer

* 设置上传访问动态解析dir

func (*EricServer) Start

func (s *EricServer) Start(host string, port int, nodeId int, mode MODE_CONST)

type EricServerBuilder

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

*

func NewEricServerBuilder

func NewEricServerBuilder(publicAsset *embed.FS, serverName string, serverMode MODE_CONST, host string, port int) *EricServerBuilder

func (*EricServerBuilder) RegisterHttpRoute

func (this *EricServerBuilder) RegisterHttpRoute(httpRoute ...HttpRouteHandle) *EricServerBuilder

* 注册http路由

func (*EricServerBuilder) SetNodeId

func (this *EricServerBuilder) SetNodeId(nodeId int)

func (*EricServerBuilder) SetResponseKey

func (this *EricServerBuilder) SetResponseKey(codeKey, messageKey, dataKey string) *EricServerBuilder

* 设置json响应key @param string codeKey 响应code @param string messageKey 响应message @param string dataKey 响应data

func (*EricServerBuilder) SetServerName

func (this *EricServerBuilder) SetServerName(serverName string)

func (*EricServerBuilder) SetStaticDir

func (this *EricServerBuilder) SetStaticDir(httpDir, sourceDir string) *EricServerBuilder

* 设置静态目录 @param httpDir string js css 等http访问 @param sourceDir string js css 绝对路径

func (*EricServerBuilder) SetStaticDirs

func (this *EricServerBuilder) SetStaticDirs(staticSource ...*StaticStaticResources) *EricServerBuilder

* 批量设置静态资源目录

func (*EricServerBuilder) SetTemplate

func (this *EricServerBuilder) SetTemplate(sourceDir, subFix string) *EricServerBuilder

* 设置页面模板 @param sourceDir string 模板存放目录 @param subFix string 模板后缀

func (*EricServerBuilder) SetTlsConf

func (this *EricServerBuilder) SetTlsConf(tlsConf *HttpTlsConf)

func (*EricServerBuilder) SetUploadDIr

func (this *EricServerBuilder) SetUploadDIr(httpDir, sourceDir string) *EricServerBuilder

* 设置上传访问动态解析dir @param httpDir string http访问目录 @param sourceDir string string本地存放目录

type Err

type Err struct {
	Code    int    // 错误码
	Message string // 展示给用户看的
	Errord  error  // 保存内部错误信息
}

定义错误

func (*Err) Error

func (err *Err) Error() string

type Errno

type Errno struct {
	Code    int
	Message string
	// contains filtered or unexported fields
}

定义错误码

func (Errno) Error

func (err Errno) Error() string

type HttpRouteHandle

type HttpRouteHandle func(e *Route.Engine)

type HttpTlsConf

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

func NewHttpTlsConf

func NewHttpTlsConf(certFilePath string, keyFilePath string) *HttpTlsConf

type KError

type KError struct {
	Code    int
	Message string
	// contains filtered or unexported fields
}

func NewKError

func NewKError(code int, message string, err ...error) *KError

func (*KError) Error

func (t *KError) Error() string

type MODE_CONST

type MODE_CONST string
const (
	MODE_CONST_DEV MODE_CONST = "dev"
	MODE_CONST_PRO MODE_CONST = "pro"
)

type RpcParam

type RpcParam struct {
	JsonVer   string `json:"json_ver"`
	RequestId string `json:"request_id"`
	Version   string `json:"ver"`
}

*默认传参*

type StaticStaticResources

type StaticStaticResources struct {
	HttpDir   string //http访问
	SourceDir string //本地路径
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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