Golang rapid development framework — adding gin framework (IV)

Time:2022-5-24

Golang rapid development framework — adding gin framework (IV)

background

The golang chapter of knowledge sharing is a record of all kinds of knowledge I learned when I use golang everyday. I will sort it out and share it with you in the form of articles for common learning. We welcome your continued attention.

The knowledge sharing series currently includes Java, golang, Linux, docker and so on.

development environment

  • System: windows10
  • Language: golang
  • Golang version: 1.17
  • Code warehouse:FastDevelopGo

content

We often need to use some basic components when we use golang to develop projects. Each new project is cumbersome, and the existing market feeling is not suitable for us. Therefore, we decided to build a set ourselves and open source it for everyone to use. We welcome you to put forward various needs.
Now let’s continue to improve the framework. The requirements to be completed in this section are:

  • increaseginFramework, which is convenient for our subsequent web development.

1. Install gin

go get -u github.com/gin-gonic/gin

2. Write the following code for initial use of the gin framework

package web

import (
    "FastDevelopGo/src/base/log"
    "FastDevelopGo/src/base/web/router"
    "github.com/gin-gonic/gin"
    "github.com/spf13/viper"
    "go.uber.org/zap"
)

type WebConfig struct {
    Host string `json:"host,omitempty"`
    Port string `json:"port,omitempty"`
}

//The web configuration file object is used for the initial use of global configuration
var webConfig WebConfig

func InitGin() {
    initGinConfig()
    startGin()
}

//Initial web related configuration files and objects
func initGinConfig() {
    //Initial read configuration file
    if err := viper.UnmarshalKey("web", &webConfig); err != nil {
        log. Logger. Error ("fatal error reading configuration file:", zap. Error (ERR))
        return
    }
}

//Core startup gin framework function, main function
func startGin() {
    //Initialize basic configuration
    r := gin.Default()
    //Initialize gateway
    router.InitRouter()
    r.Run(webConfig.Host + ":" + webConfig.Port)
}
package router

func InitRouter() {
    //Todo here we initialize various gateway configurations
    //Todo initializes default static resources
    //Todo initializes the default exception handling gateway
    //Todo initializes the default template directory
}

3. Make supplementary web related configuration on the basis of the original configuration file

#This is the default configuration file
[sys]
conf_type="toml"
conf_dir="conf/app"
conf_name="base"

[log]
level="debug"
encoding="json"
outputPaths=["stdout", "./tmp/logs"]
errorOutputPaths=["stderr"]
[log.initialFields]
foo = "bar"
[log.encoderConfig]
messageKey="message"
levelKey="level"
levelEncoder="lowercase"

[web]
host = "0.0.0.0"
port = "8080"

4. Start and test, observe the output of workbench log, and it has been used normally.

[GIN-debug] Listening and serving HTTP on 0.0.0.0:8080

Next, I’m going to add related initial exception handling and static resource loading. You are welcome to continue to pay attention to the development of the framework.

Note:
In this framework, I will add visualization page, code quick generation module, project framework quick generation module, etc. for my initial ideas. If you have other requirements and ideas, you are welcome to leave a message in the comment area or directly put forward valuable issues in the code warehouse

Welcome to start actively. Your attention is my biggest motivation.

This document declares that:

Golang rapid development framework -- adding gin framework (IV)

88×31.png

Knowledge sharing license agreement
This work is byCn HuashaouseCreative Commons Attribution – non commercial use 4.0 international license agreementLicense.