Orange quickly build a framework for the use of having an elegant exit function HTTP service

Previous we introduced the relevant details and the use of summary procedure Golang elegant exit: https://www.cnblogs.com/zhucheer/p/12337277.html

Today we use the framework to quickly build a Web program with a go elegant exit function.

first step:

GOPATH directory created under a directory demo (are free), and create a main.go in the directory, and writes the code:

main Package 

Import (
"gitee.com/zhucheer/orange/app"
"FMT"
"Time"
)

FUNC main () {
Router: = {} the Route &
app.AppStart (Router)
}

type the Route struct {
}

FUNC (S * the Route ) ServeMux () {
app.NewRouter ( ""). GET ( "/", FUNC (ctx * app.Context) error {
// after adding processing logic by which, after the process exits will wait for the completion of the processing logic of ExitWaitFunDo then exit;
app.ExitWaitFunDo (FUNC () {
// RUN something
time.sleep (. 5 * time.Second)
fmt.Println ( "the Shutdown gracefully")
})
return ctx.ToString ( "the Hello World!")
})

}

Step Two: We go mod will depend on the relevant installation;

We first configure the environment variables and domestic agents go according to their own version;

go less than version 1.13

the window executed in cmd: 
SET GO111MODULE = ON 
SET GOPROXY : = HTTPS // goproxy.cn 

performed under Linux: 
Export GO111MODULE = ON 
Export GOPROXY = HTTPS: // goproxy.cn

go version 1.13 or greater

go env -w GOPROXY=https://goproxy.cn,direct

Then execute the following command in the current project directory:

the init go mod // initialize go mod package management 
go mod Tidy // load dependencies 
go mod Vendor // will depend on the package copied to the project directory to 
go run main.go

 

Not surprisingly, the program started, as shown below:

We can try to access http://127.0.0.1:8088, we have a code logically print "Shutdown gracefully" Wait 5s, you can Ctrl immediately after refresh the browser page + C to make the program exits, we can console clearly see that the program is waiting for printing "Shutdown gracefully" and then exit the program.

 

Orange can be easily constructed using the framework of a gracefully exit the http service, we recommend that you try;

It also includes a variety of packaged Web development components, interested students can try:

Code: https://gitee.com/zhucheer/orange

Documentation: https://www.kancloud.cn/chase688/orange_framework/1448035

Guess you like

Origin www.cnblogs.com/zhucheer/p/12341595.html