Skip to content

Commit

Permalink
기본적인 web server 구축 (#5)
Browse files Browse the repository at this point in the history
* up: 의존성

* add: 테스트로 토큰 검사할 수 있는 컨트롤러

* add: api spec

* up: 로그인 사용자 반환값 변경

* add: 토큰 값을 검사하는 res 추가

* fix: 항상 이미지 pull 하도록 수정 (#4)

* up: 사용하지 않는 의존성을 제거

* add: 프로그램 버전 명 추가

* up: 안내메시지 친화적으로 변경

* up: 리소스 리미트 추가

* docs: 설명 추가

* up: 웹 서버 상태를 알 수 있는 기본 컨트롤러 추가
  • Loading branch information
parkgang committed Jun 6, 2021
1 parent d59feab commit e9125ed
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 17 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# oauth-server

gin을 이용하여 인증서버를 구축합니다.
redis와 의존됩니다.
OAuth 인증서버를 구축합니다.

1. JWT Token을 사용합니다.

# Stack

1. go:1.16.3
1. vscode
1. gin
1. redis
1. docker

# 시퀀스 다이어그램

# History
> 추가 예정
## 프로젝트 생성 방법
# 빠른 시작

1. `go mod init oauth-server`
1. `go get -u github.com/gin-gonic/gin`
1. `.vscode/launch.json``Launch Package` 템플릿으로 디버깅 환경 구축
1. vscode에서 `F5` 혹은 `go run main.go` 으로 시작합니다.
8 changes: 8 additions & 0 deletions api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

@Host = http://localhost:3000

### 로그인
GET {{Host}}/login

### 토큰 검사
GET {{Host}}/token/valid
10 changes: 4 additions & 6 deletions controller/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (

func Login(c *gin.Context) {
var msg struct {
Name string `json:"user"`
Message string
Number int
Id string `json:"id"`
Name string `json:"name"`
}
msg.Name = "Lena"
msg.Message = "hey"
msg.Number = 123
msg.Id = "2d63c985-6745-9d20-7470-fe025dc2a6b0"
msg.Name = "Kyungeun"
c.JSON(http.StatusOK, msg)
}
20 changes: 20 additions & 0 deletions controller/token/valid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package token

import (
"net/http"
"time"

"github.com/gin-gonic/gin"
)

func Valid(c *gin.Context) {
var msg struct {
Id int `json:"id"`
Message string `json:"message"`
DateTime time.Time `json:"dateTime"`
}
msg.Id = 1
msg.Message = "토큰이 유효합니다."
msg.DateTime = time.Now()
c.JSON(http.StatusOK, msg)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module oauth-server
go 1.16

require (
github.com/gin-gonic/gin v1.7.1 // indirect
github.com/gin-gonic/gin v1.7.1
github.com/go-playground/validator/v10 v10.6.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.11 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
Expand All @@ -16,6 +18,7 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand All @@ -32,10 +35,12 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.5 h1:NozRHfUeEta89taVkyfsDVSy2f7v89Frft4pjnWuGuc=
Expand All @@ -62,13 +67,16 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions k8s/prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ spec:
containers:
- name: oauth-server
image: ghcr.io/belf-kr/oauth-server/gin
imagePullPolicy: "Always"
ports:
- containerPort: 3000
resources:
limits:
cpu: 100m
memory: 100Mi
---
apiVersion: v1
kind: Service
Expand Down
5 changes: 5 additions & 0 deletions k8s/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ spec:
containers:
- name: oauth-server
image: ghcr.io/belf-kr/oauth-server/gin
imagePullPolicy: "Always"
ports:
- containerPort: 3000
resources:
limits:
cpu: 100m
memory: 100Mi
---
apiVersion: v1
kind: Service
Expand Down
5 changes: 5 additions & 0 deletions k8s/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ spec:
containers:
- name: oauth-server
image: ghcr.io/belf-kr/oauth-server/gin
imagePullPolicy: "Always"
ports:
- containerPort: 3000
resources:
limits:
cpu: 100m
memory: 100Mi
---
apiVersion: v1
kind: Service
Expand Down
34 changes: 31 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
package main

import (
"fmt"
"net/http"
"oauth-server/controller"
"oauth-server/controller/token"
"os"

"github.com/gin-gonic/gin"
)

const (
name = "oauth-server"
version = "0.1.0"
)

func init() {
fmt.Printf("version: %s\n", version)
}

func main() {
r := gin.Default()

r.GET("/", func(c *gin.Context) {
resData := []byte(name)
c.Data(http.StatusOK, "text/html; charset=utf-8", resData)
})
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
resData := []byte(`OK`)
c.Data(http.StatusOK, "text/html; charset=utf-8", resData)
})
r.GET("/version", func(c *gin.Context) {
resData := []byte(version)
c.Data(http.StatusOK, "text/html; charset=utf-8", resData)
})
r.GET("/env", func(c *gin.Context) {
resData := os.Environ()
c.JSON(http.StatusOK, resData)
})

r.GET("/login", controller.Login)
r.GET("/token/valid", token.Valid)

r.Run(":3000")
}

0 comments on commit e9125ed

Please sign in to comment.