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 operations such as purchasing a server. The deployed Gin application
can use the auto scaling, pay-as-you-go, and O&M-free features of Function Compute.
Prerequisites
The following operations are complete:
Notice
The content about how to use Funcraft to migrate an application from a traditional
framework is no longer maintained. We recommend that you use Serverless Devs to migrate
relevant frameworks to Function Compute.
For more information about how to migrate Function Compute-related resources from
Funcraft to Serverless Devs, see Migrate resources from Funcraft to Serverless Devs.
For more information about how to use Serverless Devs to migrate an application from
a web framework, see Use Serverless Devs to migrate web frameworks.
We apologize for any inconvenience caused.
Background information
Gin is a microframework written in Go, and features elegant encapsulation, a user-friendly
API, and clear source code annotations. Gin is fast, flexible, and fault-tolerant.
Procedure
Example 1
- Install Gin in an environment where Go 1.11 or later is installed. For more information,
see Gin Web Framework.
go get -u github.com/gin-gonic/gin
- Create a project named example.go and write the following code:
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 on-premises project.
- Run the
fun deploy -y
command to deploy the application to Function Compute.fun deploy -y
The following result is 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 requires the startup service to listen on port 0.0.0.0:9000. For
more information, see
Overview. The deployment logs indicate that Funcraft attempts to detect the startup port of
the application. If the port does not meet the requirement, press the ENTER key. Then,
Funcraft changes the port and detects the executable program generated by the build.
If no executable program is detected, you are prompted to run the specified command
to compile a project. Alternatively, you can press the ENTER key. Then, Funcraft helps
you compile a project. After the project is compiled, the
bootstrap and
template.yml files required by Funcraft are automatically generated, and the project is automatically
deployed.
After the application is deployed, you can check the logs to find the temporary domain
name that is generated by Function Compute. 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. You must use a domain name that has obtained
an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more
information, see
Configure a custom domain name.
Example 2
- Run the following command to clone the sample project to your on-premises 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 an Apsara File Storage NAS file system is not mounted, Function Compute
has read and write permissions only on this directory.
- Run the following command to compile the project:
- Run the on-premises project.
- Run the
fun deploy -y
command to deploy the application to Function Compute.
After the application is deployed, you can check logs to find the temporary domain
name that is generated by Function Compute. You can use the 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. You must use a domain name that has obtained
an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more
information, see
Configure a custom domain name.