Coder Social home page Coder Social logo

aa12gq / webapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from farseer-go/webapi

0.0 0.0 0.0 306 KB

用于快速构建api服务,带来极简、优雅的开发体验。编写api服务时,不需要使用httpRequest、httpResponse等数据结构。

Home Page: https://farseer-go.gitee.io/

License: MIT License

Shell 0.10% Go 99.90% HTML 0.01%

webapi's Introduction

webapi 概述

包:"github.com/farseer-go/webapi"

模块:webapi.Module

codecov Build

用于快速构建api服务,带来极简、优雅的开发体验。编写api服务时,不需要使用httpRequest、httpResponse等数据结构。

webapi使用了中间件的管道模型编写,让我们加入非业务逻辑时非常简单。

包含两种风格来提供API服务:

  • MinimalApi:动态API风格(直接绑定到逻辑层)
  • Mvc:Controller、Action风格

使用minimalApi时,甚至不需要UI层来提供API服务。

webapi有哪些功能

  • 支持中间件
  • 入参、出参隐式绑定
  • 支持静态目录绑定
  • ActionFilter过虑器
  • ActionResult抽象结果
  • Area区域设置
  • MinimalApi模式
  • Mvc模式
    • HttpContext上下文
    • Header隐式绑定

大部份情况下,除了main需要配置webapi路由外,在你的api handle中就是一个普通的func函数,不需要依赖webapi组件。webapi会根据func函数出入参隐式绑定数据

func main() {
	fs.Initialize[webapi.Module]("FOPS")
	webapi.RegisterPOST("/mini/hello1", Hello1)
	webapi.RegisterPOST("/mini/hello3", Hello3, "pageSize", "pageIndex")
	webapi.Run()
}

// 使用结构(DTO)来接收入参
// 返回string
func Hello1(req pageSizeRequest) string {
	return fmt.Sprintf("hello world pageSize=%d,pageIndex=%d", req.PageSize, req.PageIndex)
}

// 使用基础参数来接收入参
// 返回pageSizeRequest结构(会自动转成json)
func Hello3(pageSize int, pageIndex int) pageSizeRequest {
    return pageSizeRequest{
        PageSize:  pageSize,
        PageIndex: pageIndex,
    }
}

// 也可以定义一个结构,用于接收参数
type pageSizeRequest struct {
    PageSize  int
    PageIndex int
}

函数中,出入参都会自动绑定数据

如果是application/json,则会自动被反序列化成model,如果是x-www-form-urlencoded,则会将每一项的key/value匹配到model字段中

可以看到,整个过程,不需要json序列化httpRequesthttpResponse的操作。

webapi's People

Contributors

steden avatar huangshanming avatar mengyan258 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.