Browse Source

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

master
arnaucode 6 years ago
parent
commit
0c1c193645
5 changed files with 39 additions and 5 deletions
  1. +20
    -0
      RESTfunctions.go
  2. +2
    -1
      config.json
  3. +2
    -1
      main.go
  4. +1
    -0
      readConfig.go
  5. +14
    -3
      webapp/views/post/post.html

+ 20
- 0
RESTfunctions.go

@ -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()

+ 2
- 1
config.json

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

+ 2
- 1
main.go

@ -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()

+ 1
- 0
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"`

+ 14
- 3
webapp/views/post/post.html

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

Loading…
Cancel
Save