index.js 3.6 KB

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