index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const jwtSecret = 'AjnjLhjxM'
  2. const jwt = require('jsonwebtoken')
  3. const express = require('express');
  4. const express_graphql = require('express-graphql');
  5. const { buildSchema, printSchema, GraphQLString } = require('graphql');
  6. const expand = require('mm-graphql/expand')
  7. const fs = require('fs')
  8. const uploadPath = `${__dirname}/public/images/`;
  9. const upload = require('multer')({ dest: uploadPath })
  10. ;(async () => {
  11. const {Savable, slice, getModels} = await require('./models.js')()
  12. const { jwtGQLAnon, jwtCheck } = require('mm-graphql/jwt')
  13. let schema = buildSchema(`
  14. type User {
  15. _id: String
  16. createdAt: String
  17. login: String
  18. nick : String
  19. avatar: Image
  20. acl: [String]
  21. }
  22. input UserInput {
  23. _id: String
  24. login: String
  25. nick : String
  26. password: String
  27. acl: [String]
  28. avatar: ImageInput
  29. }
  30. type Image {
  31. _id: ID,
  32. createdAt: String
  33. text: String,
  34. url: String,
  35. originalFileName: String,
  36. userAvatar: User,
  37. good: Good
  38. category: Category
  39. owner: User
  40. }
  41. input ImageInput {
  42. _id: ID,
  43. text: String,
  44. userAvatar: UserInput,
  45. good: GoodInput
  46. category: CategoryInput
  47. }
  48. type Category {
  49. _id: ID,
  50. createdAt: String
  51. name: String,
  52. goods: [Good]
  53. image: Image
  54. owner: User
  55. parent: Category
  56. subCategories: [Category]
  57. }
  58. input CategoryInput {
  59. _id: ID,
  60. name: String,
  61. goods: [GoodInput]
  62. image: ImageInput
  63. parent: CategoryInput
  64. subCategories: [CategoryInput]
  65. }
  66. type Good {
  67. _id: ID,
  68. createdAt: String
  69. name: String,
  70. description: String
  71. price: Float
  72. orderGoods: [OrderGood]
  73. categories: [Category]
  74. images: [Image]
  75. owner: User
  76. }
  77. input GoodInput {
  78. _id: ID,
  79. name: String,
  80. description: String
  81. price: Float
  82. categories: [CategoryInput]
  83. images: [ImageInput]
  84. }
  85. type OrderGood {
  86. _id: ID,
  87. createdAt: String
  88. price: Float,
  89. count: Float,
  90. goodName: String,
  91. good: Good,
  92. order: Order
  93. owner: User
  94. total: Float
  95. }
  96. input OrderGoodInput {
  97. _id: ID,
  98. count: Int!,
  99. good: GoodInput,
  100. order: OrderInput
  101. }
  102. type Order {
  103. _id: ID
  104. createdAt: String
  105. total: Float
  106. orderGoods: [OrderGood]
  107. owner: User
  108. }
  109. input OrderInput {
  110. _id: ID
  111. orderGoods: [OrderGoodInput]
  112. }
  113. `);
  114. schema = expand(schema, {
  115. login:{
  116. type: GraphQLString,
  117. args: {login: {type: GraphQLString},
  118. password: {type: GraphQLString},
  119. },
  120. async resolve(root, {login, password}, context, info){
  121. const Savable = context.models.Savable
  122. if (!login || !password) return null;
  123. const user = await Savable.m.User.findOne({login, password})
  124. console.log(user, {login, password})
  125. if (!user)
  126. return null;
  127. const token = jwt.sign({ sub: {id: user._id, login, acl: user.acl}}, jwtSecret); //подписывам токен нашим ключем
  128. return token
  129. }
  130. }
  131. })
  132. console.log(printSchema(schema))
  133. const app = express();
  134. app.use(require('cors')())
  135. app.use(express.static('public'));
  136. app.use('/graphql', express_graphql(jwtGQLAnon({schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  137. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  138. let decoded;
  139. if (decoded = jwtCheck(req, jwtSecret)){
  140. console.log('SOME UPLOAD', decoded, req.file)
  141. let {models: {Image }} = await getModels(decoded.sub)
  142. if (req.file){
  143. let image = await Image.fromFileData(req.file)
  144. res.end(JSON.stringify({_id: image._id, url: image.url}))
  145. }
  146. else {
  147. res.end('дичь')
  148. }
  149. }
  150. else {
  151. res.status(503).send('permission denied')
  152. }
  153. })
  154. app.use(express.static('public'));
  155. let socketPath = "/home/asmer/node_hosts/shop-roles"
  156. app.listen(socketPath, () => {
  157. console.log(`Express GraphQL Server Now Running On ${socketPath}/graphql`);
  158. fs.chmodSync(socketPath, '777');
  159. });
  160. })()
  161. process.on('uncaughtException', (error) => {
  162. console.log('UNCAUGHT EXCEPTION', error)
  163. })