go 모듈 생성하기
mkdir server && cd server
go mod init main
gin-gonic web framework 설치하기
go get -u github.com/gin-gonic/gin
main.go에 코드 작성하기
//vim main.go
package main
import (
"net/http"
"os"
"fmt"
"github.com/gin-gonic/gin"
)
func GetHostName(c *gin.Context){
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
hostname = "error"
}
c.JSON(http.StatusOK, gin.H{
"message": hostname,
})
}
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.GET("/test",GetHostName)
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
https://cloud.google.com/sdk/gcloud/reference/compute/scp
gcloud compute scp server vm-share-server-01:/home/gcptae2089 --zone=us-central1-c
인스턴스 템플릿 자동화 스크립트에 추가
nohup /home/gcptae2089/server 1>/dev/null 2>&1 &