Skip to content

Commit

Permalink
split videoserver to streaming-service and video-metadata-service
Browse files Browse the repository at this point in the history
  • Loading branch information
col3name committed Jun 15, 2021
1 parent 05c1d09 commit 220096e
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 25 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: build
build: lint build-video build-user build-thumbgenerator build-videoprocessor build-video-scaler build-notifier
build: lint build-video build-user build-streaming-server build-thumbgenerator build-videoprocessor build-video-scaler build-notifier

.PHONY: lint
lint:
Expand All @@ -13,6 +13,10 @@ build-videos:
build-user:
go build -o .\bin\user\userserver.exe .\cmd\user\main.go

.PHONY: build-streaming-server
build-streaming-server:
go build -o .\bin\streaming-server\streamingserver.exe .\cmd\streaming-server\main.go

.PHONY: build-thumbgenerator
build-thumbgenerator:
go build -o .\bin\thumbgenerator\thumbgenerator.exe .\cmd\thumbgenerator\main.go
Expand Down Expand Up @@ -46,6 +50,10 @@ run-videoserver:
run-user:
.\bin\user\userserver.exe

.PHONY: run-streaming-server
run-streaming-server:
.\bin\streaming-server\streamingserver.exe

.PHONY: run-video-comments
run-video-comments:
.\bin\comments\video-comments.exe
Expand Down
29 changes: 29 additions & 0 deletions cmd/streaming-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/bearname/videohost/internal/common/infrarstructure/server"
"github.com/bearname/videohost/internal/common/util"
"github.com/bearname/videohost/internal/stream-service/infrastructure/transport/router"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)

func main() {
logFile := "stream.log"
log.SetFormatter(&log.JSONFormatter{})
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err == nil {
log.SetOutput(file)
defer file.Close()
}

portStr := util.ParseEnvString("PORT", "8020")
port, err := strconv.Atoi(portStr)
if err != nil {
log.Info("Start with default port: 8020")
log.WithError(err).Fatal("failed to parse parseConfig")
}

server.ExecuteServer("streaming-server", port, router.Router())
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package service
package app

import (
"fmt"
"net/http"
)

type StreamServiceImpl struct {
type StreamService struct {
}

func NewStreamServiceImpl() StreamServiceImpl {
return *new(StreamServiceImpl)
func NewStreamService() StreamService {
return *new(StreamService)
}

func (s *StreamServiceImpl) ServeHlsM3u8(w http.ResponseWriter, r *http.Request, videoId string, m3u8Name string) {
func (s *StreamService) ServeHlsM3u8(w http.ResponseWriter, r *http.Request, videoId string, m3u8Name string) {
mediaBase := s.getMediaBase(videoId)

mediaFile := fmt.Sprintf("%s\\%s", mediaBase, m3u8Name)
http.ServeFile(w, r, mediaFile)
w.Header().Set("Content-Type", "application/x-mpegURL")
//w.WriteHeader(http.StatusOK)
http.ServeFile(w, r, mediaFile)
}

func (s *StreamServiceImpl) ServeHlsTs(w http.ResponseWriter, r *http.Request, segName, videoId string) {
func (s *StreamService) ServeHlsTs(w http.ResponseWriter, r *http.Request, segName, videoId string) {
mediaBase := s.getMediaBase(videoId)

mediaFile := fmt.Sprintf("%s\\%s", mediaBase, segName)
http.ServeFile(w, r, mediaFile)
w.Header().Set("Content-Type", "video/MP2T")
}

func (_ *StreamServiceImpl) getMediaBase(id string) string {
func (_ *StreamService) getMediaBase(id string) string {
return "content\\" + id
}
8 changes: 8 additions & 0 deletions internal/stream-service/domain/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package domain

import "net/http"

type StreamService interface {
ServeHlsM3u8(w http.ResponseWriter, r *http.Request, videoId string, m3u8Name string)
ServeHlsTs(w http.ResponseWriter, r *http.Request, segName, videoId string)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package controller

import (
"github.com/bearname/videohost/internal/common/infrarstructure/transport/controller"
"github.com/bearname/videohost/internal/videoserver/app/service"
"github.com/bearname/videohost/internal/stream-service/app"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net/http"
)

type StreamController struct {
controller.BaseController
streamService service.StreamServiceImpl
streamService app.StreamService
}

func NewStreamController(streamService service.StreamServiceImpl) *StreamController {
func NewStreamController(streamService app.StreamService) *StreamController {
v := new(StreamController)
v.streamService = streamService
return v
Expand Down
45 changes: 45 additions & 0 deletions internal/stream-service/infrastructure/transport/router/routers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package router

import (
"github.com/bearname/videohost/internal/common/infrarstructure/profile"
"github.com/bearname/videohost/internal/common/infrarstructure/transport/handler"
"github.com/bearname/videohost/internal/common/infrarstructure/transport/middleware"
"github.com/bearname/videohost/internal/stream-service/app"
"github.com/bearname/videohost/internal/stream-service/infrastructure/controller"
"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
"net/http"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /api/v1/
func Router() http.Handler {

router := mux.NewRouter()
router.HandleFunc("/swagger/{id}", httpSwagger.Handler(httpSwagger.URL("http://localhost:8020/swagger/doc.json"))).Methods(http.MethodGet)

router.HandleFunc("/health", handler.HealthHandler).Methods(http.MethodGet)
router.HandleFunc("/ready", handler.ReadyHandler).Methods(http.MethodGet)

streamController := controller.NewStreamController(app.NewStreamService())
router.HandleFunc("/media/{videoId}/stream/", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/media/{videoId}/{quality}/stream/", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/media/{videoId}/{quality}/stream/{segName:index-[0-9]+.ts}", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)

var imgServer = http.FileServer(http.Dir("C:\\Users\\mikha\\go\\src\\videohost\\bin\\videoserver\\content"))
router.PathPrefix("/content/").Handler(http.StripPrefix("/content/", imgServer))

return middleware.LogMiddleware(profile.BuildHandlers(router))
}
6 changes: 0 additions & 6 deletions internal/videoserver/domain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ import (
"github.com/bearname/videohost/internal/videoserver/domain/dto"
"github.com/bearname/videohost/internal/videoserver/domain/model"
"github.com/google/uuid"
"net/http"
)

type StreamService interface {
ServeHlsM3u8(w http.ResponseWriter, r *http.Request, videoId string, m3u8Name string)
ServeHlsTs(w http.ResponseWriter, r *http.Request, segName, videoId string)
}

type VideoService interface {
FindVideo(videoId string) (*model.Video, error)
UploadVideo(userId string, videoDto *dto.UploadVideoDto) (uuid.UUID, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/bearname/videohost/internal/common/infrarstructure/transport/controller"
"github.com/bearname/videohost/internal/common/util"
"github.com/bearname/videohost/internal/videoserver/app/service"
dto2 "github.com/bearname/videohost/internal/videoserver/domain/dto"
"github.com/bearname/videohost/internal/videoserver/domain/dto"
"github.com/bearname/videohost/internal/videoserver/domain/model"
"github.com/bearname/videohost/internal/videoserver/domain/repository"
"github.com/bearname/videohost/internal/videoserver/infrastructure/transport/requestparser"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c VideoController) GetVideos() func(http.ResponseWriter, *http.Request) {
c.BaseController.WriteResponse(writer, http.StatusBadRequest, false, err.Error())
return
}
searchDto := result.(dto2.SearchDto)
searchDto := result.(dto.SearchDto)

log.Info("page ", searchDto.Page, " count video ", searchDto.Count)
onPage, err := c.videoService.FindVideoOnPage(&searchDto)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (c *VideoController) UploadVideo() func(http.ResponseWriter, *http.Request)
return
}

videoDto := uploadVideoDto.(*dto2.UploadVideoDto)
videoDto := uploadVideoDto.(*dto.UploadVideoDto)
videoId, err := c.videoService.UploadVideo(userDto.UserId, videoDto)
if err != nil {
log.Error(err.Error())
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *VideoController) UpdateTitleAndDescription() func(http.ResponseWriter,
return
}

var videoDto dto2.VideoMetadata
var videoDto dto.VideoMetadata
err = json.NewDecoder(request.Body).Decode(&videoDto)
if err != nil {
c.BaseController.WriteResponse(writer, http.StatusBadRequest, false, "cannot decode videoId|title|description struct")
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *VideoController) SearchVideo() func(http.ResponseWriter, *http.Request)
c.BaseController.WriteResponse(writer, http.StatusBadRequest, false, err.Error())
return
}
searchDto := result.(dto2.SearchDto)
searchDto := result.(dto.SearchDto)

log.Info("page ", searchDto.Page, " count video ", searchDto.Count)
pageCount, ok := c.videoRepository.GetPageCount(searchDto.Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
caching "github.com/bearname/videohost/internal/common/infrarstructure/redis"
"github.com/bearname/videohost/internal/common/infrarstructure/transport/handler"
"github.com/bearname/videohost/internal/common/infrarstructure/transport/middleware"
"github.com/bearname/videohost/internal/stream-service/app"
controller2 "github.com/bearname/videohost/internal/stream-service/infrastructure/controller"
"github.com/bearname/videohost/internal/videoserver/app/service"
"github.com/bearname/videohost/internal/videoserver/infrastructure/mysql"
"github.com/bearname/videohost/internal/videoserver/infrastructure/transport/controller"
Expand Down Expand Up @@ -55,7 +57,7 @@ func Router(connector db.Connector, messageBrokerAddress string, authServerAddre
router.HandleFunc("/health", handler.HealthHandler).Methods(http.MethodGet)
router.HandleFunc("/ready", handler.ReadyHandler).Methods(http.MethodGet)

streamController := controller.NewStreamController(service.NewStreamServiceImpl())
streamController := controller2.NewStreamController(app.NewStreamService())
router.HandleFunc("/media/{videoId}/stream/", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/media/{videoId}/{quality}/stream/", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/media/{videoId}/{quality}/stream/{segName:index-[0-9]+.ts}", streamController.StreamHandler).Methods(http.MethodGet, http.MethodOptions)
Expand Down
3 changes: 2 additions & 1 deletion stop.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

taskkill /F /IM videoserver.exe
taskkill /F /IM userserver.exe
taskkill /F /IM streamingserver.exe
taskkill /F /IM thumbgenerator.exe
taskkill /F /IM video-scaler.exe
taskkill /F /IM notifier.exe
taskkill /F /IM notifier.exe

0 comments on commit 220096e

Please sign in to comment.