wasmcloud/cmd/wasmcloudd/handler.go

223 lines
4.5 KiB
Go

package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/go-interpreter/wagon/wasm"
"github.com/google/uuid"
shell "github.com/ipfs/go-ipfs-api"
"github.com/rogpeppe/go-internal/txtar"
"tulpa.dev/within/wasmcloud/cmd/internal"
"within.website/ln"
"within.website/olin/namegen"
)
func createHandler(w http.ResponseWriter, r *http.Request, u *User) {
if !u.CanCreateHandlers {
http.Error(w, "you can't create handlers, contact support", http.StatusForbidden)
return
}
ctx := r.Context()
sh := shell.NewShell(*ipfsURL)
data, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, 16*1024*1024))
if err != nil {
ln.Error(ctx, err)
http.Error(w, "invalid data", http.StatusBadRequest)
return
}
r.Body.Close()
var hdlr internal.Handler
err = json.Unmarshal(data, &hdlr)
if err != nil {
ln.Error(ctx, err)
http.Error(w, "not json", http.StatusBadRequest)
return
}
switch string(hdlr.ABI) {
case string(internal.CWA), string(internal.Dagger):
default:
ln.Log(ctx, ln.Info("unknown ABI"), ln.F{"abi": hdlr.ABI})
http.Error(w, "unknown ABI", http.StatusBadRequest)
return
}
buf := bytes.NewBuffer(hdlr.WASM)
_, err = wasm.DecodeModule(buf)
if err != nil {
ln.Error(ctx, err)
http.Error(w, "not webassembly", http.StatusBadRequest)
}
cid, err := sh.Add(bytes.NewBuffer(hdlr.WASM))
if err != nil {
ln.Error(ctx, err)
http.Error(w, "can't upload wasm to storage", http.StatusInternalServerError)
return
}
n := namegen.Next()
h := Handler{
Name: n,
User: *u,
UserID: u.ID,
Path: cid,
}
if err := db.Save(&h).Error; err != nil {
ln.Error(ctx, err)
http.Error(w, "can't save record", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(h)
}
func listHandlers(w http.ResponseWriter, r *http.Request, u *User) {
ctx := r.Context()
var hdlrs []Handler
err := db.Where("user_id = ?", u.ID).Scan(&hdlrs).Error
if err != nil {
ln.Error(ctx, err)
http.Error(w, "can't read handlers", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(hdlrs)
}
func getLogs(w http.ResponseWriter, r *http.Request, u *User) {
ctx := r.Context()
q := r.URL.Query()
name := q.Get("name")
if name == "" {
http.NotFound(w, r)
return
}
var hdlr Handler
err := db.Where("name = ? AND user_id = ?", name, u.ID).First(&hdlr).Error
if err != nil {
ln.Error(ctx, err)
http.NotFound(w, r)
return
}
var elogs []ExecutionLog
err = db.Where("handler_id = ?", hdlr.ID).Find(&elogs).Error
if err != nil {
ln.Error(ctx, err)
http.Error(w, "handler id not found", http.StatusInternalServerError)
return
}
type resultLine struct {
ExecID string `json:"exec_id"`
Comment string `json:"comment"`
Logs map[string]string `json:"logs"`
}
var result []resultLine
for _, elog := range elogs {
arc := txtar.Parse(elog.Data)
logs := map[string]string{}
for _, file := range arc.Files {
logs[file.Name] = string(file.Data)
}
result = append(result, resultLine{
ExecID: elog.RunID,
Comment: string(arc.Comment),
Logs: logs,
})
}
json.NewEncoder(w).Encode(result)
}
func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) {
ctx := r.Context()
q := r.URL.Query()
name := q.Get("name")
if name == "" {
http.NotFound(w, r)
return
}
var hdlr Handler
err := db.Where("name = ?", name).First(&hdlr).Error
if err != nil {
ln.Error(ctx, err)
http.NotFound(w, r)
return
}
data, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, 1*1024*1024))
if err != nil {
ln.Error(ctx, err)
http.NotFound(w, r)
}
execID := uuid.New().String()
er := internal.ExecRequest{
WASMCID: hdlr.Path,
Name: hdlr.Name,
Data: data,
Env: map[string]string{
"RUN_ID": execID,
},
UUID: execID,
}
encData, err := json.Marshal(er)
if err != nil {
ln.Error(ctx, err)
http.NotFound(w, r)
}
go func() {
f := ln.F{
"name": name,
"run_id": execID,
}
ln.Log(ctx, ln.Action("invoke-handler"), f)
msg, err := nc.Request(internal.TopicName, encData, 5*time.Minute)
if err != nil {
ln.Error(ctx, err)
return
}
var resp internal.ExecResponse
err = json.Unmarshal(msg.Data, &resp)
if err != nil {
ln.Error(ctx, err)
return
}
data := txtar.Format(&resp.Logs)
entry := ExecutionLog{
HandlerID: hdlr.ID,
RunID: execID,
Data: data,
}
err = db.Save(&entry).Error
if err != nil {
ln.Error(ctx, err)
return
}
ln.Log(ctx, ln.Action("saving-logs"), f)
}()
}