|
|
@ -1,25 +1,43 @@
|
|
|
|
# GO GRPC BASIC
|
|
|
|
# GO GRPC BASIC
|
|
|
|
|
|
|
|
|
|
|
|
## Requirements
|
|
|
|
## Requirements
|
|
|
|
check other branch to step by step create project
|
|
|
|
### `Check other branch to step by step create project from 1/dev, 2/grpc-gateway, etc`
|
|
|
|
|
|
|
|
|
|
|
|
### Validate Go Installation
|
|
|
|
### Validate Go Installation
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
$ go version
|
|
|
|
$ go version
|
|
|
|
go version go1.18.3 linux/amd64
|
|
|
|
go version go1.18.3 linux/amd64
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Add GOPATH
|
|
|
|
|
|
|
|
add/edit to your ~/.bashrc or ~/.zshrc file
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
export GOPATH=/home/{your-user?}/go
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dont forget to execute,
|
|
|
|
|
|
|
|
to reload bash
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
source ~/.bashrc
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
to reload zsh
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
source ~/.zshrc
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Validate GOPATH
|
|
|
|
### Validate GOPATH
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
echo $GOPATH
|
|
|
|
$ echo $GOPATH
|
|
|
|
|
|
|
|
/home/aji/go
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Start Project in Go
|
|
|
|
### Start Project in Go
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
cd $GOPATH/src
|
|
|
|
$ cd $GOPATH/src
|
|
|
|
mkdir -p github.com/ajikamaludin/go-grpc_basic
|
|
|
|
$ mkdir -p github.com/ajikamaludin/go-grpc_basic
|
|
|
|
cd github.com/ajikamaludin/go-grpc_basic
|
|
|
|
$ cd github.com/ajikamaludin/go-grpc_basic
|
|
|
|
go mod init github.com/ajikamaludin/go-grpc_basic
|
|
|
|
$ go mod init github.com/ajikamaludin/go-grpc_basic
|
|
|
|
go mod tidy
|
|
|
|
$ go mod tidy
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Install Protoc
|
|
|
|
### Install Protoc
|
|
|
@ -48,14 +66,14 @@ go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@latest
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Add PATH
|
|
|
|
### Add PATH
|
|
|
|
add to your ~/.bashrc or ~/.zshrc file
|
|
|
|
add/edit to your ~/.bashrc or ~/.zshrc file
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
export PATH="$PATH:$GOPATH/bin"
|
|
|
|
export PATH="$PATH:$GOPATH/bin"
|
|
|
|
```
|
|
|
|
```
|
|
|
|
### Validate Protoc Dependency Golang Installation
|
|
|
|
### Validate Protoc Dependency Golang Installation
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
~ ❯ ls $GOPATH/bin
|
|
|
|
~ ❯ ls $GOPATH/bin
|
|
|
|
dlv gopls protoc-gen-go protoc-gen-grpc-gateway revel
|
|
|
|
dlv gopls protoc-gen-go protoc-gen-grpc-gateway
|
|
|
|
go-outline grpcurl protoc-gen-go-grpc protoc-gen-swagger
|
|
|
|
go-outline grpcurl protoc-gen-go-grpc protoc-gen-swagger
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -63,6 +81,7 @@ go-outline grpcurl protoc-gen-go-grpc protoc-gen-swagger
|
|
|
|
### Setup Project
|
|
|
|
### Setup Project
|
|
|
|
- create `proto` dir
|
|
|
|
- create `proto` dir
|
|
|
|
- create versioning dir and service dir `health`
|
|
|
|
- create versioning dir and service dir `health`
|
|
|
|
|
|
|
|
- exec `get-lib.sh` in `proto` dir to download / get important library to use by `health.proto` usage is in compile proto file
|
|
|
|
- create proto file `health.proto`
|
|
|
|
- create proto file `health.proto`
|
|
|
|
- compile / generate proto with `compile-proto.sh` in proto dir
|
|
|
|
- compile / generate proto with `compile-proto.sh` in proto dir
|
|
|
|
|
|
|
|
|
|
|
@ -115,7 +134,7 @@ service HealthService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
- re - compile / re - generate proto with `compile-proto.sh` in proto dir
|
|
|
|
- re - compile / re - generate proto with execute `compile-proto.sh` in proto dir
|
|
|
|
- `go mod tidy`
|
|
|
|
- `go mod tidy`
|
|
|
|
- `go get "github.com/gorilla/handlers"` TODO: what is for ?
|
|
|
|
- `go get "github.com/gorilla/handlers"` TODO: what is for ?
|
|
|
|
- create `http.go` in router dir and implement NewHTTPServer and register health api service
|
|
|
|
- create `http.go` in router dir and implement NewHTTPServer and register health api service
|
|
|
@ -206,11 +225,28 @@ runtime.HTTPError = errors.CustomHTTPError
|
|
|
|
|
|
|
|
|
|
|
|
### Jwt ( Json Web Tokens ) Auth
|
|
|
|
### Jwt ( Json Web Tokens ) Auth
|
|
|
|
- add jwt configs to `config.yaml`, jwt: issuer: ajikamaludin, key: P@ssw0rd, type: Bearer
|
|
|
|
- add jwt configs to `config.yaml`, jwt: issuer: ajikamaludin, key: P@ssw0rd, type: Bearer
|
|
|
|
- changes `pkg/v1/config/config.go` add new config struct and validate config, [UPDATE] change New to GetInstance for singleton config
|
|
|
|
- changes `pkg/v1/config/config.go` add new config struct and validate config, **[UPDATE]** change New to GetInstance for singleton config
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
|
|
|
var lock = &sync.Mutex{}
|
|
|
|
|
|
|
|
var config *Config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func GetInstance() (*Config, error) {
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
|
|
|
|
lock.Lock()
|
|
|
|
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
config, err := New()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
- changes `pkg/v1/utils/constants/constants.go` to add new const for jwt token expired and refresh token expired
|
|
|
|
- changes `pkg/v1/utils/constants/constants.go` to add new const for jwt token expired and refresh token expired
|
|
|
|
- `go get github.com/dgrijalva/jwt-go`, lib to handle jwt token in go
|
|
|
|
- `go get github.com/dgrijalva/jwt-go`, lib to handle jwt token in go
|
|
|
|
- create new pkg `pkg/v1/jwt/jwt.go`, implement Generate token and Claim token
|
|
|
|
- create new pkg `pkg/v1/jwt/jwt.go`, implement Generate token and Claim token
|
|
|
|
- create new proto `proto/v1/auth/auth.proto` auth for login and register service, recompile `sh compile-proto.sh` [UPDATE]
|
|
|
|
- create new proto `proto/v1/auth/auth.proto` auth for login and register service, recompile `sh compile-proto.sh` **[UPDATE]**
|
|
|
|
- implement `api/v1/auth/auth.go`, `api/v1/auth/login.go` and `api/v1/auth/register.go`
|
|
|
|
- implement `api/v1/auth/auth.go`, `api/v1/auth/login.go` and `api/v1/auth/register.go`
|
|
|
|
- register new grpc service to grpc server
|
|
|
|
- register new grpc service to grpc server
|
|
|
|
```go
|
|
|
|
```go
|
|
|
|