百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

gin-api-demo

> 编程语言
开源

使用 Go 中的 Gin 框架创建一个具有 JSON API 所需大部分核心功能的 JSON API 的示例。

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 Go 中的 Gin 框架创建一个具有 JSON API 所需大部分核心功能的 JSON API 的示例。

Gin API Demo

An example of using the Gin framework in Go to create a JSON api with most of the core functionality a JSON api needs.

Installation

Install Glide

bash
curl https://glide.sh/get | sh
# or, on OSX
brew install glide

Check out the project and install it's dependencies

bash
$ go get github.com/sbecker/gin-api-demo
$ cd $GOPATH/src/github.com/sbecker/gin-api-demo/
$ glide install

How to run

In one terminal, run the server:

bash
$ cd $GOPATH/src/github.com/sbecker/gin-api-demo
$ go run main.go

In another terminal, try making requests:

bash
# Get all users as a non-admin - see only one user, with a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users
bash
# Get all users as an admin - see all users, with all json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users
bash
# Try to get all users as a non-user - get 401 Unauthorized
$ curl -i http://localhost:8080/users
bash
# Get a single user as a non-admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/1
bash
# Get a single user as an admin - see a subset of json attributes
$ curl -H 'Authorization: Bearer userB' -i http://localhost:8080/users/1
bash
# Try to get a different user than self - get 404 Not Found
$ curl -H 'Authorization: Bearer userA' -i http://localhost:8080/users/2
bash
# Try to get a single user as a non-user, get 401 Unauthorized
$ curl -i http://localhost:8080/users/2

Done:

  • basic restful routing - GET /users, GET /users/:id
  • token authorization middleware
  • multi-level json serialization - display subset of json objects for non-admin users
  • separation of concerns - models, dao, resources, serializers, authorization
  • Glide for dependency management
  • UUIDs instead of integers for primary keys
  • Request-Id headers: Each API request has an associated request identifier
  • Cross-origin-request headers
  • Continuous integration
  • JSON logging
  • Env var configuration

Todo:

  • JWT token authorization
  • Pagination
  • Versioning
  • Standardize error responses
  • Integration with a real database
  • Swagger live documentation
  • Godoc
  • A test suite
  • Database migrations
  • Continuous deployment
  • Command line interface - run server, run migrations, start db console, etc
  • CLI with commands for migration, db drop/create/reset

Research:

  • Client side query language - a la GraphQL/Falkor/Datomic

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Go

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言