some changes
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package invoke
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.oblat.lv/alex/triggerssmith/internal/config"
|
||||
@@ -22,6 +25,46 @@ type Function struct {
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
type TerminalLogger struct {
|
||||
fc *worker.FuncConfig
|
||||
}
|
||||
|
||||
func (l *TerminalLogger) Write(line string) {
|
||||
slog.Warn("function stderr", slog.String("line", line), slog.String("n:v", fmt.Sprintf("%s:%s", l.fc.Name, l.fc.Version)))
|
||||
}
|
||||
|
||||
type JSONFileLogger struct {
|
||||
fc *worker.FuncConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewJSONFileLogger(fc *worker.FuncConfig, path string) (*JSONFileLogger, error) {
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
handler := slog.NewJSONHandler(file, &slog.HandlerOptions{})
|
||||
logger := slog.New(handler)
|
||||
|
||||
return &JSONFileLogger{
|
||||
fc: fc,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *JSONFileLogger) Write(line string) {
|
||||
l.logger.Warn("function stderr",
|
||||
slog.String("function", l.fc.Name),
|
||||
slog.String("version", l.fc.Version),
|
||||
slog.String("line", line),
|
||||
)
|
||||
}
|
||||
|
||||
func InvokeHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "function_id")
|
||||
@@ -49,8 +92,46 @@ func InvokeHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
var logger worker.Logger
|
||||
switch fc.Log.Output {
|
||||
case "stdout":
|
||||
logger = &TerminalLogger{
|
||||
fc: fc,
|
||||
}
|
||||
case "file":
|
||||
fileLogger, err := NewJSONFileLogger(fc, filepath.Join(treeCfg.Log.Path, fmt.Sprintf("%s:%s", fc.Name, f.Path), "event.log.json"))
|
||||
if err != nil {
|
||||
slog.Error("Failed to create file logger", slog.String("err", err.Error()))
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
logger = fileLogger
|
||||
}
|
||||
|
||||
var frmt = func(s1 string, s2 string) string {
|
||||
return fmt.Sprintf("FAAS_%s=%s", s1, s2)
|
||||
}
|
||||
var env = []string{
|
||||
frmt("PROTOCOL", r.Proto),
|
||||
|
||||
frmt("METHOD", r.Method),
|
||||
frmt("PATH", r.URL.Path),
|
||||
frmt("QUERY", r.URL.RawQuery),
|
||||
}
|
||||
|
||||
for k, v := range r.Header {
|
||||
key := "FAAS_HEADER_" + strings.ReplaceAll(k, "-", "_")
|
||||
env = append(env, key+"="+v[0])
|
||||
}
|
||||
|
||||
input, _ := io.ReadAll(r.Body)
|
||||
output, err := worker.RunFunction(filepath.Join(root, f.FunctionName, f.Path, fc.Entry), fc, input)
|
||||
path := filepath.Join(root, f.FunctionName, f.Path, fc.Entry)
|
||||
output, err := worker.RunFunction(&worker.RunOps{
|
||||
Path: path,
|
||||
FuncConfig: fc,
|
||||
Log: logger,
|
||||
Env: env,
|
||||
}, input)
|
||||
if err != nil {
|
||||
slog.Error("Failed to run function", slog.String("err", err.Error()))
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
@@ -42,6 +42,6 @@ func (r *Router) RouteHandler() chi.Router {
|
||||
})
|
||||
w.Write([]byte(b))
|
||||
})
|
||||
r.r.Handle("/i/invoke/function/{function_id}/{function_version}", invoke.InvokeHandler(r.cfg))
|
||||
r.r.Handle("/invoke/function/{function_id}/{function_version}", invoke.InvokeHandler(r.cfg))
|
||||
return r.r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user