index.js 3.9 KB

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