arnaucube
arnaucube/decentralized-blogging-platformPublic Self-hosted repository
post article to ipfs, and get from ipfs into article page working without being logged (headers conflict)
arnaucode arnaucode@gmail.com
Sat, 03 Feb 2018 14:48:15 +0100
commit

0c1c193645045a1502f0974b8f3de365fdec73bc

parent

592bf9e8e8e7ed902fb96826be0ea7221c5d387d

5 files changed, 39 insertions(+), 5 deletions(-)

jump to
M RESTfunctions.go → RESTfunctions.go

@@ -3,9 +3,14 @@

import ( "encoding/json" "fmt" + "io/ioutil" + "log" "net/http" + "os/exec" + "strings" "time" + "github.com/fatih/color" "github.com/gorilla/mux" "gopkg.in/mgo.v2/bson" )

@@ -151,6 +156,21 @@ usertoken := r.Header.Get("Authorization")

user := User{} err = userCollection.Find(bson.M{"token": usertoken}).One(&user) check(err) + + //save the post.Content into an html file + err = ioutil.WriteFile(postsDir+"/"+"file.html", []byte(post.Content), 0644) + check(err) + //add the html file to ipfs + out, err := exec.Command("bash", "-c", "ipfs add "+postsDir+"/file.html").Output() + if err != nil { + log.Fatal(err) + } + fmt.Println(out) + hash := strings.Split(string(out), " ")[1] + color.Blue(hash) + + //save the hash to the post.content + post.Content = hash //add date to post post.Date = time.Now()
M config.json → config.json

@@ -4,5 +4,6 @@ "webport": "8081",

"mongodb": { "ip": "127.0.0.1:27017", "database": "decentralized-blogging-platform" - } + }, + "ipfsurl": "http://127.0.0.1:5001/api/v0/" }
M main.go → main.go

@@ -12,6 +12,7 @@ mgo "gopkg.in/mgo.v2"

) const keysDir = "keys" +const postsDir = "ownposts" const keysize = 2048 const hashize = 1536

@@ -27,7 +28,7 @@

//create models directory _ = os.Mkdir(keysDir, os.ModePerm) //create models directory - _ = os.Mkdir("ownposts", os.ModePerm) + _ = os.Mkdir(postsDir, os.ModePerm) initializeToken()
M readConfig.go → readConfig.go

@@ -10,6 +10,7 @@ type Config struct {

APIPort string `json:"apiport"` WebPort string `json:"webport"` Mongodb MongoConfig `json:"mongodb"` + IPFSurl string `json:"ipfsurl"` } type MongoConfig struct { IP string `json:"ip"`
M webapp/views/post/post.html → webapp/views/post/post.html

@@ -18,12 +18,23 @@ </div>

<div class="col-sm-9"> <div class="card"> <div class="card-body"> + <div class="pull-right text-secondary"> + <span class="row"> + <div class="text-right"> + {{post.date | date: "MMM d, y"}} + </div> + </span> + <span class="row"> + <div class="text-right"> + {{post.date | date: "HH:mm"}}h + </div> + </span> + </div> <h5 class="card-title">{{post.title}}</h5> - <h6 class="card-subtitle mb-2 text-muted">{{post.subtitle}}</h6> + <h6 class="card-subtitle mb-2 text-muted">{{post.summary}}</h6> <img ng-src="{{post.img}}" class="img-fluid" /> - <p class="card-text">{{post.content}}</p> - <div ng-include="'http://localhost:8080/ipfs/QmUv3dQuNREHnEFYs7JkqyxZjYfEXd4t9jej5jY2dPVaqU'"></div> + <div ng-include="'http://localhost:8080/ipfs/'+post.content"></div> <div class="pull-right"> <i title="Server" class="fa fa-heart ct_red300 fa-1x"></i> 37