-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split videoserver to streaming-service and video-metadata-service
- Loading branch information
Showing
10 changed files
with
113 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
17 changes: 9 additions & 8 deletions
17
.../videoserver/app/service/streamService.go → internal/stream-service/app/streamService.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
internal/stream-service/infrastructure/transport/router/routers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters