go 웹 서버

  1. go 모듈 생성하기

    mkdir server && cd server
    go mod init main
    
  2. gin-gonic web framework 설치하기

    go get -u github.com/gin-gonic/gin
    
  3. 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")
    }
    

gcp scp 명령어로 파일 이동시키기

https://cloud.google.com/sdk/gcloud/reference/compute/scp

gcloud compute scp server  vm-share-server-01:/home/gcptae2089 --zone=us-central1-c

인스턴스 템플릿 만들때 자동화 스크립트 입력하기

  1. 인스턴스 템플릿 자동화 스크립트에 추가

    nohup /home/gcptae2089/server 1>/dev/null 2>&1 &