post article to ipfs, and get from ipfs into article page working without being logged (headers conflict)

This commit is contained in:
arnaucode
2018-02-03 14:48:15 +01:00
parent 592bf9e8e8
commit 0c1c193645
5 changed files with 39 additions and 5 deletions

View File

@@ -3,9 +3,14 @@ package main
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"
)
@@ -152,6 +157,21 @@ func NewPost(w http.ResponseWriter, r *http.Request) {
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()

View File

@@ -4,5 +4,6 @@
"mongodb": {
"ip": "127.0.0.1:27017",
"database": "decentralized-blogging-platform"
}
},
"ipfsurl": "http://127.0.0.1:5001/api/v0/"
}

View File

@@ -12,6 +12,7 @@ import (
)
const keysDir = "keys"
const postsDir = "ownposts"
const keysize = 2048
const hashize = 1536
@@ -27,7 +28,7 @@ func main() {
//create models directory
_ = os.Mkdir(keysDir, os.ModePerm)
//create models directory
_ = os.Mkdir("ownposts", os.ModePerm)
_ = os.Mkdir(postsDir, os.ModePerm)
initializeToken()

View File

@@ -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"`

View File

@@ -18,12 +18,23 @@
<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