Basic demo for Tally X AutoIxpert integration to showcase setup in Go due to impossibility of open-sourcing n8n setup.
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
|
|
"git.kornbrand.biz/plankalkul/autoixpert-sample/pkg/models"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string `json:"port"`
|
|
APIBase string `json:"api_base"`
|
|
APIKey string `json:"api_key"`
|
|
SecretKey string `json:"secret_key"`
|
|
}
|
|
|
|
var cfg Config
|
|
|
|
type imageWithMeta struct {
|
|
Data []byte
|
|
Meta models.ImageMetaRequest
|
|
}
|
|
|
|
func main() {
|
|
|
|
slog.Info("Starting AutoIxpert Sample App")
|
|
|
|
config, err := os.Open("config.json")
|
|
if err != nil {
|
|
slog.Error("Failed to open config file", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
defer config.Close()
|
|
|
|
if err := json.NewDecoder(config).Decode(&cfg); err != nil {
|
|
slog.Error("Failed to decode config file", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
http.HandleFunc("POST /", handle)
|
|
|
|
slog.Info("Server is listening on port " + cfg.Port)
|
|
if err := http.ListenAndServe(cfg.Port, nil); err != nil {
|
|
slog.Error("Listen and serve failed", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
slog.Info("Received request on /")
|
|
|
|
defer r.Body.Close()
|
|
|
|
header := r.Header.Get("X-API-Key")
|
|
if header != cfg.SecretKey {
|
|
slog.Warn("Unauthorized request received")
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
var req models.Webhook
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
slog.Error("Failed to decode request body", "error", err)
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
report := req.ToCrashReport()
|
|
|
|
id, err := createReport(report)
|
|
if err != nil {
|
|
slog.Error("Failed to create report", "eventID", report.EventID, "error", err)
|
|
http.Error(w, "failed to create report", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = uploadImages(report.Images, id, report.EventID)
|
|
if err != nil {
|
|
slog.Error("Failed to upload images", "eventID", report.EventID, "error", err)
|
|
http.Error(w, "failed to upload images", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|