package server import ( "context" "log" "net/http" "os" "time" pbMediaSet "git.netflux.io/rob/clipper/generated/pb/media_set" "git.netflux.io/rob/clipper/media" "github.com/improbable-eng/grpc-web/go/grpcweb" "google.golang.org/grpc" "google.golang.org/grpc/grpclog" "google.golang.org/protobuf/types/known/durationpb" ) type Options struct { BindAddr string Timeout time.Duration } // mediaSetServiceController implements gRPC controller for MediaSetService type mediaSetServiceController struct { pbMediaSet.UnimplementedMediaSetServiceServer mediaSetService *media.MediaSetService } // GetMediaSet returns a pbMediaSet.MediaSet func (c *mediaSetServiceController) GetMediaSet(ctx context.Context, request *pbMediaSet.GetMediaSetRequest) (*pbMediaSet.MediaSet, error) { mediaSet, err := c.mediaSetService.GetMediaSet(ctx, request.Source, request.Id) if err != nil { return nil, err } result := pbMediaSet.MediaSet{ Id: mediaSet.ID, Audio: &pbMediaSet.MediaSet_Audio{ Bytes: mediaSet.Audio.Bytes, Channels: int32(mediaSet.Audio.Channels), Frames: mediaSet.Audio.Frames, SampleRate: int32(mediaSet.Audio.SampleRate), }, Video: &pbMediaSet.MediaSet_Video{ Bytes: mediaSet.Video.Bytes, Duration: durationpb.New(mediaSet.Video.Duration), ThumbnailWidth: int32(mediaSet.Video.ThumbnailWidth), ThumbnailHeight: int32(mediaSet.Video.ThumbnailHeight), }, } return &result, nil } func (c *mediaSetServiceController) GetPeaks(*pbMediaSet.GetPeaksRequest, pbMediaSet.MediaSetService_GetPeaksServer) error { return nil } func Start(options Options) error { grpcServer := grpc.NewServer() pbMediaSet.RegisterMediaSetServiceServer(grpcServer, &mediaSetServiceController{}) grpclog.SetLogger(log.New(os.Stdout, "server: ", log.LstdFlags)) // TODO: implement CORS grpcWebServer := grpcweb.WrapServer(grpcServer, grpcweb.WithOriginFunc(func(string) bool { return true })) handler := func(w http.ResponseWriter, r *http.Request) { grpcWebServer.ServeHTTP(w, r) } httpServer := http.Server{ Addr: options.BindAddr, ReadTimeout: options.Timeout, WriteTimeout: options.Timeout, Handler: http.HandlerFunc(handler), } return httpServer.ListenAndServe() }