This topic describes how to deploy a Gin application to Function Compute. Function Compute provides a method different from traditional deployment methods. You can deploy a traditional Gin application to Function Compute and use the application for production without performing steps such as purchasing a machine. Your Gin application can use the auto-scaling, pay-as-you-go, and O&M-free features of Function Compute.

Background information

Gin is a microframework written in Go, which features elegant encapsulation, a user-friendly API, and clear source code annotations. Gin is fast, flexible, and fault-tolerant.

Step 1: Prepare the environment

You do not need to install Docker but only Funcraft. The easiest way to install Funcraft is to download the executable binary files.

  1. Install Funcraft on the local machine. For more information, see Install Funcraft.
  2. Run the fun --version command to check whether the installation is successful.

Step 2: Migrate the application

Example 1

  1. Install Gin in an environment that has Go 1.11 or later installed. For more information, see the official sample.
    go get -u github.com/gin-gonic/gin                   
  2. When you create a project named example.go, write the following code into the project:
    package main
    import "github.com/gin-gonic/gin"
    func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
    }
                            
  3. Run the following command to run the local project:
    go run example.go                    
  4. Run the fun deploy -y command to deploy the project to Function Compute.
    fun deploy -y
    The following results are returned:
    current folder is not a fun project.
    Fun detected your application doesn't listen on '0.0.0.0:9000' in example.go
    Fun will replace your addr to '0.0.0.0:9000', and also backup your origin file example.go to example.go.bak
    ? Are your sure? Yes
    Could not find any bin files from current folder.
    Before using 'fun deploy', you must use 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"' to comile your project.
    ? Let Fun exec this command for you? Yes
    Executing command 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"'...
    Tips: 
    You must use 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"' to recompile your project every time before using fun deploy.
    Generating template.yml...
    Generate Fun project successfully!
    ========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
    ...  ...   ....
                    trigger httpTrigger deploy success
            function express deploy success
    service express deploy success
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Request a new temporary domain ...
    The assigned temporary domain is 15014775-1986***.test.functioncompute.com, expired at 2020-04-03 09:52:55, limited by 1000 per day.
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success                        
    Function Compute must start the service and listen to Port 0.0.0.0:9000. In the deployment log, Funcraft attempts to detect the start port of the application. If the ports do not match, press enter to modify the port, and then Funcraft automatically detects the executable program generated by the build. If no executable program is detected, you are prompted to compile with the specified command. Funcraft will compile the code after you press Enter. bootstrap file and template.yml file, and finally automatic deployment.

    After the deployment is successful, you can see the temporary domain name generated by Function Compute in the log. Through this temporary domain name, you can directly access the just deployed application.

    Note The temporary domain name is used only for demonstration and development purposes. It is valid only for a limited time. To use an application in a production environment, you must bind a domain name that has been filed with Alibaba Cloud. For more information, see Bind a custom domain name.

Example 2

  1. Run the following command to clone the sample project to your local machine. For more information, see the official sample.
    git clone https://github.com/tanhe123/mdblog.git                        
  2. Modify the configuration file.
    1. In the config directory, rename the config.example.toml file as config.toml.
    2. Open the config.toml file and modify the following items:
      • Change port=8091 to port=9000, so that your application starts on port 9000.
      • Change debug = true to debug = false, so that the production version is used.
      • Change dir = "logs" to dir = "/tmp" to write logs to the /tmp directory. If Apsara File Storage NAS is not mounted, Function Compute has read and write permissions only for this directory.
  3. Run the following command to compile the project:
    go build                   
  4. Run the following command to run the local project:
    ./mdblog                    
  5. Run the fun deploy -y command to deploy the project to Function Compute.
    fun deploy -y

    After the project is deployed, check the logs to find the temporary domain name that Function Compute generated. You can use this temporary domain name to access the deployed application.

    Note The temporary domain name is used only for demonstration and development purposes. It is valid only for a limited time. To use an application in a production environment, you must bind a domain name that has been filed with Alibaba Cloud. For more information, see Bind a custom domain name.