wasmcloud/cmd/wasmcloudd/handler.go

243 lines
4.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/go-interpreter/wagon/wasm"
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 invokeHandlerSync(w http.ResponseWriter, r *http.Request) {
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)
}
resp, err := runHandler(ctx, hdlr, 5*time.Minute, data)
if err != nil {
ln.Error(ctx, err)
return
}
logData := txtar.Format(&resp.Logs)
entry := ExecutionLog{
HandlerID: hdlr.ID,
RunID: resp.UUID,
Data: logData,
}
err = db.Save(&entry).Error
if err != nil {
ln.Error(ctx, err)
return
}
ln.Log(ctx, ln.Action("saving-logs"))
for _, file := range resp.Logs.Files {
if file.Name == "stdout.txt" {
w.Write(file.Data)
return
}
}
}
func invokeHandler(w http.ResponseWriter, r *http.Request) {
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)
}
go func() {
resp, err := runHandler(ctx, hdlr, 5*time.Minute, data)
if err != nil {
ln.Error(ctx, err)
return
}
data := txtar.Format(&resp.Logs)
entry := ExecutionLog{
HandlerID: hdlr.ID,
RunID: resp.UUID,
Data: data,
}
err = db.Save(&entry).Error
if err != nil {
ln.Error(ctx, err)
return
}
ln.Log(ctx, ln.Action("saving-logs"))
}()
}