Compare commits

...

12 Commits
7/jwt ... main

@ -1,25 +1,43 @@
# GO GRPC BASIC
## 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
```bash
$ go version
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
```bash
echo $GOPATH
$ echo $GOPATH
/home/aji/go
```
### Start Project in Go
```bash
cd $GOPATH/src
mkdir -p github.com/ajikamaludin/go-grpc_basic
cd github.com/ajikamaludin/go-grpc_basic
go mod init github.com/ajikamaludin/go-grpc_basic
go mod tidy
$ cd $GOPATH/src
$ mkdir -p github.com/ajikamaludin/go-grpc_basic
$ cd github.com/ajikamaludin/go-grpc_basic
$ go mod init github.com/ajikamaludin/go-grpc_basic
$ go mod tidy
```
### Install Protoc
@ -48,14 +66,14 @@ go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@latest
```
### Add PATH
add to your ~/.bashrc or ~/.zshrc file
add/edit to your ~/.bashrc or ~/.zshrc file
```bash
export PATH="$PATH:$GOPATH/bin"
```
### Validate Protoc Dependency Golang Installation
```bash
~ 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
```
@ -63,6 +81,7 @@ go-outline grpcurl protoc-gen-go-grpc protoc-gen-swagger
### Setup Project
- create `proto` dir
- 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`
- 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 get "github.com/gorilla/handlers"` TODO: what is for ?
- 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
- 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
- `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 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`
- register new grpc service to grpc server
```go

@ -46,7 +46,7 @@ func GetInstance() (*Config, error) {
if config == nil {
lock.Lock()
defer lock.Unlock()
config, err := new()
config, err := New()
if err != nil {
return nil, err
}
@ -55,7 +55,7 @@ func GetInstance() (*Config, error) {
return config, nil
}
func new() (*Config, error) {
func New() (*Config, error) {
cfgPath, err := parseFlag()
if err != nil {
log.Fatal(err)

Loading…
Cancel
Save