-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (90 loc) · 3.08 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"fmt"
"net/http"
orbitdb "berty.tech/go-orbit-db"
client "github.com/ipfs/go-ipfs-http-client"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// messenger is a component that does a simple messenger on ipfs. A component is a
// customizable, independent, and reusable UI element. It is created by
// embedding app.Compo into a struct.
type messenger struct {
app.Compo
message string
dbMessage string
}
func (m *messenger) OnMount(app.Context) {
initIPFS()
}
// The Render method is where the component appearance is defined. Here, a
// "messenger World!" is displayed as a heading.
func (m *messenger) Render() app.UI {
return app.Div().Class("input-group my-3").Body(
app.Input().Type("text").Class("form-control").Value(m.message).Placeholder("Store message").OnInput(m.OnStoreMessage),
app.Input().Type("text").Class("form-control").Value(m.dbMessage).Placeholder("DB message"),
app.Div().Class("input-group-append").Body(
app.Button().Class("btn btn-primary").Body(app.Text("Store Message")).OnClick(m.storeMessage),
),
)
}
func (m *messenger) OnStoreMessage(ctx app.Context, e app.Event) {
m.message = e.Get("target").Get("value").String()
m.Update()
}
func (m *messenger) storeMessage(ctx app.Context, e app.Event) {
}
func main() {
app.Route("/", &messenger{})
app.RunWhenOnBrowser()
initServer()
}
func initIPFS() {
c, err := client.NewURLApiWithClient("localhost:5001", &http.Client{})
if err != nil {
panic(fmt.Errorf("failed to connect to local api: %s", err))
}
fmt.Println(c)
/// ------ Setting up the IPFS Repo
/// --- Part I: Getting a IPFS node running
// fmt.Println("-- Getting an IPFS node running -- ")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// // Spawn a node using a temporary path, creating a temporary repo for the run
// fmt.Println("Spawning node on a temporary repo")
// ipfs, err := spawnEphemeral(ctx)
// if err != nil {
// panic(fmt.Errorf("failed to spawn ephemeral node: %s", err))
// }
// fmt.Println("IPFS node is running")
db, err := orbitdb.NewOrbitDB(ctx, c, &orbitdb.NewOrbitDBOptions{})
if err != nil {
panic(fmt.Errorf("failed to create new orbitdb: %s", err))
}
dbStore, err := db.Create(ctx, "test", "keyvalue", &orbitdb.CreateDBOptions{})
if err != nil {
panic(fmt.Errorf("failed to create new db store: %s", err))
}
fmt.Printf("dbStore address: %s\n", dbStore.Address())
KvStore, err = db.KeyValue(ctx, dbStore.Address().String(), &orbitdb.CreateDBOptions{})
if err != nil {
panic(fmt.Errorf("failed to get kv store: %s", err))
}
kvStorePut(ctx, "message", []byte("test1"))
v := kvStoreGet(ctx, "message")
fmt.Printf("kv store value: %s\n", v)
}
func kvStorePut(ctx context.Context, key string, val []byte) {
_, err := KvStore.Put(ctx, "message", []byte("test1"))
if err != nil {
panic(fmt.Errorf("failed to put in kv store: %s", err))
}
}
func kvStoreGet(ctx context.Context, key string) []byte {
v, err := KvStore.Get(ctx, "message")
if err != nil {
panic(fmt.Errorf("failed to get value from kv store: %s", err))
}
return v
}