A small API development framework based on Go, can quickly develop business, follow the SOLID design principles

Posted May 26, 20203 min read

snake

A Go framework suitable for rapid business development, mainly to provide API services.

Project address: https://github.com/1024casts/snake

Technology Stack

  • Framework routing uses gin routing
  • Middleware uses gin framework middleware
  • Database component gorm
  • The document is generated using swagger
  • Configuration file parsing library viper
  • Use JWT for identity authentication
  • Validator validator is also the default validator of gin framework
  • Task scheduling cron
  • Package management tool go module
  • Test framework goConvey
  • CI/CD Github Actions

characteristic

  • Follow RESTful API design specifications
  • Based on the GIN WEB framework, provides rich middleware support(user authentication, cross-domain, access log, request frequency limit, tracking ID, etc.)
  • Database storage based on GORM
  • JWT certification
  • Support Swagger document(based on swaggo )
  • Use make to manage Go projects
  • Use shell(admin.sh) script to manage process
  • Support multiple environment configuration

Directory Structure

    Makefile # project management file
    conf # Unified storage directory of configuration files
    config # Go package dedicated to processing configuration and configuration files
    db.sql # When deploying a new environment, you can log in to the MySQL client and execute source db.sql to create databases and tables
    docs # swagger documentation, generated by swag init
    handler # Similar to C in MVC architecture, used to read input and forward the processing flow to the actual processing function, and finally return the result
    log # Directory for storing logs
    main.go # project entry file
    model # database model
    pkg # Some packaged packages
    repository # Data access layer
    router # Routing and middleware directory
    service # Business logic package
    schedule # Task scheduling configuration directory
    scripts # Store scripts used to perform various construction, installation, analysis and other operations

Download and install

# Go to your own development directory, download and install, you can use GOPATH
git clone https://github.com/1024casts/snake

Quick start

TIPS:requires local database and redis installation

//Go to the download directory
cd snake

//Generate local environment configuration file
cp config.sample.yaml config.local.yaml

//compile
make build

//run
./scripts/admin.sh start

Common commands

  • make help
  • make dep download go dependency package
  • make build
  • make swag-init generates interface documentation
  • make test-coverage generates test coverage
  • make lint check code specification

Module

  • User(example)

Interface documentation

http://localhost:8080/swagger/index.html

Development protocol

deploy

Separate deployment

After uploading to the server, simply run the command

./scripts/admin.sh start

Docker deployment

If docker is installed, you can start the application with the following command:

//run
docker-compose up -d

//verify
http://127.0.0.1/health

Supervisord

If Supervisord is installed, you can add the following to the configuration file(default:/etc/supervisor/supervisord.conf):

[program:snake]
# environment =
directory =/home/go/src/snake /
command =/home/go/bin_snake
autostart = true
autorestart = true
user = root
stdout_logfile =/home/log/snake_std.log
startsecs = 2
startretries = 2
stdout_logfile_maxbytes = 10MB
stdout_logfile_backups = 10
stderr_logfile =/home/log/snake_err.log
stderr_logfile_maxbytes = 10MB
stderr_logfile_backups = 10

Restart Supervisord

supervisorctl restart

CHANGELOG

Everyone is welcome to discuss, communicate, and mention PR. If you like, you can give a star: https://github.com/1024casts/snake