diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6d439cd7f..1dd3abb53 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,4 +15,4 @@ jobs: go-version: 1.13 - name: build - run: make build + run: make build-cli diff --git a/.gitignore b/.gitignore index 6d24bcac9..9923d7f9f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ secret/* .envrc db/*/data/* .env.* -serviceAccount.json \ No newline at end of file +serviceAccount.json +rapid +helper \ No newline at end of file diff --git a/.realize.yaml b/.realize.yaml index 4cbcc1537..4151dc588 100755 --- a/.realize.yaml +++ b/.realize.yaml @@ -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 @@ -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 @@ -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 diff --git a/Makefile b/Makefile index d105e4155..1a7c8b0b8 100644 --- a/Makefile +++ b/Makefile @@ -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` diff --git a/README.md b/README.md index 773d9ae10..0ce1092f7 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,6 @@ the boilerplate for monorepo application (support only http protocol) ### server starting -- local - -```bash -> realize start -``` - - docker ```bash diff --git a/cmd/rapid/default-grpc/cmd.go b/cmd/rapid/default-grpc/cmd.go new file mode 100644 index 000000000..55dc2de70 --- /dev/null +++ b/cmd/rapid/default-grpc/cmd.go @@ -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 +} diff --git a/cmd/default-grpc/dependency.go b/cmd/rapid/default-grpc/dependency.go similarity index 99% rename from cmd/default-grpc/dependency.go rename to cmd/rapid/default-grpc/dependency.go index c30f6d3bb..c864e3d60 100644 --- a/cmd/default-grpc/dependency.go +++ b/cmd/rapid/default-grpc/dependency.go @@ -1,4 +1,4 @@ -package main +package defaultgrpc import ( "fmt" diff --git a/cmd/default-grpc/env.go b/cmd/rapid/default-grpc/env.go similarity index 96% rename from cmd/default-grpc/env.go rename to cmd/rapid/default-grpc/env.go index b285527e9..5af9400cb 100644 --- a/cmd/default-grpc/env.go +++ b/cmd/rapid/default-grpc/env.go @@ -1,4 +1,4 @@ -package main +package defaultgrpc type environment struct { Port string `env:"DEFAULT_GRPC_PORT,required"` diff --git a/cmd/default-grpc/main.go b/cmd/rapid/default-grpc/run.go similarity index 97% rename from cmd/default-grpc/main.go rename to cmd/rapid/default-grpc/run.go index 10bc5ef90..4cd7b112f 100644 --- a/cmd/default-grpc/main.go +++ b/cmd/rapid/default-grpc/run.go @@ -1,4 +1,4 @@ -package main +package defaultgrpc import ( "context" @@ -13,7 +13,7 @@ import ( "go.uber.org/zap" ) -func main() { +func run() { // initilize environment variables e := &environment{} diff --git a/cmd/rapid/default-http/cmd.go b/cmd/rapid/default-http/cmd.go new file mode 100644 index 000000000..d0437c668 --- /dev/null +++ b/cmd/rapid/default-http/cmd.go @@ -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 +} diff --git a/cmd/default/dependency.go b/cmd/rapid/default-http/dependency.go similarity index 98% rename from cmd/default/dependency.go rename to cmd/rapid/default-http/dependency.go index 94d2193fe..23ed54781 100644 --- a/cmd/default/dependency.go +++ b/cmd/rapid/default-http/dependency.go @@ -1,4 +1,4 @@ -package main +package defaulthttp import ( "github.com/abyssparanoia/rapid-go/internal/default/handler/api" diff --git a/cmd/default/env.go b/cmd/rapid/default-http/env.go similarity index 96% rename from cmd/default/env.go rename to cmd/rapid/default-http/env.go index 428c386ec..335b7d531 100644 --- a/cmd/default/env.go +++ b/cmd/rapid/default-http/env.go @@ -1,4 +1,4 @@ -package main +package defaulthttp type environment struct { Port string `env:"DEFAULT_PORT,required"` diff --git a/cmd/default/routing.go b/cmd/rapid/default-http/routing.go similarity index 97% rename from cmd/default/routing.go rename to cmd/rapid/default-http/routing.go index efcdd6687..cc9f16de7 100644 --- a/cmd/default/routing.go +++ b/cmd/rapid/default-http/routing.go @@ -1,4 +1,4 @@ -package main +package defaulthttp import ( "net/http" diff --git a/cmd/default/main.go b/cmd/rapid/default-http/run.go similarity index 70% rename from cmd/default/main.go rename to cmd/rapid/default-http/run.go index 6398edbdf..37d00b1f7 100644 --- a/cmd/default/main.go +++ b/cmd/rapid/default-http/run.go @@ -1,4 +1,4 @@ -package main +package defaulthttp import ( "context" @@ -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 { @@ -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") } diff --git a/cmd/rapid/main.go b/cmd/rapid/main.go new file mode 100644 index 000000000..f7d07b015 --- /dev/null +++ b/cmd/rapid/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + execute() +} diff --git a/cmd/rapid/push-notification/cmd.go b/cmd/rapid/push-notification/cmd.go new file mode 100644 index 000000000..d3fc5f075 --- /dev/null +++ b/cmd/rapid/push-notification/cmd.go @@ -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 +} diff --git a/cmd/push-notification/dependency.go b/cmd/rapid/push-notification/dependency.go similarity index 89% rename from cmd/push-notification/dependency.go rename to cmd/rapid/push-notification/dependency.go index fa7660299..8acad941b 100644 --- a/cmd/push-notification/dependency.go +++ b/cmd/rapid/push-notification/dependency.go @@ -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" @@ -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 @@ -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) diff --git a/cmd/push-notification/env.go b/cmd/rapid/push-notification/env.go similarity index 95% rename from cmd/push-notification/env.go rename to cmd/rapid/push-notification/env.go index 139d94915..f450223c0 100644 --- a/cmd/push-notification/env.go +++ b/cmd/rapid/push-notification/env.go @@ -1,4 +1,4 @@ -package main +package notification type environment struct { Port string `env:"PUSH_NOTIFICATION_PORT,required"` diff --git a/cmd/push-notification/routing.go b/cmd/rapid/push-notification/routing.go similarity index 97% rename from cmd/push-notification/routing.go rename to cmd/rapid/push-notification/routing.go index a30d7e78e..f5ad145ba 100644 --- a/cmd/push-notification/routing.go +++ b/cmd/rapid/push-notification/routing.go @@ -1,4 +1,4 @@ -package main +package notification import ( "net/http" diff --git a/cmd/push-notification/main.go b/cmd/rapid/push-notification/run.go similarity index 70% rename from cmd/push-notification/main.go rename to cmd/rapid/push-notification/run.go index 6398edbdf..f79edaa57 100644 --- a/cmd/push-notification/main.go +++ b/cmd/rapid/push-notification/run.go @@ -1,4 +1,4 @@ -package main +package notification import ( "context" @@ -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 { @@ -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") } diff --git a/cmd/rapid/root.go b/cmd/rapid/root.go new file mode 100644 index 000000000..687610407 --- /dev/null +++ b/cmd/rapid/root.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "os" + + notification "github.com/abyssparanoia/rapid-go/cmd/rapid/push-notification" + + defaulthttp "github.com/abyssparanoia/rapid-go/cmd/rapid/default-http" + + defaultgrpc "github.com/abyssparanoia/rapid-go/cmd/rapid/default-grpc" + + "github.com/spf13/cobra" +) + +func newCmdRoot() *cobra.Command { + cmd := &cobra.Command{ + Use: "rapid", + Short: "cli tool for rapid-go", + Long: "cli tool for rapid-go", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + cmd.HelpFunc()(cmd, args) + } + }, + } + cmd.AddCommand(defaultgrpc.NewDefaultGRPCCmd()) + cmd.AddCommand(defaulthttp.NewDefaultHTTPCmd()) + cmd.AddCommand(notification.NewPushNotificationCmd()) + return cmd +} + +func execute() { + cmd := newCmdRoot() + if err := cmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/docker/production/default/Dockerfile b/docker/production/default/Dockerfile index 8c2d69ede..e9ab54ac1 100644 --- a/docker/production/default/Dockerfile +++ b/docker/production/default/Dockerfile @@ -8,19 +8,17 @@ ENV CGO_ENABLED=0 COPY . . -RUN go install -v -tags netgo -ldflags '-extldflags "-static"' ./cmd/default/ +RUN go install -v -tags netgo -ldflags '-extldflags "-static"' ./cmd/rapid/ FROM alpine AS server RUN apk add ca-certificates -COPY --from=builder /go/bin/default /bin/server -COPY --from=builder /go/src/github.com/abyssparanoia/rapid-go/.env.default /go/src/github.com/abyssparanoia/rapid-go/.env.default - +COPY --from=builder /go/bin/rapid /bin/rapid WORKDIR /go/src/github.com/abyssparanoia/rapid-go/ ENV PORT 8080 EXPOSE 8080 -ENTRYPOINT ["/bin/server"] \ No newline at end of file +ENTRYPOINT ["rapid","default-http","run"] \ No newline at end of file diff --git a/helper b/helper deleted file mode 100755 index b7a34e7bd..000000000 Binary files a/helper and /dev/null differ