index.js 4.6 KB

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