当前位置: 首页 > 软件库 > 云计算 > 云原生 >

go-api-boilerplate

授权协议 MIT License
开发语言 Google Go
所属分类 云计算、 云原生
软件类型 开源软件
地区 不详
投 递 者 鲁辉
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

�� Golang API Starter Kit

logo

Go Server/API boilerplate using best practices, DDD, CQRS, ES, gRPC.

Table of Contents

�� ABOUT

The main purpose of this project is to provide boilerplate project setup using best practices, DDD, CQRS, ES, gRPC. Featuring kubernetes for both development and production environments. Allowing to work with environment reflecting production one, allowing to reduce any misconfigurations.

This is mono-repository of many services such as authentication or user domain. Each service has it own code base with exception of shared packages to simplify things for this boilerplate. Services communicate witch each other using gRPC. Each service might expose HTTP API for external communication or/and gRPC.

This project setup should reduce the time spent on environment configuration for the whole kubernetes cluster and/or each of microservice. Extracting each of services to own repository or keeping it as mono-repo should be a matter of preference.

Please look for comments like @TODO and @FIXME to better understand things than need attention.

Web UI example (React)

This boilerplate includes simple Web UI to demonstrate example interaction with API.Once deployed and hosts are set please visit https://api.go-api-boilerplate.local to access UI.

Web UI

Key concepts:

  1. Rest API
  2. Docker
  3. Kubernetes
  4. Helm chart
  5. Terraform
  6. gRPC
  7. Domain Driven Design (DDD)
  8. CQRS
  9. Event Sourcing
  10. Hexagonal, Onion, Clean Architecture
  11. oAuth2

Worth getting to know packages used in this boilerplate:

  1. gorouter
  2. message-bus
  3. gollback
  4. shutdown
  5. pubsub
  6. pushpull
  7. gocontainer

�� DOCUMENTATION

�� EXAMPLE

Quick start

Localhost alias

Edit /etc/hosts to add localhost alias

➜ go-api-boilerplate git:(master) cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 go-api-boilerplate.local api.go-api-boilerplate.local maildev.go-api-boilerplate.local mysql.go-api-boilerplate.local

Build release

Local image

make docker-build BIN=auth
make docker-build BIN=migrate
make docker-build BIN=user
make docker-build BIN=web

GitHub Package Registry

Creating tag with metadata will trigger github workflow and publish docker image to GitHub Package Registry.

Tag v1.0.0+user will trigger build for user service releasing 1.0.0 docker image tag.you can create release for all services in cmd directory.

v1.0.0+auth
v1.0.0+user
v1.0.0+web
v1.0.0+migrate

Replace image details in main.yaml

image:
-    repository: go-api-boilerplate-user
+    repository: docker.pkg.github.com/vardius/go-api-boilerplate/go-api-boilerplate-user
-    tag: latest
+    tag: 1.0.0
    pullPolicy: IfNotPresent

repeat for all services and migrate init containers.

Private Registry

Log in to Docker

docker login

Copy docker config

cp ~/.docker/config.json ./k8s/.docker/config.json

Verify config.json

Deploy release

make terraform-install

Destroy

make terraform-destroy

If persistent volume is stack in terminating, this happens when persistent volume is protected. You should be able to cross verify this:

kubectl describe pvc PVC_NAME --namespace=go-api-boilerplate | grep Finalizers

Output:
Finalizers:    [kubernetes.io/pvc-protection]

You can fix this by setting finalizers to null using kubectl patch:

kubectl patch pvc PVC_NAME --namespace=go-api-boilerplate -p '{"metadata":{"finalizers": []}}' --type=merge

Build tags

Build flags are used for different persistence layers. Please see services.go file for details. Provided layers are mysql, mongo and memory.If desired in similar way new layer can be easily added, following given patter.

go build -tags=persistence_mysql

Available build tags

  • persistence_mysql (mysql service container)
  • persistence_mongodb (mongodb service container)

Importantpersistence layer defaults to memory if no flag is provided (Docker image sets persistence_mysql flag), see each service Dockerfile for details.

Domain

Dispatching command

Send example JSON via POST request

curl -d '{"email":"test@test.com"}' -H "Content-Type: application/json" -X POST https://api.go-api-boilerplate.local/users/v1/dispatch/user/user-register-with-email --insecure

View

Public routes

Get user details https://api.go-api-boilerplate.local/users/v1/34e7ed39-aa94-4ef2-9422-401bba9fc812

{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"test@test.com"}

Get list of users https://api.go-api-boilerplate.local/users/v1?page=1&limit=10

{"page":1,"limit":20,"total":1,"users":[{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"test@test.com"}]}

Protected routes

Access protected route using auth token https://api.go-api-boilerplate.local/users/v1/me.

{"code": "401","message": "Unauthorized"}

Request access token for user

curl -d '{"email":"test@test.com"}' -H "Content-Type: application/json" -X POST https://api.go-api-boilerplate.local/users/v1/dispatch/user/user-request-access-token --insecure

Get your access token from mail catcher https://maildev.go-api-boilerplate.local.

Access protected route using auth token https://api.go-api-boilerplate.local/users/v1/me?authToken=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJyXHUwMDE277-977-977-977-9IiwiZXhwIjoxNTU5NjEwOTc2LCJzdWIiOiIzNGU3ZWQzOS1hYTk0LTRlZjItOTQyMi00MDFiYmE5ZmM4MTIifQ.pEkgtDAvNh2D3Dtgfpu4tt-Atn1h6QwMkDhz4KpgFxNX8jE7fQH00J6K5V7CV063pigxWhOMMTRLmQdhzhajzQ

{"id":"34e7ed39-aa94-4ef2-9422-401bba9fc812","email":"test@test.com"}

�� Sponsoring

�� Contributing

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas?We are using the github issue tracker to manage them.

����‍������‍�� Core Team:


Rafał Lorenz

Marin Kirkov

�� Backers

Support us with a monthly donation and help us continue our activities.

�� Sponsors

Proudly sponsored by Open Collective sponsors.

�� License

  • API简介 vpp其实也有自己的control-plane。它们之间的就是使用API来交互,底层是用的共享内存机制。 control-plane可以是使用不同的语言来写,支持C/python/java/go 在这里了解的是用C语言与vpp通信。如图1所示。VAT通过命令行来控制VPP。 图1,VAT(vpp api test)与vpp通信: +--------------+

  • 添加新API和controller 说明 这里需要说明一下,新API和controller之间的关系,本质上说他们没有直接关系。 CRD 在kubernetes中添加新API的方法叫CRD(另外一种叫做聚合层,不是本文关注的重点,感兴趣的同学可以自行参考官网),是一种在不改变原有API Server接口的情况动态扩展API方法。在本文档中如无特殊说明扩展API等同于CRD。每个CRD对应着一种资源

  • 将本地数据库迁移到服务器 我将引导您完成将现有Go API转换为无服务器并将其部署到具有AWS Severless Application Model(SAM)的 AWS Lambda和API Gateway的过程。 整个过程应少于10分钟。 让我们开始吧! 1.设定 我们的示例API使用HttpRouter 包,因此我们首先安装它。 $ go get github.com/julienschmi

 相关资料
  • go_sogou_api 推广API 基于webservice,主要实现账户结构和账户报表下载,用于数据分析。 Install go_sogou_api go get github.com/nanjishidu/go_sogou_api/... Import go_sogou_api import v1 "github.com/nanjishidu/go_sogou_api/v1"

  • 关于 go-gin-api 是基于 Gin 进行模块化设计的 API 框架,封装了常用的功能,使用简单,致力于进行快速的业务研发,同时增加了更多限制,约束项目组开发成员,规避混乱无序及自由随意的编码。 供参考学习,线上使用请谨慎! 集成组件: 支持 rate 接口限流 支持 panic 异常时邮件通知 支持 cors 接口跨域 支持 Prometheus 指标记录 支持 Swagger 接口文档生

  • Pokemon-GO-node-api 是 Pokemon GO API Node.js 库。 安装 & 使用: npm install pokemon-go-node-api var Pokeio = require('pokemon-go-node-api') 示例:

  • Google API Go Client 可以自动生成 Google 的 Go 语言 API。 示例代码: package mainimport (    "container/list"    "encoding/json"    "fmt"    "io/ioutil"    "log"    "math"    "math/rand"    "net/http"    "os"    "st

  • 初使化MinIO Client对象。 MinIO package main import ( "fmt" "github.com/minio/minio-go/v6" ) func main() { // 使用ssl ssl := true // 初使化minio client对象。 minioClient,

  • 我试图遵循Google Sheets API快速入门: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate (向下滚动到示例,然后单击GO) 以下是我尝试更新电子表格的方式: 输出: hello.go:43:结构初始化器中无效的字段名ValueInputOption h