Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 03102b8

Browse files
authored
Merge pull request #84 from aboodman/fix
Fix some rot in examples/randobj
2 parents 9c577e2 + 1342850 commit 03102b8

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

examples/api/randobj/randobj.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
8+
u "github.com/ipfs/go-ipfs-util"
9+
"github.com/ipfs/go-ipfs/core"
10+
"github.com/ipfs/go-ipfs/core/coreunix"
11+
"github.com/ipfs/go-ipfs/repo/fsrepo"
12+
)
13+
14+
var gnode *core.IpfsNode
15+
16+
func ServeIpfsRand(w http.ResponseWriter, r *http.Request) {
17+
read := io.LimitReader(u.NewTimeSeededRand(), 2048)
18+
19+
str, err := coreunix.Add(gnode, read)
20+
if err != nil {
21+
w.WriteHeader(504)
22+
w.Write([]byte(err.Error()))
23+
} else {
24+
w.Write([]byte(str))
25+
}
26+
}
27+
28+
func main() {
29+
r, err := fsrepo.Open("~/.ipfs")
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
// Make our 'master' context and defer cancelling it
35+
ctx, cancel := context.WithCancel(context.Background())
36+
defer cancel()
37+
38+
cfg := &core.BuildCfg{
39+
Repo: r,
40+
Online: true,
41+
}
42+
43+
node, err := core.NewNode(ctx, cfg)
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
// Set the global node for access in the handler
49+
gnode = node
50+
51+
http.HandleFunc("/ipfsobject", ServeIpfsRand)
52+
http.ListenAndServe(":8080", nil)
53+
}

examples/api/randobj/readme.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ First, lets get some imports:
99
package main
1010
1111
import (
12+
"context"
1213
"io"
1314
"net/http"
14-
"os"
1515
16+
u "github.com/ipfs/go-ipfs-util"
1617
"github.com/ipfs/go-ipfs/core"
1718
"github.com/ipfs/go-ipfs/core/coreunix"
1819
"github.com/ipfs/go-ipfs/repo/fsrepo"
19-
u "github.com/ipfs/go-ipfs/util"
20-
21-
"code.google.com/p/go.net/context"
2220
)
2321
```
2422

0 commit comments

Comments
 (0)