feat: add cors support for localhost and alfg.github.io.

docker: add dockerfile, docker-compose example and update goreleaser with
docker support.
This commit is contained in:
Alf
2021-03-14 21:57:57 -07:00
parent 2a3d8f3064
commit bd6581dd57
6 changed files with 78 additions and 10 deletions

3
.dockerignore Normal file
View File

@@ -0,0 +1,3 @@
*.mp4
*.tmp
*.exe

View File

@@ -18,4 +18,7 @@ archives:
format: zip
files:
- README.md
- LICENSE
- LICENSE
dockers:
- image_templates:
- alfg/ffmpegd

23
Dockerfile Normal file
View File

@@ -0,0 +1,23 @@
###############################
# Build the ffmpegd-build image.
FROM golang:1.16-alpine as build
WORKDIR /go/src/ffmpegd
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
##########################
# Build the release image.
FROM alfg/ffmpeg:latest
LABEL MAINTAINER Alfred Gutierrez <alf.g.jr@gmail.com>
WORKDIR /home
ENV PATH=/opt/bin:$PATH
COPY --from=build /go/bin/ffmpegd /opt/bin/ffmpegd
EXPOSE 8080
CMD ["ffmpegd"]

View File

@@ -16,11 +16,24 @@ See [Usage](#Usage) for more details.
```
## Install
### Go
```
$ go get -u github.com/alfg/ffmpegd
```
Docker, Homebrew, and release binaries coming soon.
### Docker
A Docker image is available with [alfg/ffmpeg](https://github.com/alfg/docker-ffmpeg) installed:
```
$ docker run -it -p 8080:8080 -v /tmp/:/home ffmpegd
```
Or using the `docker-compose` example:
```
$ docker-compose up ffmpegd
```
Homebrew package coming soon.
## Usage
* [ffmpeg](https://www.ffmpeg.org/download.html) must be installed and available on your `$PATH`.
@@ -70,10 +83,8 @@ TBD
* Logging levels and output
* More error handling
* API documentation
* Docker
* Test Client Demo
* Tests
* Cross-compile binaries for releases
## License
MIT

9
docker-compose.yml Normal file
View File

@@ -0,0 +1,9 @@
version: '3'
services:
ffmpegd:
build: .
ports:
- "8080:8080"
volumes:
- ./:/home

31
main.go
View File

@@ -22,9 +22,9 @@ const (
██╔══╝ ██╔══╝ ██║╚██╔╝██║██╔═══╝ ██╔══╝ ██║ ██║██║ ██║
██║ ██║ ██║ ╚═╝ ██║██║ ███████╗╚██████╔╝██████╔╝
╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚═════╝
v0.0.3
v0.0.4
`
version = "0.0.3"
version = "0.0.4"
description = "[\u001b[32mffmpegd\u001b[0m] - websocket server for \u001b[33mffmpeg-commander\u001b[0m.\n"
usage = `
Usage:
@@ -35,11 +35,21 @@ Usage:
)
var (
allowedOrigins = []string{
"http://localhost:8080",
"http://localhost:8081",
"https://alfg.github.io",
}
clients = make(map[*websocket.Conn]bool)
broadcast = make(chan Message)
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
for _, origin := range allowedOrigins {
if r.Header.Get("Origin") == origin {
return true
}
}
return false
},
}
progressCh chan struct{}
@@ -107,7 +117,7 @@ func startServer() {
fmt.Println(" - \u001b[33mffmpegd\u001b[0m must be enabled in ffmpeg-commander options.")
fmt.Println("")
fmt.Printf("Waiting for connection...")
err := http.ListenAndServe("127.0.0.1:8080", nil)
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("ListenAndServe: ", err)
}
@@ -116,7 +126,8 @@ func startServer() {
func handleConnections(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
fmt.Printf("\rWaiting for connection...\u001b[31m websocket connection failed!\u001b[0m")
return
}
defer ws.Close()
@@ -173,10 +184,18 @@ func handleFiles(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
cors(&w, r)
json.NewEncoder(w).Encode(resp)
}
func cors(w *http.ResponseWriter, r *http.Request) {
for _, origin := range allowedOrigins {
if r.Header.Get("Origin") == origin {
(*w).Header().Set("Access-Control-Allow-Origin", origin)
}
}
}
func handleMessages() {
for {
msg := <-broadcast