일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- nohup
- ssh
- PYTHON
- nmap
- openpyxl
- ubuntu
- postgresql
- mysql
- nGrinder
- ftp
- port forwarding
- insert
- SWIFT
- Jupyter
- STF_PortForwarding
- 28015
- 실행권한
- GoCD
- appium server
- appium
- perfect
- centos
- STF
- kitura
- Jupyter Notebook
- Materials
- sshpass
- create table
- postgres
- rethinkdb
- Today
- Total
don't stop believing
Gin 설치와 기본 example 확인해 보기 본문
※ 참고
Gin example 포스트를 작성하면서 govendor를 사용했었습니다.
하지만 govendor 작성자도 더 이상 사용하지 말라고 합니다. go mod가 더 좋다고 합니다.
Gin Tutorial은 아래 url을 참고해 주세요.
https://blog.logrocket.com/how-to-build-a-rest-api-with-golang-using-gin-and-gorm/
Golang으로 Web Server 만드는 걸 배우다 혼란에 빠졌습니다.
Java의 Spring이나 Python의 Django, Flask, 그리고 Swift에도 Vaper, Kitura, Perfect같은 Framework이 있는데 Go에서 쌩으로 다 하려니 배울것도 많고 힘이듭니다. 그래서 전에 Swift를 배울때 처럼 첫 시작을 Framework 하나를 잡아 공부하는 방향으로 결정했습니다.
그런데 아직 Go에서는 주력 Framework이 보이지 않는것 같습니다.
아래 링크는 Go Framework의 github star와 Fork 된 수로 랭킹을 만든 페이지입니다.
https://github.com/mingrammer/go-web-framework-stars
저는 우선 초짜이니 가장 위에 링크된 것부터 배워보기로 했습니다.
아마 gin과 beego에 집중하지 않을까 싶습니다.
먼저 Gin을 보겠습니다.
https://github.com/gin-gonic/gin
튜토리얼이나 설명이 잘 돼있는 것 같습니다.
바로 설치 들어갑니다.
먼저 GOPATH 경로로 이동합니다. 그리고 github에서 gin을 받습니다.
12$ cd $GOPATH$ go get -u github.com/gin-gonic/gin
그리고 go package를 관리해 주는 vendor tool도 설치합니다. gin에서는 Govendor로 설명하고 있네요.
https://github.com/govend/govend
1$ go get github.com/kardianos/govendor
이제 프로젝트 폴더를 생성하고 안으로 이동합니다.
저는 $GOPATH/src/github.com/dejavuwing/ginbasic에 만들었습니다.
1$ mkdir -p $GOPATH/src/github.com/dejavuwing/ginbasic && cd "$_"
프로젝트 폴더 안에서 govendor를 초기화하고 gin 1.3 버전을 받습니다.
12$ govendor init$ govendor fetch github.com/gin-gonic/gin@v1.3
github에서 gin의 기본 exsample을 받습니다.
1$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
curl로 main.go를 다운받았다면 소스도 한번 봐볼까요?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package mainimport ("net/http""github.com/gin-gonic/gin")var db = make(map[string]string)func setupRouter() *gin.Engine {// Disable Console Color// gin.DisableConsoleColor()r := gin.Default()// Ping testr.GET("/ping", func(c *gin.Context) {c.String(http.StatusOK, "pong")})// Get user valuer.GET("/user/:name", func(c *gin.Context) {user := c.Params.ByName("name")value, ok := db[user]if ok {c.JSON(http.StatusOK, gin.H{"user": user, "value": value})} else {c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})}})// Authorized group (uses gin.BasicAuth() middleware)// Same than:// authorized := r.Group("/")// authorized.Use(gin.BasicAuth(gin.Credentials{// "foo": "bar",// "manu": "123",//}))authorized := r.Group("/", gin.BasicAuth(gin.Accounts{"foo": "bar", // user:foo password:bar"manu": "123", // user:manu password:123}))authorized.POST("admin", func(c *gin.Context) {user := c.MustGet(gin.AuthUserKey).(string)// Parse JSONvar json struct {Value string `json:"value" binding:"required"`}if c.Bind(&json) == nil {db[user] = json.Valuec.JSON(http.StatusOK, gin.H{"status": "ok"})}})return r}func main() {r := setupRouter()// Listen and Server in 0.0.0.0:8080r.Run(":8080")}
go run으로 바로 실행해 봅니다.
12345678910111213$ go run main.go[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.- using env: export GIN_MODE=release- using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /ping --> main.setupRouter.func1 (3 handlers)[GIN-debug] GET /user/:name --> main.setupRouter.func2 (3 handlers)[GIN-debug] POST /admin --> main.setupRouter.func3 (4 handlers)[GIN-debug] Listening and serving HTTP on :8080
우선 하나만 해보겠습니다. ping 갑니다.
다른 경로들은 인증과 관련된 것입니다.
이건 다른 sample에서 다루도록 하겠습니다.
여기까지 gin 설치와 기본 example 확인이었습니다.
'Golang > Gin' 카테고리의 다른 글
Another example: query + post form (0) | 2019.02.15 |
---|---|
Multipart/Urlencoded Form (1) | 2019.02.14 |
Querystring parameters (0) | 2019.02.14 |
Parameters in path (0) | 2019.02.13 |
Using GET, POST, PUT, PATCH, DELETE and OPTIONS (0) | 2019.02.13 |