This file contains hidden or 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
| func notFoundHandler(w http.ResponseWriter, r *http.Request) { | |
| writeRawBody(w, r, notFoundResponse, http.StatusNotFound) | |
| } | |
| func doHandler() http.HandlerFunc { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| var req Request | |
| if err := unmarshalBody(r, &req); err != nil { | |
| log.Println(err) | |
| response := NewErrorResponse(err) |
This file contains hidden or 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
| func TestNotFoundHandler(t *testing.T) { | |
| req := httptest.NewRequest("GET", "/not-found", nil) | |
| rr := httptest.NewRecorder() | |
| notFoundHandler(rr, req) | |
| res := rr.Result() | |
| resBody, _ := ioutil.ReadAll(res.Body) | |
| assert.Equal(t, http.StatusNotFound, res.StatusCode) | |
| assert.JSONEq(t, `{"message": "not found"}`, string(resBody)) |
This file contains hidden or 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
| func remoteHandler(client HTTPClient) http.HandlerFunc { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| url := r.URL.Query().Get("url") | |
| if url == "" { | |
| url = "http://example.com/" | |
| } | |
| req, err := http.NewRequest(http.MethodPost, url, r.Body) | |
| if err != nil { | |
| log.Println(err) |
This file contains hidden or 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
| func TestRemoteHandlerWithClientMock(t *testing.T) { | |
| mockRes := &http.Response{ | |
| StatusCode: http.StatusOK, | |
| Body: ioutil.NopCloser(strings.NewReader(`{"foo": "bar"}`)), | |
| } | |
| clientMock := mocks.HTTPClient{} | |
| clientMock.On("Do", mock.AnythingOfType("*http.Request")).Return(mockRes, nil) | |
| req := httptest.NewRequest("GET", "/remote", nil) | |
| rr := httptest.NewRecorder() |
This file contains hidden or 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
| func TestRemoteHandlerWithStubServer(t *testing.T) { | |
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(http.StatusServiceUnavailable) | |
| })) | |
| defer ts.Close() | |
| url := fmt.Sprintf("/remote?url=%s", ts.URL) | |
| req := httptest.NewRequest("GET", url, nil) | |
| rr := httptest.NewRecorder() |
This file contains hidden or 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
| # 1. Build | |
| FROM golang:alpine as builder | |
| ENV GO111MODULE=on \ | |
| CGO_ENABLED=0 \ | |
| GOOS=linux \ | |
| GOARCH=amd64 | |
| WORKDIR /build |
This file contains hidden or 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
| version: '3' | |
| services: | |
| calc: | |
| build: . | |
| networks: | |
| - calc-nw | |
| ports: | |
| - "${CALC_PORT:-8080}:8080" |
This file contains hidden or 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
| func notFoundHandler(w http.ResponseWriter, r *http.Request) { | |
| writeRawBody(w, r, notFoundResponse, http.StatusNotFound) | |
| } | |
| func doHandler() http.HandlerFunc { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| var req Request | |
| if err := unmarshalBody(r, &req); err != nil { | |
| log.Println(err) | |
| response := NewErrorResponse(err) |
This file contains hidden or 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
| func Start(address string, routes map[string]http.HandlerFunc) error { | |
| mux := http.NewServeMux() | |
| for route, handler := range routes { | |
| mux.Handle(route, handler) | |
| } | |
| s := &http.Server{ | |
| Addr: address, | |
| Handler: mux, |
This file contains hidden or 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
| { | |
| "Records": [ | |
| { | |
| "messageId": "19dd0b57-b21e-4ac1-bd88-01bbb068cb78", | |
| "receiptHandle": "MessageReceiptHandle", | |
| "body": "Hello from SQS!", | |
| "attributes": { | |
| "ApproximateReceiveCount": "1", | |
| "SentTimestamp": "1523232000000", | |
| "SenderId": "123456789012", |