-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
97 lines (81 loc) · 2.41 KB
/
server.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
package main
import (
"bytes"
"fmt"
"log/slog"
"net/http"
"os"
scyllaridae "github.com/lehigh-university-libraries/scyllaridae/internal/config"
"github.com/lehigh-university-libraries/scyllaridae/pkg/api"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Server struct {
Config *scyllaridae.ServerConfig
}
func runHTTPServer(server *Server) {
http.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
// Use the method as the handler
http.HandleFunc("/", server.MessageHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
slog.Info("Server listening", "port", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
panic(err)
}
}
func (s *Server) MessageHandler(w http.ResponseWriter, r *http.Request) {
slog.Info(r.RequestURI, "method", r.Method, "ip", r.RemoteAddr, "proto", r.Proto)
if r.Method != http.MethodGet && r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
if r.Header.Get("Apix-Ldp-Resource") == "" && r.Header.Get("X-Islandora-Event") == "" && r.Method == http.MethodGet {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// Read the Alpaca message payload
auth := ""
if s.Config.ForwardAuth {
auth = r.Header.Get("Authorization")
}
message, err := api.DecodeAlpacaMessage(r, auth)
if err != nil {
slog.Error("Error decoding alpaca message", "err", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}
cmd, err := scyllaridae.BuildExecCommand(message, s.Config)
if err != nil {
slog.Error("Error building command", "err", err)
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
// Stream the file contents from the source URL or request body
fs, errCode, err := s.Config.GetFileStream(r, message, auth)
if err != nil {
http.Error(w, cases.Title(language.English).String(fmt.Sprint(err)), errCode)
return
}
if fs != nil {
defer fs.Close()
cmd.Stdin = fs
}
// Create a buffer to capture stderr
var stdErr bytes.Buffer
cmd.Stderr = &stdErr
// Send stdout to the ResponseWriter stream
cmd.Stdout = w
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, "Internal error", http.StatusInternalServerError)
return
}
}