index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. const jwtSecret = 'AjnjLhjxM'
  2. const express = require('express');
  3. const express_graphql = require('express-graphql');
  4. const { buildSchema, printSchema } = require('graphql');
  5. const expand = require('mm-graphql/expand')
  6. const fs = require('fs')
  7. const uploadPath = `${__dirname}/public/images/`;
  8. const upload = require('multer')({ dest: uploadPath })
  9. ;(async () => {
  10. const {Savable, slice, getModels} = await require('./models.js')()
  11. const { jwtGQL, jwtCheck } = require('mm-graphql/jwt')
  12. const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
  13. let schema = buildSchema(`
  14. type User {
  15. _id: String
  16. createdAt: String
  17. login: String
  18. nick : String
  19. avatar: Image
  20. likes: [Like]
  21. likesCount: Int
  22. incomings: [Direct]
  23. followers: [User]
  24. following: [User]
  25. }
  26. input UserInput {
  27. _id: String
  28. login: String
  29. nick : String
  30. avatar: ImageInput
  31. following: [UserInput]
  32. }
  33. type Like {
  34. _id: ID,
  35. post: Post,
  36. comment: Comment,
  37. direct: Direct,
  38. owner: User
  39. }
  40. input LikeInput {
  41. _id: ID,
  42. post: PostInput,
  43. comment: CommentInput,
  44. direct: DirectInput,
  45. user: UserInput,
  46. }
  47. type Post {
  48. _id: ID,
  49. createdAt: String
  50. title: String
  51. text: String,
  52. images: [Image],
  53. comments: [Comment],
  54. directs: [Direct],
  55. collections: [Collection]
  56. likes: [Like]
  57. likesCount: Int
  58. owner: User
  59. }
  60. input PostInput {
  61. _id: ID,
  62. title: String,
  63. text: String,
  64. images: [ImageInput],
  65. comments: [CommentInput],
  66. directs: [DirectInput],
  67. collections: [CollectionInput]
  68. }
  69. type Image {
  70. _id: ID,
  71. text: String,
  72. url: String,
  73. originalFileName: String,
  74. userAvatar: User,
  75. posts: [Post],
  76. directs: [Direct]
  77. owner: User
  78. }
  79. input ImageInput {
  80. _id: ID,
  81. text: String,
  82. userAvatar: UserInput,
  83. posts: [PostInput],
  84. directs: [DirectInput]
  85. }
  86. type Comment {
  87. _id: ID,
  88. createdAt: String,
  89. text: String,
  90. post: Post,
  91. answers: [Comment],
  92. answerTo: Comment
  93. likes: [Like]
  94. likesCount: Int
  95. owner: User
  96. }
  97. input CommentInput {
  98. _id: ID,
  99. text: String,
  100. post: PostInput,
  101. answers: [CommentInput],
  102. answerTo: CommentInput
  103. }
  104. type Direct {
  105. _id: ID,
  106. createdAt: String,
  107. text: String,
  108. post: Post,
  109. image: Image,
  110. likes: [Like]
  111. likesCount: Int
  112. to: User
  113. owner: User
  114. }
  115. input DirectInput {
  116. _id: ID,
  117. text: String,
  118. post: PostInput,
  119. image: ImageInput,
  120. likes: [LikeInput]
  121. to: UserInput
  122. }
  123. type Collection {
  124. _id: ID,
  125. text: String,
  126. posts: [Post]
  127. owner: User
  128. }
  129. input CollectionInput {
  130. _id: ID,
  131. text: String,
  132. posts: [PostInput]
  133. }
  134. `);
  135. schema = expand(schema)
  136. console.log(printSchema(schema))
  137. const app = express();
  138. app.use(require('cors')())
  139. app.use(express.static('public'));
  140. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  141. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  142. let decoded;
  143. console.log('wtf')
  144. if (decoded = jwtCheck(req, jwtSecret)){
  145. console.log('SOME UPLOAD', decoded, req.file)
  146. let {models: {Image }} = await getModels(decoded.sub)
  147. let image = await Image.fromFileData(req.file)
  148. res.end(JSON.stringify({_id: image._id, url: image.url}))
  149. }
  150. else {
  151. res.status(503).send('permission denied')
  152. }
  153. })
  154. app.use(express.static('public'));
  155. let socketPath = "/home/asmer/node_hosts/hipstagram"
  156. app.listen(socketPath, () => {
  157. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  158. fs.chmodSync(socketPath, '777');
  159. });
  160. })()