@cryptotaxi247 / kubo / commits / 9ff4dae26

gateway server

initialize if not already initialized remove gateway fs add gateway server

Brian Tiger Chow committed Jan 24, 2015 at 02:57 UTC 9ff4dae26458d32f83c4018562af705fba6be748
1 file changed +101
cmd/ipfs-gateway-fs/main.go new
+101
@@ -0,0 +1,101 @@
1 +package main
2 +
3 +import (
4 + "errors"
5 + "flag"
6 + "log"
7 + "os"
8 + "time"
9 +
10 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11 + core "github.com/jbenet/go-ipfs/core"
12 + corehttp "github.com/jbenet/go-ipfs/core/corehttp"
13 + corerepo "github.com/jbenet/go-ipfs/core/corerepo"
14 + coreunix "github.com/jbenet/go-ipfs/core/coreunix"
15 + config "github.com/jbenet/go-ipfs/repo/config"
16 + fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
17 +)
18 +
19 +var (
20 + refreshAssetsInterval = flag.Duration("refresh-assets-interval", 30*time.Second, "refresh assets")
21 + garbageCollectInterval = flag.Duration("gc-interval", 24*time.Hour, "frequency of repo garbage collection")
22 + assetsPath = flag.String("assets-path", "", "if provided, periodically adds contents of path to IPFS")
23 + host = flag.String("host", "/ip4/0.0.0.0/tcp/8080", "override the HTTP host listening address")
24 + nBitsForKeypair = flag.Int("b", 1024, "number of bits for keypair (if repo is uninitialized)")
25 +)
26 +
27 +func main() {
28 + flag.Parse()
29 + if *assetsPath == "" {
30 + log.Println("asset-path not provided. hosting gateway without file server functionality...")
31 + }
32 + if err := run(); err != nil {
33 + log.Println(err)
34 + }
35 +}
36 +
37 +func run() error {
38 + ctx, cancel := context.WithCancel(context.Background())
39 + defer cancel()
40 + repoPath, err := fsrepo.BestKnownPath()
41 + if err != nil {
42 + return err
43 + }
44 +
45 + if !fsrepo.IsInitialized(repoPath) {
46 + conf, err := config.Init(*nBitsForKeypair)
47 + if err != nil {
48 + return err
49 + }
50 + if err := fsrepo.Init(repoPath, conf); err != nil {
51 + return err
52 + }
53 + }
54 +
55 + repo := fsrepo.At(repoPath)
56 + if err := repo.Open(); err != nil { // owned by node
57 + return err
58 + }
59 +
60 + node, err := core.NewIPFSNode(ctx, core.Online(repo))
61 + if err != nil {
62 + return err
63 + }
64 + defer node.Close()
65 +
66 + go func() {
67 + for _ = range time.Tick(*garbageCollectInterval) {
68 + if err := corerepo.GarbageCollect(node, ctx); err != nil {
69 + log.Println("failed to run garbage collection", err)
70 + }
71 + }
72 + }()
73 +
74 + if *assetsPath != "" {
75 + fi, err := os.Stat(*assetsPath)
76 + if err != nil {
77 + return err
78 + }
79 + if !fi.IsDir() {
80 + return errors.New("asset path must be a directory")
81 + }
82 + go func() {
83 + for _ = range time.Tick(*refreshAssetsInterval) {
84 + _, err := coreunix.AddR(node, *assetsPath)
85 + if err != nil {
86 + log.Println(err)
87 + }
88 + }
89 + }()
90 + }
91 +
92 + opts := []corehttp.ServeOption{
93 + corehttp.GatewayOption,
94 + }
95 + if err := corehttp.ListenAndServe(node, *host, opts...); err != nil {
96 + return err
97 + }
98 +
99 + // TODO serve files
100 + return nil
101 +}