-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api,pkg): create generic server package for services
- Loading branch information
1 parent
3800fc0
commit 0df2782
Showing
7 changed files
with
214 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
package routes | ||
|
||
import ( | ||
svc "github.com/shellhub-io/shellhub/api/services" | ||
"github.com/shellhub-io/shellhub/api/services" | ||
) | ||
|
||
type Handler struct { | ||
service svc.Service | ||
service services.Service | ||
} | ||
|
||
func NewHandler(s svc.Service) *Handler { | ||
func (h *Handler) GetService() any { | ||
return h.service | ||
} | ||
|
||
func NewHandler(s services.Service) *Handler { | ||
return &Handler{service: s} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package server | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/getsentry/sentry-go" | ||
"github.com/labstack/echo/v4" | ||
"github.com/shellhub-io/shellhub/api/pkg/echo/handlers" | ||
) | ||
|
||
// Echo is a wrapper around the Echo HTTP server with simplified lifecycle management. | ||
type Echo struct { | ||
echo *echo.Echo | ||
} | ||
|
||
var _ Server[*echo.Echo] = new(Echo) | ||
|
||
// Underlying returns the underlying HTTP server. | ||
func (s *Echo) Underlying() *echo.Echo { | ||
return s.echo | ||
} | ||
|
||
// Close closes the server. | ||
func (s *Echo) Close() error { | ||
return s.echo.Close() | ||
} | ||
|
||
// Start starts the server at a given address. | ||
func (s *Echo) Start(addr string) error { | ||
return s.echo.Start(addr) | ||
} | ||
|
||
// Listen starts the HTTP server, listing for connections in [ServerListenDefaultAddress]. | ||
func (s *Echo) Listen() error { | ||
return s.echo.Start(ServerListenDefaultAddress) | ||
} | ||
|
||
// SentryOption enables, if DSN is a valid value, the error reporter for a Sentry's server. | ||
var SentryOption = func(dsn string) func(server *echo.Echo) { | ||
return func(server *echo.Echo) { | ||
if dsn != "" { | ||
reporter, err := sentry.NewClient(sentry.ClientOptions{ //nolint:exhaustruct | ||
Dsn: dsn, | ||
Release: os.Getenv("SHELLHUB_VERSION"), | ||
EnableTracing: true, | ||
TracesSampleRate: 1, | ||
}) | ||
if err != nil { | ||
server.HTTPErrorHandler = handlers.NewErrors(nil) | ||
|
||
return | ||
} | ||
|
||
server.HTTPErrorHandler = handlers.NewErrors(reporter) | ||
} | ||
} | ||
} |
Oops, something went wrong.