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
package main | |
import ( | |
"io" | |
"log" | |
"mime/multipart" | |
"net/http" | |
"os" | |
) |
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
type DomainError struct { | |
prefix string | |
code int | |
msg string | |
} | |
func NewError(prefix string, code int, msg string) error { | |
return &DomainError{prefix: prefix, code: code, msg: msg} | |
} |
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 main() { | |
... | |
mux := handler(conf) | |
... | |
} | |
func handler(conf config.Config) chi.Router { | |
db, err := gorm.Open(mysql.Open(conf.Database.String), &gorm.Config{ | |
SkipDefaultTransaction: true, | |
}) |
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
service: sls-svc-ws | |
frameworkVersion: '2' | |
provider: | |
name: aws | |
runtime: go1.x | |
lambdaHashingVersion: 20201221 | |
stage: dev | |
region: ap-northeast-1 | |
websocketsApiName: chat-api-websocket-api |
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
// Connection stores a reference to an active WebSocket connection. | |
type Connection struct { | |
ConnectionID string `dynamodbav:"connection_id" json:"connectionId"` | |
Username string `dynamodbav:"username,omitempty" json:"username,omitempty"` // not unique | |
} | |
// Repository implements CRUD operations on the Connection table. | |
type Repository struct { | |
ddb awsiface.DynamoDB | |
tableName *string |
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
// APIClient is a simple wrapper around APIGatewayManagementAPI. | |
type APIClient struct { | |
client awsiface.APIGatewayManagementAPI | |
} | |
func NewAPIGatewayClient(client awsiface.APIGatewayManagementAPI) *APIClient { | |
return &APIClient{client: client} | |
} | |
// NewAPIClientFromConfig create a APIClient instance from a given aws.Config instance. |
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
// WebSocketService implements the core logic of svc-ws. | |
type WebSocketService struct { | |
client *Client | |
repository *Repository | |
} | |
func NewWebSocketService(client *Client, repository *Repository) *WebSocketService { | |
return &WebSocketService{ | |
client: client, | |
repository: repository, |
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
// Package-level vars survive for the life of the | |
// Lambda, and help avoid unnecessary allocations. | |
var ( | |
logger *ws.Logger | |
svc *ws.WebSocketService | |
) | |
// main initialises package-level vars and calls lambda.Start, passing | |
// handler, which is wrapped in middleware that initialises logging. | |
func main() { |
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
type HandlerFunc func(ctx context.Context, req *events.APIGatewayWebsocketProxyRequest) (Response, error) | |
// Middleware executed initialization logic common to all WS handlers. | |
func Middleware(logger *Logger, next HandlerFunc) HandlerFunc { | |
return func(ctx context.Context, req *events.APIGatewayWebsocketProxyRequest) (Response, error) { | |
// set common context values for logging | |
ctx = context.WithValue(ctx, KeyConnectionID, req.RequestContext.ConnectionID) | |
ctx = context.WithValue(ctx, KeyRequestID, req.RequestContext.RequestID) | |
// flush buffered logs on exit |
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 TestHandler(t *testing.T) { | |
logger = ws.NewNopLogger() | |
t.Run("returns 200 on successful save", func(t *testing.T) { | |
ddb := &awsiface.MockDDB{} | |
ddb.PutItemFn = func(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error) { | |
return &dynamodb.PutItemOutput{}, nil | |
} | |
repo := ws.NewRepository(ddb, "connections") | |
svc = ws.NewWebSocketService(nil, repo) |