Code Monkey home page Code Monkey logo

api-server-demo's Introduction

api-server-demo

Using go development language to develop server API interface

描述

  • 用go开发语言开发服务器API接口

目录

  • 使用go modules 初始化项目
  • 启动一个最简单的 RESTful API 服务器
  • 配置文件读取
  • 初始化Mysql数据库并建立连接
  • 自定义业务错误信息
  • 读取和返回HTTP请求
  • 用户业务逻辑处理(业务处理)
    • 注册
    • 登录
  • HTTP调用添加自定义处理逻辑
  • 路由中间件
    • 路由请求校验
    • 分类使用方式
    • 自定义中间件
    • 签名验证
      • 开启JWT认证
    • 记录和管理API日志
    • 异常捕获
    • 链路追踪
  • 参数验证(validator.v9)
    • 模型绑定和验证
    • 自定义验证器
  • 请求头
    • 自定义请求头
    • Cookies应用
    • Session应用
  • API身份验证
  • API性能分析
  • 支持系统检测接口(如cpu,disk,health,memery)
  • 给API命令增加版本号功能
  • 生成Swagger在线文档
  • 给API增加启动脚本
  • 语言文件支持(zh and en)
  • 支持优雅地重启或停止
  • 支持多种运行模式
  • 支持打包并压缩脚本

操作

  • 安装
go get -u github.com/dsw0214/api-server-demo
  • 初始化表格

    • 导入测试sql到数据库
  • 修改配置

    • conf/config.yaml
  • 直接运行

go run .
  • 打包运行
go build .
  • 打包并压缩脚本
sh build_compress.sh 
upx is /usr/local/bin/upx
***Use upx Compress beging***
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2018
UPX 3.95        Markus Oberhumer, Laszlo Molnar & John Reiser   Aug 26th 2018

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
  15739628 ->   5836816   37.08%   macho/amd64   api-server-demo               

Packed 1 file.

Api接口范例

  • 注册
curl http://127.0.0.1:8888/register -X POST -d "Username=test01&Password=123456"
{"code":20000,"message":"OK","data":{"username":"test01","message":"Register Success"}}
  • 登录
curl http://127.0.0.1:8888/login -X POST -d "Username=test01&Password=123456"
{"code":20000,"message":"OK","data":{"hello":"Welcome","userName":"test01"}}

扩展包

  • Gin is a web framework written in Go (Golang)
  • Viper is Go configuration with fangs!
  • Gorm is fantastic ORM library for Golang
  • log is log package
  • Mysql is a MySQL driver for Go's
  • Bcrypt is a Encryption package
  • pprof is HTTP server runtime profiling data in the format expected by the pprof visualization tool.

国内代理

api-server-demo's People

Contributors

dsw0214 avatar shiweitu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

coderlee

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.