clipper/backend/server/server.go

77 lines
1.7 KiB
Go
Raw Normal View History

2021-09-25 17:00:19 +00:00
package server
import (
"math/rand"
"sync"
2021-09-25 17:00:19 +00:00
"time"
"github.com/gorilla/websocket"
2021-09-25 17:00:19 +00:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Options struct {
BindAddr string
Timeout time.Duration
}
type messageSinkStore struct {
sinks map[*websocket.Conn]*MessageSink
mu sync.Mutex
}
func (ss *messageSinkStore) Put(conn *websocket.Conn, s *MessageSink) {
ss.mu.Lock()
defer ss.mu.Unlock()
ss.sinks[conn] = s
}
func (ss *messageSinkStore) Get(conn *websocket.Conn) (*MessageSink, bool) {
ss.mu.Lock()
defer ss.mu.Unlock()
s, ok := ss.sinks[conn]
return s, ok
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
2021-09-25 17:00:19 +00:00
func Start(opts Options) error {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
h := handler{
messageSinks: &messageSinkStore{
sinks: make(map[*websocket.Conn]*MessageSink),
},
}
// websockets provide their own custom CORS handler, so don't add further
// middleware here.
e.GET("/api/websocket", h.upgradeWebsocket)
api := e.Group("/api")
api.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{Timeout: opts.Timeout}))
api.Use(middleware.CORSWithConfig(middleware.CORSConfig{
2021-09-25 17:00:19 +00:00
AllowOrigins: []string{"http://localhost:3000"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
api.GET("/media_sets/:id", h.getMediaSet)
api.GET("/media_sets/:id/peaks", h.getPeaks)
api.GET("/media_sets/:id/thumbnails", h.getThumbnails)
api.GET("/media_sets/:id/video", h.getVideo)
2021-09-25 17:00:19 +00:00
return e.Start(opts.BindAddr)
}