Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented rapid cli #138

Merged
merged 15 commits into from
May 17, 2020
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
go-version: 1.13

- name: build
run: make build
run: make build-cli
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ secret/*
.envrc
db/*/data/*
.env.*
serviceAccount.json
serviceAccount.json
rapid
helper
21 changes: 15 additions & 6 deletions .realize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ schema:
commands:
install:
status: true
method: go build -o ./cmd/default/default-server ./cmd/default/
method: make build-cli
run:
status: true
method: ./cmd/default/default-server
method: ./rapid
fmt:
status: true
args:
- default-http
- run
watcher:
extensions:
- go
Expand All @@ -29,12 +32,15 @@ schema:
commands:
install:
status: true
method: go build -o ./cmd/default-grpc/default-gprc-server ./cmd/default-grpc/
method: make build-cli
run:
status: true
method: ./cmd/default-grpc/default-gprc-server
method: ./rapid
fmt:
status: true
args:
- default-grpc
- run
watcher:
extensions:
- go
Expand All @@ -50,12 +56,15 @@ schema:
commands:
install:
status: true
method: go build -o ./cmd/push-notification/push-notification-server ./cmd/push-notification/
method: make build-cli
run:
status: true
method: ./cmd/push-notification/push-notification-server
method: ./rapid
fmt:
status: true
args:
- push-notification
- run
watcher:
extensions:
- go
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ init:
format:
$(call format)

build:
go build -o default-server ./cmd/default

build-helper:
go build -o helper ./cmd/helper

build-cli:
go build -o ./rapid ./cmd/rapid

test:
go test `go list ./... | grep -v internal/dbmodels`

Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ the boilerplate for monorepo application (support only http protocol)

### server starting

- local

```bash
> realize start
```

- docker

```bash
Expand Down
31 changes: 31 additions & 0 deletions cmd/rapid/default-grpc/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package defaultgrpc

import (
"github.com/spf13/cobra"
)

// NewDefaultGRPCCmd ... new default GRPC cmd
func NewDefaultGRPCCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "default-grpc",
Short: "cli default grpc server",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
}
},
}
cmd.AddCommand(newDefaultGRPCRunCmd())
return cmd
}

func newDefaultGRPCRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "running default grpc server",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
return cmd
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaultgrpc

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/default-grpc/env.go → cmd/rapid/default-grpc/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaultgrpc

type environment struct {
Port string `env:"DEFAULT_GRPC_PORT,required"`
Expand Down
4 changes: 2 additions & 2 deletions cmd/default-grpc/main.go → cmd/rapid/default-grpc/run.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaultgrpc

import (
"context"
Expand All @@ -13,7 +13,7 @@ import (
"go.uber.org/zap"
)

func main() {
func run() {

// initilize environment variables
e := &environment{}
Expand Down
31 changes: 31 additions & 0 deletions cmd/rapid/default-http/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package defaulthttp

import (
"github.com/spf13/cobra"
)

// NewDefaultHTTPCmd ... new default HTTP cmd
func NewDefaultHTTPCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "default-http",
Short: "cli default http server",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
}
},
}
cmd.AddCommand(newDefaultHTTPRunCmd())
return cmd
}

func newDefaultHTTPRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "running default http server",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
return cmd
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaulthttp

import (
"github.com/abyssparanoia/rapid-go/internal/default/handler/api"
Expand Down
2 changes: 1 addition & 1 deletion cmd/default/env.go → cmd/rapid/default-http/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaulthttp

type environment struct {
Port string `env:"DEFAULT_PORT,required"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaulthttp

import (
"net/http"
Expand Down
16 changes: 8 additions & 8 deletions cmd/default/main.go → cmd/rapid/default-http/run.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package defaulthttp

import (
"context"
Expand All @@ -10,12 +10,13 @@ import (
"time"

"github.com/abyssparanoia/rapid-go/internal/pkg/log"
"go.uber.org/zap"

"github.com/caarlos0/env/v6"
"github.com/go-chi/chi"
)

func main() {
func run() {

e := &environment{}
if err := env.Parse(e); err != nil {
Expand Down Expand Up @@ -44,26 +45,25 @@ func main() {
}

// Run
logger.Sugar().Debugf("[START] server. port: %s\n", addr)
logger.Info(fmt.Sprintf("[START] server. port: %s\n", addr))
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.Sugar().Debugf("[CLOSED] server closed with error: %s\n", err)
logger.Error("[CLOSED] server closed with error", zap.Error(err))
}
}()

// graceful shuttdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
logger.Sugar().Debugf("SIGNAL %d received, so server shutting down now...\n", <-quit)
logger.Info(fmt.Sprintf("SIGNAL %d received, so server shutting down now...\n", <-quit))

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err = server.Shutdown(ctx)
if err != nil {
logger.Sugar().Debugf("failed to gracefully shutdown: %s\n", err)
logger.Error("failed to gracefully shutdown", zap.Error(err))
}

logger.Sugar().Debugf("server shutdown completed\n")

logger.Info("server shutdown completed")
}
5 changes: 5 additions & 0 deletions cmd/rapid/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
execute()
}
31 changes: 31 additions & 0 deletions cmd/rapid/push-notification/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package notification

import (
"github.com/spf13/cobra"
)

// NewPushNotificationCmd ... new notification server cmd
func NewPushNotificationCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "push-notification",
Short: "cli default push notification server",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
}
},
}
cmd.AddCommand(newPushNotificationRunCmd())
return cmd
}

func newPushNotificationRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "running push notification server",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
return cmd
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main
package notification

import (
"github.com/abyssparanoia/rapid-go/internal/pkg/gluefcm"
"github.com/abyssparanoia/rapid-go/internal/pkg/gluefirestore"
"github.com/abyssparanoia/rapid-go/internal/pkg/httpheader"
"github.com/abyssparanoia/rapid-go/internal/pkg/httpmiddleware"
"github.com/abyssparanoia/rapid-go/internal/pkg/middleware/requestlog"
"github.com/abyssparanoia/rapid-go/internal/push-notification/domain/service"
"github.com/abyssparanoia/rapid-go/internal/push-notification/handler/api"
"github.com/abyssparanoia/rapid-go/internal/push-notification/infrastructure/repository"
Expand All @@ -14,7 +14,7 @@ import (

// Dependency ... dependency
type Dependency struct {
httpMiddleware *httpmiddleware.HTTPMiddleware
httpMiddleware *requestlog.HTTPMiddleware
DummyHTTPHeader *httpheader.Middleware
HTTPHeader *httpheader.Middleware
TokenHandler *api.TokenHandler
Expand All @@ -38,7 +38,7 @@ func (d *Dependency) Inject(e *environment, logger *zap.Logger) {
dhh := httpheader.NewDummy()
hh := httpheader.New()

d.httpMiddleware = httpmiddleware.New(logger)
d.httpMiddleware = requestlog.New(logger)

d.DummyHTTPHeader = httpheader.NewMiddleware(dhh)
d.HTTPHeader = httpheader.NewMiddleware(hh)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package notification

type environment struct {
Port string `env:"PUSH_NOTIFICATION_PORT,required"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package notification

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package notification

import (
"context"
Expand All @@ -10,12 +10,13 @@ import (
"time"

"github.com/abyssparanoia/rapid-go/internal/pkg/log"
"go.uber.org/zap"

"github.com/caarlos0/env/v6"
"github.com/go-chi/chi"
)

func main() {
func run() {

e := &environment{}
if err := env.Parse(e); err != nil {
Expand Down Expand Up @@ -44,26 +45,25 @@ func main() {
}

// Run
logger.Sugar().Debugf("[START] server. port: %s\n", addr)
logger.Info(fmt.Sprintf("[START] server. port: %s\n", addr))
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.Sugar().Debugf("[CLOSED] server closed with error: %s\n", err)
logger.Error("[CLOSED] server closed with error", zap.Error(err))
}
}()

// graceful shuttdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
logger.Sugar().Debugf("SIGNAL %d received, so server shutting down now...\n", <-quit)
logger.Info(fmt.Sprintf("SIGNAL %d received, so server shutting down now...\n", <-quit))

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err = server.Shutdown(ctx)
if err != nil {
logger.Sugar().Debugf("failed to gracefully shutdown: %s\n", err)
logger.Error("failed to gracefully shutdown", zap.Error(err))
}

logger.Sugar().Debugf("server shutdown completed\n")

logger.Info("server shutdown completed")
}
Loading