-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (47 loc) · 1.01 KB
/
Copy pathmain.go
File metadata and controls
60 lines (47 loc) · 1.01 KB
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
package main
import "C"
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"github.com/gorilla/mux"
shell "github.com/ipfs/go-ipfs-api"
)
func main() {
host := "localhost:5001"
if os.Getenv("INSIDE_DOCKER") == "true" {
host = "host.docker.internal:5001"
}
r := mux.NewRouter()
r.HandleFunc("/{ipfsRef}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
sh := shell.NewShell(host)
o, err := sh.Cat(vars["ipfsRef"])
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(o)
if err != nil {
fmt.Println(err)
}
tmpfile, err := ioutil.TempFile("", "ipswarm.*.wasm")
if err != nil {
log.Println(err)
}
defer tmpfile.Close()
if _, err := tmpfile.Write(body); err != nil {
log.Println(err)
}
cmd := exec.Command("./wasmtime", tmpfile.Name())
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
}
fmt.Fprintf(w, string(stdoutStderr))
})
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":8585", nil))
}