-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce HTTP API with few GET endpoints
- Loading branch information
Showing
7 changed files
with
604 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/hyperbadger/nomad-pipeline/pkg/api" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var serverCmd = &cobra.Command{ | ||
Use: "server", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
_logger, _ := zap.NewProduction() | ||
defer _logger.Sync() | ||
|
||
logger := _logger.Sugar() | ||
|
||
ps, err := api.NewPipelineServer(logger) | ||
if err != nil { | ||
logger.Fatalf("error creating pipeline server: %w", err) | ||
} | ||
|
||
srv := ps.NewHTTPServer(addr) | ||
|
||
if err := srv.ListenAndServe(); err != nil { | ||
logger.Fatalf("server errored: %v", err) | ||
} | ||
}, | ||
} | ||
|
||
var addr string | ||
|
||
func init() { | ||
serverCmd.Flags().StringVar(&addr, "addr", "127.0.0.1:4656", "address server will listen on") | ||
|
||
rootCmd.AddCommand(serverCmd) | ||
} |
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,72 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Error struct { | ||
Code int `json:"code"` | ||
Status string `json:"status"` | ||
Type string `json:"type"` | ||
Message string `json:"message"` | ||
Details string `json:"details"` | ||
} | ||
|
||
const ( | ||
ErrorTypeNomadUpstream = "nomad_upstream" | ||
) | ||
|
||
type ErrorOption func(*Error) | ||
|
||
func NewError(opts ...ErrorOption) *Error { | ||
err := &Error{ | ||
Code: http.StatusInternalServerError, | ||
Status: http.StatusText(http.StatusInternalServerError), | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (err *Error) Apply(c *gin.Context, logger *zap.SugaredLogger) { | ||
c.JSON(err.Code, gin.H{"error": err}) | ||
|
||
logger.Errorw( | ||
err.Message, | ||
"code", err.Code, | ||
"status", err.Status, | ||
"type", err.Type, | ||
"details", err.Details, | ||
) | ||
} | ||
|
||
func WithCode(code int) ErrorOption { | ||
return func(err *Error) { | ||
err.Code = code | ||
err.Status = http.StatusText(code) | ||
} | ||
} | ||
|
||
func WithType(errType string) ErrorOption { | ||
return func(err *Error) { | ||
err.Type = errType | ||
} | ||
} | ||
|
||
func WithMessage(msg string) ErrorOption { | ||
return func(err *Error) { | ||
err.Message = msg | ||
} | ||
} | ||
|
||
func WithError(err error) ErrorOption { | ||
return func(_err *Error) { | ||
_err.Details = err.Error() | ||
} | ||
} |
Oops, something went wrong.