index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const jwtSecret = 'AjnjLhjxM'
  2. const express = require('express');
  3. const express_graphql = require('express-graphql');
  4. const cors = require('cors');
  5. const { buildSchema, printSchema } = 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. const uploadTrackPath = `${__dirname}/public/track/`;
  11. const uploadTrack = require('multer')({ dest: uploadTrackPath, fieldSize: 1024*1024*128 })
  12. ;(async () => {
  13. const {Savable, slice, getModels} = await require('./models.js')()
  14. const { jwtGQL, jwtCheck } = require('mm-graphql/jwt')
  15. const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret})
  16. let schema = buildSchema(`
  17. type User {
  18. _id: String
  19. createdAt: String
  20. login: String
  21. nick : String
  22. avatar: Image
  23. }
  24. input UserInput {
  25. _id: String
  26. login: String
  27. nick : String
  28. avatar: ImageInput
  29. }
  30. type Image {
  31. _id: ID,
  32. text: String,
  33. url: String,
  34. originalFileName: String,
  35. userAvatar: User,
  36. owner: User
  37. }
  38. input ImageInput {
  39. _id: ID,
  40. text: String,
  41. userAvatar: UserInput,
  42. }
  43. type Track {
  44. _id: ID,
  45. url: String,
  46. originalFileName: String,
  47. owner: User
  48. id3: ID3
  49. playlists: [Playlist]
  50. }
  51. type ID3 {
  52. title: String
  53. artist: String
  54. album: String
  55. year: String
  56. genre: String
  57. trackNumber: String
  58. }
  59. input TrackInput {
  60. _id: ID,
  61. playlists: [PlaylistInput]
  62. }
  63. type Playlist {
  64. _id: ID,
  65. owner: User
  66. name: String
  67. description: String
  68. tracks: [Track]
  69. }
  70. input PlaylistInput {
  71. _id: ID,
  72. name: String
  73. description: String
  74. tracks: [TrackInput]
  75. }
  76. `);
  77. schema = expand(schema)
  78. console.log(printSchema(schema))
  79. const app = express();
  80. app.use(cors())
  81. app.use(express.static('public'));
  82. app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret})))
  83. app.post('/upload', upload.single('photo'), async (req, res, next) => {
  84. let decoded;
  85. console.log('wtf')
  86. if (decoded = jwtCheck(req, jwtSecret)){
  87. console.log('SOME UPLOAD', decoded, req.file)
  88. let {models: {Image }} = await getModels(decoded.sub)
  89. let image = await Image.fromFileData(req.file)
  90. if (image.error){
  91. res.end(JSON.stringify(image))
  92. return
  93. }
  94. res.end(JSON.stringify({_id: image._id, url: image.url}))
  95. }
  96. else {
  97. res.status(503).send('permission denied')
  98. }
  99. })
  100. app.post('/track', uploadTrack.single('track'), async (req, res, next) => {
  101. let decoded;
  102. if (decoded = jwtCheck(req, jwtSecret)){
  103. console.log('SOME UPLOAD OF TRACK', decoded, req.file)
  104. let {models: { Track }} = await getModels(decoded.sub)
  105. let track = await Track.fromFileData(req.file)
  106. if (track.error){
  107. console.log('error', track.error)
  108. res.end(JSON.stringify(track))
  109. return
  110. }
  111. res.end(JSON.stringify({_id: track._id, url: track.url}))
  112. }
  113. else {
  114. res.status(503).send('permission denied')
  115. }
  116. })
  117. app.use(express.static('public'));
  118. let socketPath = "/home/asmer/node_hosts/player"
  119. app.listen(socketPath, () => {
  120. console.log('Express GraphQL Server Now Running On localhost:4000/graphql');
  121. fs.chmodSync(socketPath, '777');
  122. });
  123. })()