Polish the Golang demo with Gin web framework

This commit is contained in:
LI Qing 2020-05-28 09:34:37 +00:00
parent c1911e6585
commit c27a94194f
3 changed files with 35 additions and 33 deletions

@ -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"}`.

@ -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

@ -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")
}
"github.com/gin-gonic/gin"
)
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))
// 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")
}