使用gin-gonic编写Web服务的最佳实践是什么?

huangapple go评论88阅读模式
英文:

What is the best practices of writing web services using gin-gonic

问题

在其他框架(如Ruby on Rails)中,它们有类似的目录结构,用于存放与模型相关的代码、视图相关的代码、控制器相关的代码以及种子数据库的位置。在gin-gonic中有没有类似的目录组织结构?或者有什么建议?

英文:

In other frameworks like Ruby on Rails, they are having directory structure like where to keep model related code, where to keep view related code, where to keep controller related code and where to seed DB. Is there any directory organization in gin-gonic? Or Any suggestion?

答案1

得分: 3

Gin不是一个有主见的框架。这是我为REST服务遵循的目录结构:

  1. Contracts(合同)

    • 包含请求者和服务之间的合同
    • 每个资源的请求结构
    • 每个资源的响应结构
    • 错误响应结构
  2. Core(核心)

    • 这个目录包含实际工作的代码
    • 它与请求最初是http无关
  3. HTTPService(HTTP服务)

    • 路由
    • HTTP处理程序
    • 处理HTTP请求的代码等

这种方法的原因是,代码库不会与HTTP紧密耦合,以后可以添加ThriftService,整个服务器就可以作为Thrift服务使用了。

英文:

Gin is not an opinionated framework.This is the directory structure I follow for REST services

  1. Contracts
  • Contains the contracts between requester and the service
  • request structure per resource
  • response structure per resource
  • error response structures
  1. Core
  • This directory contains code that does the actual work
  • It will be irrespective of the fact that the request originally was http
  1. HTTPService
  • routes
  • http handlers
  • code that handles http request etc.

The reasoning behind this approach is that the code base won't be much coupled to HTTP and later one may add ThriftService and the whole server is ready for being used as a thrift service

huangapple
  • 本文由 发表于 2016年11月11日 14:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/40542005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定