Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ npm install

Then set *environment variables* as necessary. The following variables are available:
* **WATCH_PIN** (Sets the PIN users need to use to access the stream, default *NOT SET*)
* **ADMIN_PIN** (Sets the PIN for the admin interface, default *4542*)
* **ADMIN_PIN** (Sets the PIN for the admin interface, default *0000*)

## Usage

Expand Down
105 changes: 105 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package config

import (
"github.com/ilyakaznacheev/cleanenv"

log "github.com/sirupsen/logrus"
)

type Configuration struct {
Access Access
Port int `env:"PORT" env-default:"8080"`
State State
}

type State struct {
Video Video
Subtitles []Subtitle
Playback Playback
}

type Access struct {
WatchPIN string `env:"WATCH_PIN"`
AdminPIN string `env:"ADMIN_PIN" env-default:"0000"`
}

type Subtitle struct {
Path string
Language string
LanguageCode string
}

type Playback struct {
PlaybackRunning bool
Seconds int
}

type Video struct {
Title string `env:"VIDEO_TITLE"`
Path string `env:"VIDEO_PATH"`
}

var config Configuration

func Init() {
if err := cleanenv.ReadEnv(&config); err != nil {
log.Fatal("Error when parsing the configuration.")
}
}

func GetPort() (port int) {
return config.Port
}

func GetState() (state State) {
return config.State
}

func SetVideoTitle(title string) {
config.State.Video.Title = title
}

func SetVideoPath(path string) {
config.State.Video.Path = path
}

func StartPlayback() {
config.State.Playback.PlaybackRunning = true
}

func PausePlayback() {
config.State.Playback.PlaybackRunning = false
}

func ResetPlayback() {
config.State.Playback.PlaybackRunning = false
config.State.Playback.Seconds = 0
}

func SetPlaybackSeconds(seconds int) {
config.State.Playback.Seconds = seconds
}

func SetSubtitles(subtitles []Subtitle) {
config.State.Subtitles = subtitles
}

func SetWatchPIN(pin string) {
config.Access.WatchPIN = pin
}

func ClearWatchPIN() {
config.Access.WatchPIN = ""
}

func GetWatchPIN() string {
return config.Access.WatchPIN
}

func SetAdminPIN(pin string) {
config.Access.AdminPIN = pin
}

func GetAdminPIN() string {
return config.Access.AdminPIN
}
71 changes: 71 additions & 0 deletions controllers/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package controllers

import (
"encoding/json"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/lukasklinger/VideoSync/config"
log "github.com/sirupsen/logrus"
)

type APIController struct{}

type APICommand struct {
Command string `json:"command"`
Value string `json:"value"`
}

func (a APIController) GetState(c *gin.Context) {
c.JSON(http.StatusOK, config.GetState())
c.Abort()
}

func (a APIController) PostState(c *gin.Context) {
var jsonObject APICommand

body, err := c.GetRawData()
if err != nil {
log.Errorf("Error getting request body: %v", err)
return
}

if err := json.Unmarshal(body, &jsonObject); err != nil {
log.Errorf("Error unmarshalling JSON", err)
return
}

updateState(jsonObject)

c.JSON(http.StatusOK, config.GetState())
c.Abort()
}

func updateState(command APICommand) {
switch command.Command {
case "start":
config.StartPlayback()
case "clientpause":
pausePlayback(command)
case "reset":
config.ResetPlayback()
case "title":
config.SetVideoTitle(command.Value)
case "time":
seconds, _ := strconv.Atoi(command.Value)
config.SetPlaybackSeconds(seconds)
case "video":
config.SetVideoPath(command.Value)
case "subtitles":
// TODO
}
}

func pausePlayback(command APICommand) {
// TODO
}

func setSubtitles() {
// TODO
}
13 changes: 13 additions & 0 deletions controllers/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package controllers

import (
"net/http"

"github.com/gin-gonic/gin"
)

type HealthController struct{}

func (h HealthController) Status(c *gin.Context) {
c.String(http.StatusOK, "Working!")
}
30 changes: 30 additions & 0 deletions controllers/pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package controllers

import (
"net/http"

"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/lukasklinger/VideoSync/config"
)

type PINController struct{}

func (p PINController) Authenticate(c *gin.Context) {
pin := c.PostForm("pin")
session := sessions.Default(c)

if pin == config.GetWatchPIN() {
session.Set("watchPIN", pin)
c.Redirect(http.StatusFound, "/watch")
} else if pin == config.GetAdminPIN() {
session.Set("adminPIN", pin)
c.Redirect(http.StatusFound, "/admin")
} else {
// Wrong PIN, redirect back to PIN interface
c.Redirect(http.StatusFound, "/pin")
}

session.Save()
c.Abort()
}
33 changes: 33 additions & 0 deletions controllers/websockets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package controllers

import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/lukasklinger/VideoSync/model"
log "github.com/sirupsen/logrus"
)

type WebSocketsController struct {
Pool *model.Pool
}

var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func NewWebsocketController(pool *model.Pool) WebSocketsController {
return WebSocketsController{Pool: pool}
}

func (w WebSocketsController) Serve(c *gin.Context) {
connection, err := wsupgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Debug("Error upgrading websocket connection: %v", err)
}

client := &model.Client{Conn: connection, Pool: w.Pool}

w.Pool.Register <- client
client.Read()
}
13 changes: 0 additions & 13 deletions docker-compose.yml

This file was deleted.

30 changes: 30 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module github.com/lukasklinger/VideoSync

go 1.16

require (
github.com/BurntSushi/toml v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chenjiandongx/ginprom v0.0.0-20210617023641-6c809602c38a
github.com/gin-contrib/sessions v0.0.3
github.com/gin-gonic/gin v1.7.4
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/gorilla/websocket v1.4.2
github.com/ilyakaznacheev/cleanenv v1.2.5
github.com/joho/godotenv v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.31.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b // indirect
github.com/sirupsen/logrus v1.8.1
github.com/ugorji/go v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
Loading