diff --git a/.gitignore b/.gitignore index 0bdfd49..752fb54 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ keys +ownposts diff --git a/RESTfunctions.go b/RESTfunctions.go index 4a7e0b0..d9cd704 100644 --- a/RESTfunctions.go +++ b/RESTfunctions.go @@ -4,16 +4,32 @@ import ( "encoding/json" "fmt" "net/http" + "time" + "github.com/gorilla/mux" "gopkg.in/mgo.v2/bson" ) +type Post struct { + ID bson.ObjectId `json:"id" bson:"_id,omitempty"` + Title string `json:"title"` + Summary string `json:"summary"` + Date time.Time `json:"date"` + ThumbImg string `json:"thumbimg"` + Content string `json:"content"` + User User `json:"user"` +} type User struct { - Id bson.ObjectId `json:"id" bson:"_id,omitempty"` - Username string `json:"username"` - Email string `json:"email"` - Password string `json:"password"` - Token string `json:"token"` + ID bson.ObjectId `json:"id" bson:"_id,omitempty"` + Username string `json:"username"` + Name string `json:"name"` + LastName string `json:"lastname"` + Twitter string `json:"twitter" bson:",omitempty"` + Description string `json:"description" bson:",omitempty"` + Email string `json:"email" bson:",omitempty"` + Password string `json:"password" bson:",omitempty"` + Token string `json:"token" bson:",omitempty"` + Posts []Post `json:"posts" bson:",omitempty"` } func Index(w http.ResponseWriter, r *http.Request) { @@ -85,7 +101,7 @@ func Login(w http.ResponseWriter, r *http.Request) { rUser.Token = token //update with the token - err = userCollection.Update(bson.M{"_id": rUser.Id}, rUser) + err = userCollection.Update(bson.M{"_id": rUser.ID}, rUser) check(err) jResp, err := json.Marshal(rUser) @@ -94,3 +110,91 @@ func Login(w http.ResponseWriter, r *http.Request) { } fmt.Fprintln(w, string(jResp)) } + +func GetUsers(w http.ResponseWriter, r *http.Request) { + //get projects from mongodb + users := []User{} + err := userCollection.Find(bson.M{}).Limit(50).All(&users) + check(err) + + jResp, err := json.Marshal(users) + check(err) + fmt.Fprintln(w, string(jResp)) +} +func GetUser(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + userid := vars["userid"] + + //get projects from mongodb + user := User{} + err := userCollection.Find(bson.M{"_id": bson.ObjectIdHex(userid)}).One(&user) + check(err) + + err = postCollection.Find(bson.M{"user._id": bson.ObjectIdHex(userid)}).Limit(50).All(&user.Posts) + check(err) + + jResp, err := json.Marshal(user) + check(err) + fmt.Fprintln(w, string(jResp)) +} +func NewPost(w http.ResponseWriter, r *http.Request) { + decoder := json.NewDecoder(r.Body) + var post Post + err := decoder.Decode(&post) + if err != nil { + panic(err) + } + defer r.Body.Close() + + //get user by token + usertoken := r.Header.Get("Authorization") + user := User{} + err = userCollection.Find(bson.M{"token": usertoken}).One(&user) + check(err) + + //add date to post + post.Date = time.Now() + + //add user.id to post.user + post.User.ID = user.ID + err = postCollection.Insert(post) + check(err) + + //get user + //user = User{} + err = userCollection.Find(bson.M{"token": usertoken}).One(&user) + check(err) + + jResp, err := json.Marshal(user) + if err != nil { + panic(err) + } + fmt.Fprintln(w, string(jResp)) +} + +func GetPosts(w http.ResponseWriter, r *http.Request) { + //get projects from mongodb + posts := []Post{} + err := postCollection.Find(bson.M{}).Limit(50).All(&posts) + check(err) + + jResp, err := json.Marshal(posts) + check(err) + fmt.Fprintln(w, string(jResp)) +} +func GetPost(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + postid := vars["postid"] + + //get projects from mongodb + post := Post{} + err := postCollection.Find(bson.M{"_id": bson.ObjectIdHex(postid)}).One(&post) + check(err) + + err = userCollection.Find(bson.M{"_id": post.User.ID}).One(&post.User) + check(err) + + jResp, err := json.Marshal(post) + check(err) + fmt.Fprintln(w, string(jResp)) +} diff --git a/main.go b/main.go index ca1e809..44378f1 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "os" "github.com/fatih/color" "github.com/gorilla/handlers" @@ -15,6 +16,7 @@ const keysize = 2048 const hashize = 1536 var userCollection *mgo.Collection +var postCollection *mgo.Collection func main() { color.Blue("Starting ipfs-ai-models-market") @@ -22,12 +24,18 @@ func main() { readConfig("config.json") fmt.Println(config) + //create models directory + _ = os.Mkdir(keysDir, os.ModePerm) + //create models directory + _ = os.Mkdir("ownposts", os.ModePerm) + initializeToken() //mongodb session, err := getSession() check(err) userCollection = getCollection(session, "users") + postCollection = getCollection(session, "posts") //run thw webserver go GUI() @@ -37,7 +45,7 @@ func main() { log.Print("port: ") log.Println(config.APIPort) router := NewRouter() - headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin"}) + headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Access-Control-Allow-Origin", "Authorization"}) originsOk := handlers.AllowedOrigins([]string{"*"}) methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) log.Fatal(http.ListenAndServe(":"+config.APIPort, handlers.CORS(originsOk, headersOk, methodsOk)(router))) diff --git a/restRoutes.go b/restRoutes.go index e053e94..9850d20 100755 --- a/restRoutes.go +++ b/restRoutes.go @@ -21,4 +21,34 @@ var routes = Routes{ "/login", Login, }, + Route{ + "GetUsers", + "GET", + "/users", + GetUsers, + }, + Route{ + "GetUser", + "GET", + "/user/{userid}", + GetUser, + }, + Route{ + "NewPost", + "POST", + "/post", + NewPost, + }, + Route{ + "GetPosts", + "GET", + "/posts", + GetPosts, + }, + Route{ + "GetPost", + "GET", + "/post/{postid}", + GetPost, + }, } diff --git a/webapp/app.js b/webapp/app.js index 223b490..67db7a5 100644 --- a/webapp/app.js +++ b/webapp/app.js @@ -12,6 +12,7 @@ angular.module('app', [ 'app.login', 'app.main', 'app.newmodel', + 'app.users', 'app.user', 'app.post', 'app.write' @@ -37,7 +38,7 @@ console.log("window", window.location.hash); localStorage.removeItem('dblog_user'); localStorage.removeItem('dblog_user'); - window.location='#!/main'; + //window.location='#!/main'; $routeProvider.otherwise({redirectTo: '/main'}); } } @@ -76,6 +77,7 @@ console.log("window", window.location.hash); .factory('api', function($http) { return { init: function() { + console.log("http", $http.options); var dblog_user = JSON.parse(localStorage.getItem('dblog_user')); if (dblog_user) { $http.defaults.headers.common['Authorization'] = dblog_user.token; @@ -86,4 +88,12 @@ console.log("window", window.location.hash); }) .run(function(api) { api.init(); - }); + }) + .config(function($sceDelegateProvider) { + $sceDelegateProvider.resourceUrlWhitelist([ + // Allow same origin resource loads. + 'self', + // Allow loading from outer templates domain. + 'http://localhost:8080/**' + ]); +}); diff --git a/webapp/css/own.css b/webapp/css/own.css index 587b0d3..e598f4f 100644 --- a/webapp/css/own.css +++ b/webapp/css/own.css @@ -32,7 +32,7 @@ a, a:hover { } .o_userImgCircular { - width: 60px; + width: 40px; background: #ffffff; border-radius: 150px; -webkit-border-radius: 150px; @@ -46,3 +46,9 @@ a, a:hover { width: 50%; width: 50%; } + +.o_card-body { + padding-left:1.25rem; + padding-right:1.25rem; + padding-top:0.8rem; +} diff --git a/webapp/fake_data.js b/webapp/fake_data.js index b1ad494..e7b4bb2 100644 --- a/webapp/fake_data.js +++ b/webapp/fake_data.js @@ -9,25 +9,37 @@ var user = { title: "This is the second post", subtitle: "this is the subtitle of the second post", img: "https://cdn-images-1.medium.com/fit/t/800/240/1*4_E6m7J0112DBi1Lmdniiw.png", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "This is the first post", subtitle: "this is the subtitle of the first post", img: "https://bootstrap-themes.github.io/application/assets/img/unsplash_1.jpg", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "Thinking about python development", subtitle: "this is the subtitle of the second post", img: "https://cdn.static-economist.com/sites/default/files/images/2015/09/blogs/economist-explains/code2.png", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "Thinking about G", subtitle: "this is the subtitle of the first post", img: "https://cdn-images-1.medium.com/max/1600/1*RNkyx-Zq7w61eR74nMYgnA.jpeg", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } } ] }; @@ -35,13 +47,19 @@ var featured_posts = [{ title: "Thinking about python development", subtitle: "this is the subtitle of the second post", img: "https://cdn.static-economist.com/sites/default/files/images/2015/09/blogs/economist-explains/code2.png", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "Thinking about G", subtitle: "this is the subtitle of the first post", img: "https://cdn-images-1.medium.com/max/1600/1*RNkyx-Zq7w61eR74nMYgnA.jpeg", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } } ]; @@ -49,13 +67,19 @@ var posts = [{ title: "This is the second post", subtitle: "this is the subtitle of the second post", img: "https://cdn-images-1.medium.com/fit/t/800/240/1*4_E6m7J0112DBi1Lmdniiw.png", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "This is the first post", subtitle: "this is the subtitle of the first post", img: "https://bootstrap-themes.github.io/application/assets/img/unsplash_1.jpg", - content: "Some quick example text to build on the card title and make up the bulk of the card's content." + content: "Some quick example text to build on the card title and make up the bulk of the card's content.", + user: { + id: "5a732c952f009b384d5effb7" + } }, { title: "Thinking about python development", diff --git a/webapp/index.html b/webapp/index.html index e50ea14..eed0387 100644 --- a/webapp/index.html +++ b/webapp/index.html @@ -68,6 +68,7 @@ Works for both browser and electron with the same code --> + diff --git a/webapp/views/login/login.html b/webapp/views/login/login.html index 04e91f5..90d2a0c 100755 --- a/webapp/views/login/login.html +++ b/webapp/views/login/login.html @@ -1,4 +1,4 @@ -
+
diff --git a/webapp/views/main/main.js b/webapp/views/main/main.js index ffe5743..d36b0e4 100755 --- a/webapp/views/main/main.js +++ b/webapp/views/main/main.js @@ -12,8 +12,14 @@ angular.module('app.main', ['ngRoute']) .controller('MainCtrl', function($scope, $rootScope, $http) { - $scope.user = user; - $scope.featured_posts= featured_posts; - $scope.posts = posts; + $scope.posts = {}; + $http.get(apiurl + 'posts') + .then(function(data) { + console.log('data success'); + console.log(data); + $scope.posts = data.data; + }, function(data) { + console.log('no user'); + }); }); diff --git a/webapp/views/navbar.html b/webapp/views/navbar.html index 8a37bac..47fafb7 100755 --- a/webapp/views/navbar.html +++ b/webapp/views/navbar.html @@ -1,5 +1,5 @@
-