Skip to content

Instantly share code, notes, and snippets.

package otel
import (
"context"
"os"
"time"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/slog"
import {ref} from "vue"
import {Leave, Message, User, UserList, usernames} from "../data/data"
import {eventBus} from "./eventBus"
import {defineStore} from "pinia"
export const usePiniaStore = defineStore("main", () => {
const connectionId = ref<string | null>()
const currentRecipient = ref<User | null>()
const userList = ref(new Map<string, User>())
const messages = ref(new Map<string, Message[]>())
<script lang="ts">
import { computed, defineComponent, PropType } from "vue"
import { useStore } from "@/store/store"
import { User } from "@/store/data"
export default defineComponent({
name: "UserItem",
props: {
user: {
type: Object as PropType<User>,
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)
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
// 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() {
// 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,
// 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.
// 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
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