index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const jwtSecret = 'SniPPetValeraZhgi'
  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. createdAt: String
  31. url: String,
  32. originalFileName: String,
  33. userAvatar: User,
  34. owner: User
  35. }
  36. input ImageInput {
  37. _id: ID,
  38. createdAt: String
  39. text: String,
  40. userAvatar: UserInput
  41. }
  42. type Snippet {
  43. _id: ID,
  44. owner: User
  45. createdAt: String
  46. title: String
  47. description: String,
  48. comments: [Comment]
  49. files: [File]
  50. tags: [String]
  51. }
  52. input SnippetInput {
  53. _id: ID,
  54. title: String
  55. description: String,
  56. comments: [CommentInput]
  57. files: [FileInput]
  58. tags: [String]
  59. }
  60. type Comment {
  61. _id: ID,
  62. owner: User
  63. createdAt: String
  64. text: String
  65. snippet: Snippet
  66. answers: [Comment]
  67. answerTo: Comment
  68. }
  69. input CommentInput {
  70. _id: ID,
  71. text: String
  72. snippet: SnippetInput
  73. answerTo: CommentInput
  74. }
  75. type File {
  76. _id: ID,
  77. owner: User
  78. createdAt: String
  79. name: String,
  80. text: String
  81. type: String
  82. snippet: Snippet
  83. }
  84. input FileInput {
  85. _id: ID,
  86. name: String,
  87. text: String
  88. type: String
  89. snippet: SnippetInput
  90. }
  91. `);
  92. schema = expand(schema)
  93. console.log(printSchema(schema))
  94. const app = express();
  95. app.use(express.static('public'));
  96. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  97. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  98. let decoded;
  99. console.log('wtf')
  100. if (decoded = jwtCheck(req, jwtSecret)){
  101. console.log('SOME UPLOAD', decoded, req.file)
  102. let {models: {Image }} = await getModels(decoded.sub)
  103. let image = await Image.fromFileData(req.file)
  104. res.end(JSON.stringify({_id: image._id, url: image.url}))
  105. }
  106. else {
  107. res.status(503).send('permission denied')
  108. }
  109. })
  110. app.use(express.static('public'));
  111. let socketPath = "/home/asmer/node_hosts/snippet"
  112. app.listen(socketPath, () => {
  113. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  114. fs.chmodSync(socketPath, '777');
  115. });
  116. })()