index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  83. input OrderGoodInput {
  84. _id: ID,
  85. count: Int!,
  86. good: [GoodInput],
  87. order: [OrderInput]
  88. }
  89. type Order {
  90. _id: ID
  91. total: Float
  92. orderGoods: [OrderGood]
  93. owner: User
  94. }
  95. input OrderInput {
  96. _id: ID
  97. orderGoods: [OrderGoodInput]
  98. }
  99. `);
  100. schema = expand(schema)
  101. console.log(printSchema(schema))
  102. const app = express();
  103. app.use(express.static('public'));
  104. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  105. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  106. let decoded;
  107. console.log('wtf')
  108. if (decoded = jwtCheck(req, jwtSecret)){
  109. console.log('SOME UPLOAD', decoded, req.file)
  110. let {models: {Image }} = await getModels(decoded.sub)
  111. let image = await Image.fromFileData(req.file)
  112. res.end(JSON.stringify({_id: image._id, url: image.url}))
  113. }
  114. else {
  115. res.status(503).send('permission denied')
  116. }
  117. })
  118. app.use(express.static('public'));
  119. let socketPath = "/home/asmer/node_hosts/shop"
  120. app.listen(socketPath, () => {
  121. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  122. fs.chmodSync(socketPath, '777');
  123. });
  124. })()