From 6f426465d2ffb4af7307e86c9ec03b919eca5123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20M=C3=A9ry?= Date: Tue, 30 Apr 2024 19:07:27 +0200 Subject: [PATCH] feat(containers): add sse example --- containers/sse/Dockerfile | 25 ++++++++++++++ containers/sse/README.md | 15 +++++++++ containers/sse/cmd/sse/main.go | 61 ++++++++++++++++++++++++++++++++++ containers/sse/go.mod | 22 ++++++++++++ containers/sse/go.sum | 41 +++++++++++++++++++++++ containers/sse/www/index.html | 23 +++++++++++++ 6 files changed, 187 insertions(+) create mode 100644 containers/sse/Dockerfile create mode 100644 containers/sse/README.md create mode 100644 containers/sse/cmd/sse/main.go create mode 100644 containers/sse/go.mod create mode 100644 containers/sse/go.sum create mode 100644 containers/sse/www/index.html diff --git a/containers/sse/Dockerfile b/containers/sse/Dockerfile new file mode 100644 index 0000000..95b2beb --- /dev/null +++ b/containers/sse/Dockerfile @@ -0,0 +1,25 @@ +FROM golang:1.22-alpine3.19 AS build + +# Install git, this used to fetch VCS information when building the application +RUN apk add --no-cache git + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build ./cmd/sse + +FROM alpine:3.19 + +WORKDIR / + +COPY ./www /www + +COPY --from=build /app/sse . + +EXPOSE 8080 + +CMD ["/sse"] diff --git a/containers/sse/README.md b/containers/sse/README.md new file mode 100644 index 0000000..4642987 --- /dev/null +++ b/containers/sse/README.md @@ -0,0 +1,15 @@ +# Server Side Events with Serverless Containers + +This is a simple example of how to use Server Side Events with Scaleway Serverless Containers. + +## Deploying + +You can deploy the application using the Scaleway CLI. + +```console +scw container deploy +``` + +## Reference + +- diff --git a/containers/sse/cmd/sse/main.go b/containers/sse/cmd/sse/main.go new file mode 100644 index 0000000..a23688d --- /dev/null +++ b/containers/sse/cmd/sse/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "errors" + "log" + "net/http" + "os" + "time" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/r3labs/sse/v2" +) + +var ( + // Serverless Containers will scale to multiple instances. + // This is a way to identify which instance is sending the message to identify the source. + hostName, _ = os.Hostname() +) + +func main() { + e := echo.New() + + server := sse.New() // create SSE broadcaster server + server.AutoReplay = false // do not replay messages for each new subscriber that connects + _ = server.CreateStream("time") // EventSource in "index.html" connecting to stream named "time" + + go func(s *sse.Server) { + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + s.Publish("time", &sse.Event{ + Data: []byte("time: " + time.Now().Format(time.RFC3339Nano) + " from: " + hostName), + }) + } + } + }(server) + + e.Use(middleware.Logger()) + e.Use(middleware.Recover()) + e.Static("/", "www") + + e.GET("/sse", func(c echo.Context) error { // longer variant with disconnect logic + log.Printf("The client is connected: %v\n", c.RealIP()) + go func() { + <-c.Request().Context().Done() // Received Browser Disconnection + log.Printf("The client is disconnected: %v\n", c.RealIP()) + }() + + server.ServeHTTP(c.Response(), c.Request()) + + return nil + }) + + if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatal(err) + } +} diff --git a/containers/sse/go.mod b/containers/sse/go.mod new file mode 100644 index 0000000..8385c38 --- /dev/null +++ b/containers/sse/go.mod @@ -0,0 +1,22 @@ +module ssetest + +go 1.22.1 + +require github.com/labstack/echo/v4 v4.12.0 + +require gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect + +require ( + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/r3labs/sse/v2 v2.10.0 + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/time v0.5.0 // indirect +) diff --git a/containers/sse/go.sum b/containers/sse/go.sum new file mode 100644 index 0000000..2089ea7 --- /dev/null +++ b/containers/sse/go.sum @@ -0,0 +1,41 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= +github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0= +github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y= +gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/containers/sse/www/index.html b/containers/sse/www/index.html new file mode 100644 index 0000000..29d1d2a --- /dev/null +++ b/containers/sse/www/index.html @@ -0,0 +1,23 @@ + + + + + +

Getting server-sent updates

+
+ + + + + + \ No newline at end of file