index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const jwtSecret = '4atikJgznM'
  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: ID
  16. createdAt: String
  17. login: String
  18. nick : String
  19. acl: [String]
  20. avatar: Media
  21. chats: [Chat]
  22. }
  23. input UserInput {
  24. _id: ID
  25. login: String
  26. nick : String
  27. password: String
  28. acl: [String]
  29. avatar: MediaInput
  30. chats: [ChatInput]
  31. }
  32. type Message {
  33. _id: ID
  34. createdAt: String
  35. owner: User
  36. chat: Chat
  37. text: String
  38. media: [Media]
  39. replies: [Message]
  40. replyTo: Message
  41. forwarded: Message
  42. forwardWith: [Message]
  43. }
  44. input MessageInput {
  45. _id: ID
  46. text: String
  47. chat: Chat
  48. media: [MediaInput]
  49. replies: [MessageInput]
  50. replyTo: MessageInput
  51. forwarded: MessageInput
  52. forwardWith: [MessageInput]
  53. }
  54. type Chat {
  55. _id: ID
  56. createdAt: String
  57. owner: User
  58. title: String
  59. members: [User]
  60. messages: [Message]
  61. avatar: Media
  62. }
  63. input ChatInput {
  64. id: Int
  65. title: String
  66. members: [UserInputt]
  67. messages: [MessageInput]
  68. }
  69. type Media {
  70. _id: ID,
  71. createdAt: String
  72. owner: User
  73. text: String,
  74. url: String,
  75. originalFileName: String,
  76. type: String
  77. userAvatar: User
  78. chatAvatar: [Chat]
  79. messages: [Message]
  80. }
  81. input MediaInput {
  82. _id: ID,
  83. text: String,
  84. userAvatar: UserInput,
  85. chatAvatar: [ChatInput]
  86. messages: [MessageInput]
  87. }
  88. `);
  89. schema = expand(schema, {
  90. login:{
  91. type: GraphQLString,
  92. args: {login: {type: GraphQLString},
  93. password: {type: GraphQLString},
  94. },
  95. async resolve(root, {login, password}, context, info){
  96. const Savable = context.models.Savable
  97. if (!login || !password) return null;
  98. const user = await Savable.m.User.findOne({login, password})
  99. console.log(user, {login, password})
  100. if (!user)
  101. return null;
  102. const token = jwt.sign({ sub: {id: user._id, login, acl: user.acl}}, jwtSecret); //подписывам токен нашим ключем
  103. return token
  104. }
  105. }
  106. })
  107. console.log(printSchema(schema))
  108. const app = express();
  109. app.use(require('cors')())
  110. app.use(express.static('public'));
  111. app.use('/graphql', express_graphql(jwtGQLAnon({schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  112. app.post('/upload', upload.single('media'), async (req, res, next) => {
  113. let decoded;
  114. if (decoded = jwtCheck(req, jwtSecret)){
  115. console.log('SOME UPLOAD', decoded, req.file)
  116. let {models: {Media }} = await getModels(decoded.sub)
  117. let media = await Media.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/graphql-chat"
  126. //let socketPath = 5000
  127. fs.unlinkSync(socketPath)
  128. app.listen(socketPath, () => {
  129. console.log(`Express GraphQL Server Now Running On ${socketPath}/graphql`);
  130. fs.chmodSync(socketPath, '777');
  131. });
  132. })()