Skip to content

Commit

Permalink
Remove assumption code is handling event JSON directly
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed Apr 26, 2024
1 parent 121f476 commit 320c0af
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 304 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint-test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: v1.54

- name: check valid yml
run: yq . ./docker-images/*/scyllaridae.yml

- name: Install dependencies
run: go get .
Expand Down
181 changes: 86 additions & 95 deletions docker-images/ffmpeg/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -1,98 +1,89 @@
destinationHttpMethod: PUT
allowedMimeTypes: [
"audio/*",
"video/*",
"image/jpeg",
"image/png"
]
allowedMimeTypes:
- "audio/*"
- "video/*"
- "image/jpeg"
- "image/png"
cmdByMimeType:
"video/x-msvideo"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"avi"
]
"video/ogg"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"ogg"
]
"audio/x-wav"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"wav"
]
"audio/mpeg"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"mp3"
]
"audio/aac"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"m4a"
]
"image/jpeg"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"image2pipe"
]
"image/png"
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-f",
"image2pipe"
]
"video/x-msvideo":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "avi"
"video/ogg":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "ogg"
"audio/x-wav":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "wav"
"audio/mpeg":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "mp3"
"audio/aac":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "m4a"
"image/jpeg":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "image2pipe"
"image/png":
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-f"
- "image2pipe"
"video/mp4":
cmd: "ffmpeg"
args: [
"-i",
"-",
"%s",
"-vcodec",
"libx264",
"-preset",
"medium",
"-acodec",
"aac",
"-strict",
"-2",
"-ab",
"128k",
"-ac",
"2",
"-async",
"1",
"-movflags",
"faststart",
"-y",
"-f",
"mp4",
"-"
]
cmd: ffmpeg
args:
- "-i"
- "-"
- "%s"
- "-vcodec"
- "libx264"
- "-preset"
- "medium"
- "-acodec"
- "aac"
- "-strict"
- "-2"
- "-ab"
- "128k"
- "-ac"
- "2"
- "-async"
- "1"
- "-movflags"
- "faststart"
- "-y"
- "-f"
- "mp4"
- "-"
18 changes: 8 additions & 10 deletions docker-images/fits/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
destinationHttpMethod: GET
forwardAuth: false
allowedMimeTypes: [
"*"
]
allowedMimeTypes:
- "*"
cmdByMimeType:
default:
cmd: "curl"
args: [
"http://fits:8080/fits/examine",
"-X",
"POST",
"-F",
"datafile=@-"
]
args:
- "http://fits:8080/fits/examine"
- "-X"
- "POST"
- "-F"
- "datafile=@-"
18 changes: 14 additions & 4 deletions docker-images/imagemagick/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
destinationHttpMethod: PUT
allowedMimeTypes: [
"text/html"
]
allowedMimeTypes:
- "application/pdf"
- "image/*"
cmdByMimeType:
"application/pdf":
cmd: convert
args:
- "pdf:-[0]"
- "%s"
- "pdf:-"
default:
cmd: "convert"
cmd: convert
args:
- "-"
- "%s"
- "image:-"
25 changes: 11 additions & 14 deletions docker-images/tesseract/scyllaridae.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
destinationHttpMethod: PUT
allowedMimeTypes: [
"application/pdf",
"image/*"
]
allowedMimeTypes:
- "application/pdf"
- "image/*"
cmdByMimeType:
"application/pdf":
cmd: pdftotext
args: [
"%s",
"-",
"-"
]
args:
- "%s"
- "-"
- "-"
default:
cmd: tesseract
args: [
"stdin",
"stdout",
"%s"
]
args:
- "stdin"
- "stdout"
- "%s"
46 changes: 4 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"fmt"
"log/slog"
"net/http"
"os"
Expand Down Expand Up @@ -39,11 +38,10 @@ func main() {
}

func MessageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodPut {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

defer r.Body.Close()

// Read the Alpaca message payload
Expand Down Expand Up @@ -73,51 +71,15 @@ func MessageHandler(w http.ResponseWriter, r *http.Request) {
cmd.Stdin = sourceResp.Body

// Create a buffer to store the output
var outBuf, stdErr bytes.Buffer
cmd.Stdout = &outBuf
var stdErr bytes.Buffer
cmd.Stdout = w
cmd.Stderr = &stdErr

slog.Info("Running command", "cmd", cmd.String())

if err := cmd.Run(); err != nil {
slog.Error("Error running command", "cmd", cmd.String(), "err", stdErr.String())
http.Error(w, "Error running convert command", http.StatusInternalServerError)
return
}

// Create the PUT request
req, err := http.NewRequest(config.DestinationHTTPMethod, message.Attachment.Content.DestinationURI, &outBuf)
if err != nil {
slog.Error("Error creating HTTP request", "err", err)
http.Error(w, "Error creating HTTP request", http.StatusInternalServerError)
return
}
if config.ForwardAuth {
auth := r.Header.Get("Authorization")
req.Header.Set("Authorization", auth)
}
req.Header.Set("Content-Type", message.Attachment.Content.MimeType)
req.Header.Set("Content-Location", message.Attachment.Content.FileUploadURI)

// Execute the PUT request
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
slog.Error("Error sending request", "method", config.DestinationHTTPMethod, "err", err)
http.Error(w, "Internal error.", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

if resp.StatusCode > 299 {
slog.Error("Request failed on destination server", "code", resp.StatusCode)
http.Error(w, fmt.Sprintf("%s request failed with status code %d", config.DestinationHTTPMethod, resp.StatusCode), resp.StatusCode)
http.Error(w, "Error running command", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
_, err = w.Write([]byte(""))
if err != nil {
slog.Error("Error writing response", "err", err)
}
}
Loading

0 comments on commit 320c0af

Please sign in to comment.