golang快速入门—echo构建web应用

golang快速入门—echo构建web应用,第1张

概述 echo web框架是go语言开发的一种高性能,可扩展,轻量级的web框架。echo框架真的非常简单,几行代码就可以启动一个高性能的http服务端。类似python的django框架可设置cookie,session,文件上传,获取用户ip等 依赖安装
go get github.com/labstack/echo/v4 # web框架
go get -u github.com/swaggo/echo-swagger # echo中使用swagger文档
go get github.com/swaggo/swag/cmd/swag # swag文档生成工具
业务实 *** 初始化基础框架
package test_echo

import (
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

var echoWeb *echo.Echo
var visitTotal int // 统计访问量
// 自定义中间件,下面是统计访问量
func CountMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
	return func(context echo.Context) error {
		visitTotal++
		context.Response().Header().Add("visit-total", fmt.Sprintf("%d", visitTotal))
		return next(context)
	}
}

// 初始化业务需要基础 *** 作,下面是初始化echo,以及中间件
func init() {
	echoWeb = echo.New()
	echoWeb.Debug = true
	echoWeb.Use(middleware.Recover()) // 主要用于拦截panic错误并且在控制台打印错误日志,避免echo程序直接崩溃
	echoWeb.Use(middleware.Logger())  // Logger中间件主要用于打印http请求日志
	echoWeb.Use(CountMiddleware)
}

// 全局使用echo对象
func GetEcho() *echo.Echo {
	return echoWeb
}

定义业务板块
package test_echo

import (
	"github.com/labstack/echo/v4"
	"net/http"
)

// -----------------------------------API HelloWorldHandler
type HelloWorldHandler struct{}

// @Summary Test
// @Description 输出hello world
// @Tags Test
// @Accept json
// @Produce json
// @Success 200 {string} string "Success"
// @Failure 400 {string} string "Failed"
// @Failure 404 {string} string "Not Found"
// @Failure 500 {string} string "Internal Error"
// @Router /hello-world [get]
func (receiver HelloWorldHandler) main(c echo.Context) error {
	return c.String(http.StatusOK, "hello world")
}
func (receiver HelloWorldHandler) router() {
	GetEcho().Add("GET", "/hello-world", receiver.main)
}

// -----------------------------------API PrintName
type PrintName struct{}
type PrintNameResponse struct {
	Name string `json:"name"`
}

// @Summary Test
// @Description 根据参数输出name
// @Tags Test
// @Accept json
// @Produce json
// @Param name query string true "名称"
// @Success 200 {object} PrintNameResponse PrintNameResponse{}
// @Failure 400 {string} string "Failed"
// @Failure 404 {string} string "Not Found"
// @Failure 500 {string} string "Internal Error"
// @Router /print-name [get]
func (receiver PrintName) main(c echo.Context) error {
	name := c.QueryParam("name") // 获取query参数
	return c.JSON(http.StatusOK, PrintNameResponse{Name: name})
}
func (receiver PrintName) router() {
	GetEcho().Add("GET", "/print-name", receiver.main)
}

将业务与框架串联
package test_echo

import echoSwagger "github.com/swaggo/echo-swagger"
import _ "casbin-go/docs"

// 设置router
func SetRouter() {
	HelloWorldHandler{}.router()
	PrintName{}.router()
	GetEcho().GET("/docs/*", echoSwagger.WrapHandler) // 引入swagger文档接口
}

// 启动router
func StartRouter() {
	SetRouter()
	GetEcho().Start(":8080")
}

执行
package main

import (
	"casbin-go/test-echo"
	"fmt"
)

// @title Swagger Example API
// @version 1.0
// @description GO测试文档.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
func echoTest() {
	test_echo.StartRouter()
}

func main() {
	echoTest()

}
结论 注意启动之前先执行
swag init # 生成api文档
通过如下地址查看文档并调用接口 http://127.0.0.1:8080/docs/index.html#/

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/995803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存