index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. good: Good
  34. category: Category
  35. owner: User
  36. }
  37. input ImageInput {
  38. _id: ID,
  39. text: String,
  40. userAvatar: UserInput,
  41. good: GoodInput
  42. category: CategoryInput
  43. }
  44. type Category {
  45. _id: ID,
  46. name: String,
  47. goods: [Good]
  48. image: Image
  49. owner: User
  50. }
  51. input CategoryInput {
  52. _id: ID,
  53. name: String!,
  54. goods: [ID]
  55. image: ImageInput
  56. }
  57. type Good {
  58. _id: ID,
  59. name: String!,
  60. description: String
  61. price: Float
  62. orderGoods: [OrderGood]
  63. categories: [Category]
  64. images: [Image]
  65. owner: User
  66. }
  67. input GoodInput {
  68. _id: ID,
  69. name: String,
  70. description: String
  71. price: Float
  72. categories: [CategoryInput]
  73. images: [ImageInput]
  74. }
  75. type OrderGood {
  76. _id: ID,
  77. price: Float,
  78. count: Float,
  79. good: Good,
  80. order: Order
  81. owner: User
  82. total: Float
  83. }
  84. input OrderGoodInput {
  85. _id: ID,
  86. count: Int!,
  87. good: GoodInput,
  88. order: OrderInput
  89. }
  90. type Order {
  91. _id: ID
  92. total: Float
  93. orderGoods: [OrderGood]
  94. owner: User
  95. }
  96. input OrderInput {
  97. _id: ID
  98. orderGoods: [OrderGoodInput]
  99. }
  100. `);
  101. schema = expand(schema)
  102. console.log(printSchema(schema))
  103. const app = express();
  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/shop"
  121. app.listen(socketPath, () => {
  122. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  123. fs.chmodSync(socketPath, '777');
  124. });
  125. })()