Skip to content

Instantly share code, notes, and snippets.

@Jimeux
Jimeux / e6_upload_client_async.go
Created February 3, 2019 06:42
io.Pipeによるファイルのアップロード
package main
import (
"io"
"log"
"mime/multipart"
"net/http"
"os"
)
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}
}
@Jimeux
Jimeux / main.go
Last active February 10, 2021 02:52
Extract Chi routing from main.go with handlers struct
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,
})
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
// 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
// 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.
// 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,
// 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() {
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
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)