refactor: create package

monorepo
Varun Patil 2023-10-21 11:36:22 -07:00
parent 48ed209d59
commit ea9e620de3
8 changed files with 32 additions and 25 deletions

View File

@ -1,4 +1,4 @@
package main
package go_vod
type Chunk struct {
id int
@ -12,4 +12,4 @@ func NewChunk(id int) *Chunk {
done: false,
notifs: make([]chan bool, 0),
}
}
}

View File

@ -1,4 +1,4 @@
package main
package go_vod
import (
"encoding/json"
@ -9,6 +9,9 @@ import (
)
type Config struct {
// Current version of go-vod
Version string
// Is this server configured?
Configured bool

View File

@ -1,4 +1,4 @@
package main
package go_vod
import (
"encoding/json"
@ -85,7 +85,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
json.NewEncoder(w).Encode(map[string]interface{}{
"version": VERSION,
"version": h.c.Version,
"size": size,
})
return
@ -149,8 +149,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *Handler) versionOk(w http.ResponseWriter, r *http.Request) bool {
expected := r.Header.Get("X-Go-Vod-Version")
if len(expected) > 0 && expected != VERSION {
log.Println("Version mismatch", expected, VERSION)
if len(expected) > 0 && expected != h.c.Version {
log.Println("Version mismatch", expected, h.c.Version)
// Try again in some time
w.WriteHeader(http.StatusServiceUnavailable)
@ -202,6 +202,8 @@ func (h *Handler) removeManager(streamid string) {
}
func (h *Handler) Start() {
h.server = &http.Server{Addr: h.c.Bind, Handler: h}
go func() {
err := h.server.ListenAndServe()
if err != nil {
@ -212,10 +214,17 @@ func (h *Handler) Start() {
for {
id := <-h.close
if id == "" {
return
break
}
h.removeManager(id)
}
// Stop server
log.Println("Exiting VOD server")
h.server.Close()
// Exit with correct status code
os.Exit(h.exitCode)
}
func (h *Handler) Close() {

View File

@ -1,4 +1,4 @@
package main
package go_vod
import (
"bytes"
@ -261,7 +261,8 @@ func (m *Manager) ffprobe() error {
m.path,
}
ctx, _ := context.WithDeadline(context.TODO(), time.Now().Add(5*time.Second))
ctx, cancel := context.WithDeadline(context.TODO(), time.Now().Add(5*time.Second))
defer cancel()
cmd := exec.CommandContext(ctx, m.c.FFprobe, args...)
var stdout, stderr bytes.Buffer

View File

@ -1,4 +1,4 @@
package main
package go_vod
import (
"bufio"

View File

@ -1,4 +1,4 @@
package main
package go_vod
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package main
package go_vod
import "net/http"

18
main.go
View File

@ -3,16 +3,18 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/pulsejet/go-vod/go_vod"
)
const VERSION = "0.1.19"
func main() {
// Build initial configuration
c := &Config{
c := &go_vod.Config{
VersionMonitor: false,
Version: VERSION,
Bind: ":47788",
ChunkSize: 3,
LookBehind: 3,
@ -37,15 +39,7 @@ func main() {
// Auto detect ffmpeg and ffprobe
c.AutoDetect()
// Build HTTP server
// Start server
log.Println("Starting go-vod " + VERSION + " on " + c.Bind)
handler := NewHandler(c)
handler.server = &http.Server{Addr: c.Bind, Handler: handler}
// Start server and wait for handler exit
handler.Start()
log.Println("Exiting VOD server")
// Exit with status code
os.Exit(handler.exitCode)
go_vod.NewHandler(c).Start()
}