-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (114 loc) · 3.08 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"runtime"
"embed"
vmixutility "github.com/FlowingSPDG/vmix-utility/server"
"github.com/gin-gonic/gin"
)
//go:embed web/dist/*
var staticFS embed.FS
//go:embed vMixMultiview/index.html
//go:embed vMixMultiview/script.js
//go:embed vMixMultiview/jquery.js
//go:embed vMixMultiview/style.css
var multiviewFS embed.FS
func main() {
log.Println("STARTING...")
// Parse flags
vmixAddr := ""
hostPort := 0
flag.StringVar(&vmixAddr, "vmix", "http://localhost:8088", "vMix API Address")
flag.IntVar(&hostPort, "host", 8080, "Server listen port")
flag.Parse()
// Init utility instance
util, err := vmixutility.NewUtilityClient(hostPort, vmixAddr)
if err != nil {
panic(err)
}
// Init Gin router
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// Cache files
index, err := staticFS.ReadFile("web/dist/index.html")
if err != nil {
panic(err)
}
favicon, err := staticFS.ReadFile("web/dist/favicon.ico")
if err != nil {
panic(err)
}
// serve static files
r.GET("/", func(c *gin.Context) {
c.Writer.WriteString(string(index))
})
r.GET("/favicon.ico", func(c *gin.Context) {
c.Data(http.StatusOK, "image/x-icon", favicon)
})
r.GET("/css/*file", func(c *gin.Context) {
file := c.Param("file")
b, err := staticFS.ReadFile("web/dist/css" + file)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.Data(http.StatusOK, "text/css", b)
})
r.GET("/js/*file", func(c *gin.Context) {
file := c.Param("file")
b, err := staticFS.ReadFile("web/dist/js" + file)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.Data(http.StatusOK, "text/css", b)
})
r.GET("/img/*file", func(c *gin.Context) {
file := c.Param("file")
b, err := staticFS.ReadFile("web/dist/img" + file)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.Data(http.StatusOK, "text/css", b)
})
r.GET("/fonts/*file", func(c *gin.Context) {
file := c.Param("file")
b, err := staticFS.ReadFile("web/dist/fonts" + file)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.Data(http.StatusOK, "text/css", b)
})
r.GET("/multiviewer/*file", func(c *gin.Context) {
file := c.Param("file")
b, err := multiviewFS.ReadFile("vMixMultiview" + file)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.Data(http.StatusOK, "", b)
})
api := r.Group("/api")
{
api.GET("/vmix", util.GetvMixURLHandler)
api.GET("/shortcuts", util.GetvMixShortcuts)
api.GET("/inputs", util.GetInputsHandler)
api.POST("/refresh", util.RefreshInputHandler)
api.POST("/multiple", util.DoMultipleFunctionsHandler)
}
// Windowsの場合、自動的にブラウザを開く
if runtime.GOOS == "windows" {
url := fmt.Sprintf("http://localhost:%d/", hostPort)
if err := exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", url).Start(); err != nil {
log.Println("Failed to open link. ignoring...")
log.Printf("ERR : %v\n", err)
}
}
log.Panicf("Failed to listen port %s : %v\n", hostPort, r.Run(fmt.Sprintf(":%d", hostPort)))
}