From c27a94194f9d611f9affb194232d19d2e02e4884 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Thu, 28 May 2020 09:34:37 +0000 Subject: [PATCH] Polish the Golang demo with Gin web framework --- demos/golang/README.md | 17 ++++++---- demos/golang/run_golang_on_occlum.sh | 4 +-- demos/golang/web_server.go | 47 +++++++++++++--------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/demos/golang/README.md b/demos/golang/README.md index 934ebd00..7f52fa44 100644 --- a/demos/golang/README.md +++ b/demos/golang/README.md @@ -1,20 +1,25 @@ # Use Golang with Occlum -This project demonstrates how Occlum enables [Golang](https://golang.org) programs running in SGX enclaves. +This project demonstrates how Occlum enables [Golang](https://golang.org) programs running in SGX enclaves, the demo program is a HTTP web server based on a widely used web framework([Gin](https://gin-gonic.com)) for Go. -Step 1: Build Golang web server program using the Occlum Golang toolchain(i.e., `occlum-go`) +Step 1: Install Gin with `occlum-go`, it may take a few minutes +``` +occlum-go get -u -v github.com/gin-gonic/gin +``` + +Step 2: Build the Golang web server using the Occlum Golang toolchain(i.e., `occlum-go`) ``` occlum-go build -o web_server -buildmode=pie ./web_server.go ``` -Step 2: You can run the web server demo on Occlum via +Step 3: You can run the web server demo on Occlum via ``` ./run_golang_on_occlum.sh ``` The HTTP web server should now start to listen on port 8090 and serve HTTP requests. -Step 3: To check whether the HTTP server works, run +Step 4: To check whether the HTTP server works, run ``` -curl http://127.0.0.1:8090/hello1 +curl http://127.0.0.1:8090/ping ``` -in another terminal, and get the response "hello,1". +in another terminal, and get the response `{"message":"pong"}`. diff --git a/demos/golang/run_golang_on_occlum.sh b/demos/golang/run_golang_on_occlum.sh index 273acf5d..0d10a858 100755 --- a/demos/golang/run_golang_on_occlum.sh +++ b/demos/golang/run_golang_on_occlum.sh @@ -16,8 +16,8 @@ fi rm -rf occlum_context && mkdir occlum_context cd occlum_context occlum init -jq '.vm.user_space_size = "320MB"' Occlum.json > temp_Occlum.json -jq '.process.default_mmap_size = "256MB"' temp_Occlum.json > Occlum.json +jq '.vm.user_space_size = "380MB"' Occlum.json > temp_Occlum.json +jq '.process.default_mmap_size = "300MB"' temp_Occlum.json > Occlum.json # 2. Copy program into Occlum Workspace and build cp ../web_server image/bin diff --git a/demos/golang/web_server.go b/demos/golang/web_server.go index f7b2bebd..2104dc58 100644 --- a/demos/golang/web_server.go +++ b/demos/golang/web_server.go @@ -1,29 +1,26 @@ package main -import "net/http" -import "log" -import "flag" -import "fmt" +import ( + "io" + "os" -type Controller struct {} -func (c Controller)ServeHTTP(writer http.ResponseWriter, request *http.Request){ - writer.Write([]byte("hello,1\n")); -} - -func hello(writer http.ResponseWriter, request *http.Request) { - writer.Write([]byte("hello,2\n")); -} - -var port string - -func init() { - flag.StringVar(&port, "port", "8090", "port number, default value is 8090") -} - -func main(){ - flag.Parse() - fmt.Println("Web Server port is:", port) - http.Handle("/hello1",&Controller{}) - http.Handle("/hello2",http.HandlerFunc(hello)) - log.Fatal(http.ListenAndServe(":" + port, nil)) + "github.com/gin-gonic/gin" +) + +func main() { + // Disable console color, you don't need console color when writing the logs to file. + gin.DisableConsoleColor() + + // Use a file for logging + f, _ := os.Create("/root/gin.log") + gin.DefaultWriter = io.MultiWriter(f, os.Stdout) + + r := gin.Default() + r.GET("/ping", func(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "pong", + }) + }) + // Listen and serve on 0.0.0.0:8090 + r.Run(":8090") }