From 0c1c193645045a1502f0974b8f3de365fdec73bc Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 3 Feb 2018 14:48:15 +0100 Subject: [PATCH] post article to ipfs, and get from ipfs into article page working without being logged (headers conflict) --- RESTfunctions.go | 20 ++++++++++++++++++++ config.json | 3 ++- main.go | 3 ++- readConfig.go | 1 + webapp/views/post/post.html | 17 ++++++++++++++--- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/RESTfunctions.go b/RESTfunctions.go index d9cd704..9fd9afc 100644 --- a/RESTfunctions.go +++ b/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() diff --git a/config.json b/config.json index d0f2924..feed86b 100755 --- a/config.json +++ b/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/" } diff --git a/main.go b/main.go index 44378f1..67ad48a 100644 --- a/main.go +++ b/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() diff --git a/readConfig.go b/readConfig.go index 3632cfb..51a7af7 100755 --- a/readConfig.go +++ b/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"` diff --git a/webapp/views/post/post.html b/webapp/views/post/post.html index 3f512fd..d045071 100644 --- a/webapp/views/post/post.html +++ b/webapp/views/post/post.html @@ -18,12 +18,23 @@
+
+ +
+ {{post.date | date: "MMM d, y"}} +
+
+ +
+ {{post.date | date: "HH:mm"}}h +
+
+
{{post.title}}
-
{{post.subtitle}}
+
{{post.summary}}
-

{{post.content}}

-
+
37