refactor: create package
parent
48ed209d59
commit
ea9e620de3
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
type Chunk struct {
|
type Chunk struct {
|
||||||
id int
|
id int
|
||||||
|
@ -12,4 +12,4 @@ func NewChunk(id int) *Chunk {
|
||||||
done: false,
|
done: false,
|
||||||
notifs: make([]chan bool, 0),
|
notifs: make([]chan bool, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -9,6 +9,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
// Current version of go-vod
|
||||||
|
Version string
|
||||||
|
|
||||||
// Is this server configured?
|
// Is this server configured?
|
||||||
Configured bool
|
Configured bool
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -85,7 +85,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||||
"version": VERSION,
|
"version": h.c.Version,
|
||||||
"size": size,
|
"size": size,
|
||||||
})
|
})
|
||||||
return
|
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 {
|
func (h *Handler) versionOk(w http.ResponseWriter, r *http.Request) bool {
|
||||||
expected := r.Header.Get("X-Go-Vod-Version")
|
expected := r.Header.Get("X-Go-Vod-Version")
|
||||||
if len(expected) > 0 && expected != VERSION {
|
if len(expected) > 0 && expected != h.c.Version {
|
||||||
log.Println("Version mismatch", expected, VERSION)
|
log.Println("Version mismatch", expected, h.c.Version)
|
||||||
|
|
||||||
// Try again in some time
|
// Try again in some time
|
||||||
w.WriteHeader(http.StatusServiceUnavailable)
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
|
@ -202,6 +202,8 @@ func (h *Handler) removeManager(streamid string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) Start() {
|
func (h *Handler) Start() {
|
||||||
|
h.server = &http.Server{Addr: h.c.Bind, Handler: h}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := h.server.ListenAndServe()
|
err := h.server.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -212,10 +214,17 @@ func (h *Handler) Start() {
|
||||||
for {
|
for {
|
||||||
id := <-h.close
|
id := <-h.close
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
h.removeManager(id)
|
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() {
|
func (h *Handler) Close() {
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -261,7 +261,8 @@ func (m *Manager) ffprobe() error {
|
||||||
m.path,
|
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...)
|
cmd := exec.CommandContext(ctx, m.c.FFprobe, args...)
|
||||||
|
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package go_vod
|
||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
18
main.go
18
main.go
|
@ -3,16 +3,18 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/pulsejet/go-vod/go_vod"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "0.1.19"
|
const VERSION = "0.1.19"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Build initial configuration
|
// Build initial configuration
|
||||||
c := &Config{
|
c := &go_vod.Config{
|
||||||
VersionMonitor: false,
|
VersionMonitor: false,
|
||||||
|
Version: VERSION,
|
||||||
Bind: ":47788",
|
Bind: ":47788",
|
||||||
ChunkSize: 3,
|
ChunkSize: 3,
|
||||||
LookBehind: 3,
|
LookBehind: 3,
|
||||||
|
@ -37,15 +39,7 @@ func main() {
|
||||||
// Auto detect ffmpeg and ffprobe
|
// Auto detect ffmpeg and ffprobe
|
||||||
c.AutoDetect()
|
c.AutoDetect()
|
||||||
|
|
||||||
// Build HTTP server
|
// Start server
|
||||||
log.Println("Starting go-vod " + VERSION + " on " + c.Bind)
|
log.Println("Starting go-vod " + VERSION + " on " + c.Bind)
|
||||||
handler := NewHandler(c)
|
go_vod.NewHandler(c).Start()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue