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.
- Install Funcraft. For more information, see Install Funcraft.
- Run the
fun --version
command to check whether the installation is successful.
- Configure Funcraft. For more information, see Configure Funcraft.
Step 2: Migrate the application
Example 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
- 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")
}
- Run the following command to run the on-premises application:
- Run the
fun deploy -y
command to deploy the application 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 on the 0.0.0.0:9000 port. For more
information, see
Overview. The deployment log indicates that Funcraft attempts to detect the startup port of
the application. If the ports do not match, press Enter to modify the port. 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. The
bootstrap file and
template.yml file are automatically generated and deployed.
After the project is deployed, check the log 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 for a limited period of time. To use an application in a production environment,
you must bind the application to a domain name that has obtained an Internet Content
Provider (ICP) filing from Alibaba Cloud. For more information, see
Bind a custom domain name.
Example 2
- 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
- Modify the configuration file.
- In the config directory, rename the config.example.toml file as config.toml.
- 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.
- Run the following command to compile the project:
- Run the following command to run the on-premises application:
- Run the
fun deploy -y
command to deploy the application to Function Compute.
After the application is deployed, you can 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 for a limited period of time. To use an application in a production environment,
you must bind the application to a domain name that has obtained an Internet Content
Provider (ICP) filing from Alibaba Cloud. For more information, see
Bind a custom domain name.