index.js 3.9 KB

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