index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. theme: String
  21. fontSize: Int
  22. }
  23. input UserInput {
  24. _id: String
  25. login: String
  26. nick : String
  27. avatar: ImageInput
  28. theme: String
  29. fontSize: Int
  30. }
  31. type Image {
  32. _id: ID,
  33. text: String,
  34. createdAt: String
  35. url: String,
  36. originalFileName: String,
  37. userAvatar: User,
  38. owner: User
  39. }
  40. input ImageInput {
  41. _id: ID,
  42. createdAt: String
  43. text: String,
  44. userAvatar: UserInput
  45. }
  46. type Snippet {
  47. _id: ID,
  48. owner: User
  49. createdAt: String
  50. title: String
  51. description: String,
  52. comments: [Comment]
  53. files: [File]
  54. tags: [String]
  55. }
  56. input SnippetInput {
  57. _id: ID,
  58. title: String
  59. description: String,
  60. comments: [CommentInput]
  61. files: [FileInput]
  62. tags: [String]
  63. }
  64. type Comment {
  65. _id: ID,
  66. owner: User
  67. createdAt: String
  68. text: String
  69. snippet: Snippet
  70. answers: [Comment]
  71. answerTo: Comment
  72. }
  73. input CommentInput {
  74. _id: ID,
  75. text: String
  76. snippet: SnippetInput
  77. answerTo: CommentInput
  78. }
  79. type File {
  80. _id: ID,
  81. owner: User
  82. createdAt: String
  83. name: String,
  84. text: String
  85. type: String
  86. snippet: Snippet
  87. }
  88. input FileInput {
  89. _id: ID,
  90. name: String,
  91. text: String
  92. type: String
  93. snippet: SnippetInput
  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. if (image){
  109. res.end(JSON.stringify({_id: image._id, url: image.url}))
  110. }
  111. else {
  112. res.end('YAKAS CHUHNYA')
  113. }
  114. }
  115. else {
  116. res.status(503).send('permission denied')
  117. }
  118. })
  119. app.use(express.static('public'));
  120. let socketPath = "/home/asmer/node_hosts/snippet"
  121. app.listen(socketPath, () => {
  122. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  123. fs.chmodSync(socketPath, '777');
  124. });
  125. })()