index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const jwtSecret = 'OLX'
  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. incomings: [Message]
  21. phones: [String]
  22. addresses: [String]
  23. }
  24. input UserInput {
  25. _id: String
  26. login: String
  27. nick : String
  28. avatar: ImageInput
  29. phones: [String]
  30. addresses: [String]
  31. }
  32. type Image {
  33. _id: ID,
  34. text: String,
  35. createdAt: String
  36. url: String,
  37. originalFileName: String,
  38. userAvatar: User,
  39. ad: Ad,
  40. message: Message
  41. owner: User
  42. }
  43. input ImageInput {
  44. _id: ID,
  45. createdAt: String
  46. text: String,
  47. userAvatar: UserInput
  48. }
  49. type Ad {
  50. _id: ID,
  51. owner: User
  52. images: [Image]
  53. comments: [Comment]
  54. createdAt: String
  55. title: String
  56. description: String,
  57. tags: [String]
  58. address: String
  59. price: Float
  60. }
  61. input AdInput {
  62. _id: ID,
  63. images: [ImageInput]
  64. title: String
  65. description: String,
  66. tags: [String]
  67. address: String
  68. price: Float
  69. }
  70. type Comment {
  71. _id: ID,
  72. owner: User
  73. createdAt: String
  74. text: String
  75. ad: Ad
  76. answers: [Comment]
  77. answerTo: Comment
  78. }
  79. input CommentInput {
  80. _id: ID,
  81. text: String
  82. ad: AdInput
  83. answerTo: CommentInput
  84. }
  85. type Message {
  86. _id: ID,
  87. owner: User
  88. createdAt: String
  89. to: User
  90. text: String
  91. image: Image
  92. }
  93. input MessageInput {
  94. _id: ID,
  95. to: UserInput
  96. text: String
  97. image: ImageInput
  98. }
  99. `);
  100. schema = expand(schema)
  101. console.log(printSchema(schema))
  102. const app = express();
  103. app.use(require('cors')())
  104. app.use(express.static('public'));
  105. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  106. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  107. let decoded;
  108. console.log('wtf')
  109. if (decoded = jwtCheck(req, jwtSecret)){
  110. console.log('SOME UPLOAD', decoded, req.file)
  111. let {models: {Image }} = await getModels(decoded.sub)
  112. let image = await Image.fromFileData(req.file)
  113. res.end(JSON.stringify({_id: image._id, url: image.url}))
  114. }
  115. else {
  116. res.status(503).send('permission denied')
  117. }
  118. })
  119. app.use(express.static('public'));
  120. let socketPath = "/home/asmer/node_hosts/marketplace"
  121. app.listen(socketPath, () => {
  122. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  123. fs.chmodSync(socketPath, '777');
  124. });
  125. })()