-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (128 loc) · 3.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Opencast Capture Agent Display
// Copyright 2024 Osnabrück University
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"embed"
"encoding/json"
"errors"
"io"
"io/fs"
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
)
type AgentStateResult struct {
Update struct {
Name string
State string
Url string
} `json:"agent-state-update"`
}
type DisplayConfig struct {
Text string `json:"text"`
Color string `json:"color"`
Background string `json:"background"`
Image string `json:"image"`
}
type Config struct {
Opencast struct {
Url string
Username string
Password string
Agent string
}
Display struct {
Capturing DisplayConfig `json:"capturing"`
Idle DisplayConfig `json:"idle"`
Unknown DisplayConfig `json:"unknown"`
}
Listen string
}
var (
config Config
//go:embed assets
res embed.FS
)
func loadConfig(configPath string) (*Config, error) {
// Open config file
yamlFile, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
// Decode YAML file
if err := yaml.Unmarshal(yamlFile, &config); err != nil {
return nil, err
}
// Ensure URL does not have trailing /
config.Opencast.Url = strings.Trim(config.Opencast.Url, "/")
if config.Opencast.Url == "" {
return nil, errors.New("No Opencast server URL in configuration")
}
if config.Listen == "" {
config.Listen = "127.0.0.1:8080"
}
return &config, nil
}
func setupRouter() *gin.Engine {
r := gin.Default()
// Use assets/index.html for /
r.GET("/", func(c *gin.Context) {
c.Request.URL.Path = "/assets/"
r.HandleContext(c)
})
// Static assets
assets, _ := fs.Sub(res, "assets")
r.StaticFS("/assets", http.FS(assets))
// Display Config
r.GET("/config", func(c *gin.Context) {
c.JSON(http.StatusOK, config.Display)
})
// Status
r.GET("/status", func(c *gin.Context) {
client := &http.Client{}
url := config.Opencast.Url + "/capture-admin/agents/" + config.Opencast.Agent + ".json"
req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(config.Opencast.Username, config.Opencast.Password)
resp, err := client.Do(req)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadGateway, nil)
return
}
if resp.StatusCode != 200 {
log.Println(resp)
c.JSON(resp.StatusCode, nil)
return
}
bodyText, err := io.ReadAll(resp.Body)
s := string(bodyText)
var result AgentStateResult
json.Unmarshal([]byte(s), &result)
c.JSON(http.StatusOK, result.Update.State == "capturing")
})
return r
}
func main() {
if _, err := loadConfig("opencast-ca-display.yml"); err != nil {
log.Fatal(err)
return
}
r := setupRouter()
r.Run(config.Listen)
}