Skip to content

Commit

Permalink
Merge pull request #346 from bavix/rest-only
Browse files Browse the repository at this point in the history
[3.x] feat: add rest cli
  • Loading branch information
rez1dent3 authored Jul 11, 2024
2 parents 70de1c2 + b5f7143 commit 23274b1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ issues:
exclude-dirs:
- example
exclude-rules:
- path: cmd/*
linters:
- gochecknoglobals
- gochecknoinits
- path: (.+)_test.go
linters:
- dupl
2 changes: 0 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:gochecknoglobals
package cmd

import (
Expand Down Expand Up @@ -53,7 +52,6 @@ var checkCmd = &cobra.Command{
},
}

//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(checkCmd)

Expand Down
61 changes: 61 additions & 0 deletions cmd/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"context"
"errors"
"net/http"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/bavix/gripmock/internal/deps"
)

// restCmd represents the rest command.
var restCmd = &cobra.Command{
Use: "rest",
Short: "Start only the rest service",
RunE: func(cmd *cobra.Command, _ []string) error {
builder := deps.NewBuilder(deps.WithDefaultConfig())
ctx, cancel := builder.SignalNotify(cmd.Context())
defer cancel()

ctx = builder.Logger(ctx)

return restServe(ctx, builder)
},
}

func restServe(ctx context.Context, builder *deps.Builder) error {
srv, err := builder.RestServe(ctx, stubFlag)
if err != nil {
return err
}

ch := make(chan error)
defer close(ch)

go func() {
zerolog.Ctx(ctx).
Info().
Str("addr", builder.Config().HTTPAddr).
Msg("Serving stub-manager")

ch <- srv.ListenAndServe()
}()

select {
case err = <-ch:
if errors.Is(err, http.ErrServerClosed) {
return nil
}

return err
case <-ctx.Done():
return ctx.Err()
}
}

func init() {
rootCmd.AddCommand(restCmd)
}
34 changes: 0 additions & 34 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//nolint:gochecknoglobals
package cmd

import (
"context"
"errors"
"net/http"
"os"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -45,37 +42,6 @@ var rootCmd = &cobra.Command{
},
}

func restServe(ctx context.Context, builder *deps.Builder) error {
srv, err := builder.RestServe(ctx, stubFlag)
if err != nil {
return err
}

ch := make(chan error)
defer close(ch)

go func() {
zerolog.Ctx(ctx).
Info().
Str("addr", builder.Config().HTTPAddr).
Msg("Serving stub-manager")

ch <- srv.ListenAndServe()
}()

select {
case err = <-ch:
if errors.Is(err, http.ErrServerClosed) {
return nil
}

return err
case <-ctx.Done():
return ctx.Err()
}
}

//nolint:gochecknoinits
func init() {
rootCmd.Flags().StringVarP(
&outputFlag,
Expand Down

0 comments on commit 23274b1

Please sign in to comment.