-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(smtpmock): refact server and add auth, create command line client (
- Loading branch information
Showing
27 changed files
with
694 additions
and
213 deletions.
There are no files selected for viewing
Binary file not shown.
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,16 @@ | ||
# build stage | ||
FROM golang:1.13.4 AS builder | ||
ADD . $GOPATH/src/github.com/ovh/cds/tools/smtpmock | ||
WORKDIR $GOPATH/src/github.com/ovh/cds/tools/smtpmock | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/smtpmocksrv github.com/ovh/cds/tools/smtpmock/server | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /tmp/smtpmockcli github.com/ovh/cds/tools/smtpmock/cli | ||
|
||
# final stage | ||
FROM alpine:latest | ||
RUN apk --no-cache add ca-certificates | ||
COPY --from=builder /tmp/smtpmocksrv ./ | ||
COPY --from=builder /tmp/smtpmockcli ./ | ||
RUN chmod +x ./smtpmocksrv | ||
ENTRYPOINT ["./smtpmocksrv"] | ||
CMD ["start"] | ||
EXPOSE 2023 2024 |
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 @@ | ||
smtpmockcli |
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,104 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/ovh/cds/tools/smtpmock" | ||
) | ||
|
||
var client smtpmock.Client | ||
|
||
func main() { | ||
flags := []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "api-url", | ||
Value: "http://localhost:2024", | ||
EnvVars: []string{"SMTPMOCK_API_URL"}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "signin-token", | ||
EnvVars: []string{"SMTPMOCK_SIGNIN_TOKEN"}, | ||
}, | ||
} | ||
|
||
app := &cli.App{ | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "messages", | ||
Usage: "Get messages", | ||
Action: messages, | ||
}, | ||
{ | ||
Name: "recipient-messages", | ||
Usage: "Get messages for a recipient", | ||
Action: recipientMessages, | ||
}, | ||
{ | ||
Name: "recipient-latest-message", | ||
Usage: "Get latest message for a recipient", | ||
Action: recipientLatestMessage, | ||
}, | ||
}, | ||
Flags: flags, | ||
Before: func(ctx *cli.Context) error { | ||
client = smtpmock.NewClient(ctx.String("api-url")) | ||
|
||
token := ctx.String("signin-token") | ||
if token != "" { | ||
if _, err := client.Signin(token); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
fmt.Printf("%v\n", err) | ||
} | ||
} | ||
|
||
func messages(ctx *cli.Context) error { | ||
ms, err := client.GetMessages() | ||
if err != nil { | ||
return err | ||
} | ||
return printJSON(ms) | ||
} | ||
|
||
func recipientMessages(ctx *cli.Context) error { | ||
if ctx.Args().Len() < 1 { | ||
return errors.New("missing recipient email") | ||
} | ||
ms, err := client.GetRecipientMessages(ctx.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
return printJSON(ms) | ||
} | ||
|
||
func recipientLatestMessage(ctx *cli.Context) error { | ||
if ctx.Args().Len() < 1 { | ||
return errors.New("missing recipient email") | ||
} | ||
m, err := client.GetRecipientLatestMessage(ctx.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
return printJSON(m) | ||
} | ||
|
||
func printJSON(i interface{}) error { | ||
buf, err := json.Marshal(i) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
fmt.Println(string(buf)) | ||
return nil | ||
} |
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,97 @@ | ||
package smtpmock | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewClient(url string) Client { | ||
return &client{url: url} | ||
} | ||
|
||
type Client interface { | ||
Signin(token string) (SigninResponse, error) | ||
GetMessages() ([]Message, error) | ||
GetRecipientMessages(email string) ([]Message, error) | ||
GetRecipientLatestMessage(email string) (Message, error) | ||
} | ||
|
||
type client struct { | ||
url string | ||
sessionToken string | ||
} | ||
|
||
func (c *client) requestJSON(method string, url string, body io.Reader, data interface{}) error { | ||
req, err := http.NewRequest(method, url, body) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
if c.sessionToken != "" { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.sessionToken)) | ||
} | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
return errors.New(fmt.Sprintf("error request at %s %s", method, req.URL.String())) | ||
} | ||
|
||
buf, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
return errors.WithStack(json.Unmarshal(buf, data)) | ||
} | ||
|
||
func (c *client) Signin(token string) (SigninResponse, error) { | ||
var res SigninResponse | ||
|
||
buf, err := json.Marshal(SigninRequest{ | ||
SigninToken: token, | ||
}) | ||
if err != nil { | ||
return res, errors.WithStack(err) | ||
} | ||
|
||
if err := c.requestJSON(http.MethodPost, c.url+"/signin", bytes.NewBuffer(buf), &res); err != nil { | ||
return res, err | ||
} | ||
c.sessionToken = res.SessionToken | ||
return res, nil | ||
} | ||
|
||
func (c *client) GetMessages() ([]Message, error) { | ||
var res []Message | ||
if err := c.requestJSON(http.MethodGet, c.url+"/messages", nil, &res); err != nil { | ||
return res, err | ||
} | ||
return res, nil | ||
} | ||
|
||
func (c *client) GetRecipientMessages(email string) ([]Message, error) { | ||
var res []Message | ||
if err := c.requestJSON(http.MethodGet, fmt.Sprintf("%s/messages/%s", c.url, email), nil, &res); err != nil { | ||
return res, err | ||
} | ||
return res, nil | ||
} | ||
|
||
func (c *client) GetRecipientLatestMessage(email string) (Message, error) { | ||
var res Message | ||
if err := c.requestJSON(http.MethodGet, fmt.Sprintf("%s/messages/%s/latest", c.url, email), nil, &res); err != nil { | ||
return res, err | ||
} | ||
return res, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.