const jwtSecret = 'AjnjLhjxM' const express = require('express'); const express_graphql = require('express-graphql'); const cors = require('cors'); const { buildSchema, printSchema } = require('graphql'); const expand = require('mm-graphql/expand') const fs = require('fs') const uploadPath = `${__dirname}/public/images/`; const upload = require('multer')({ dest: uploadPath }) const uploadTrackPath = `${__dirname}/public/track/`; const uploadTrack = require('multer')({ dest: uploadTrackPath, fieldSize: 1024*1024*128 }) ;(async () => { const {Savable, slice, getModels} = await require('./models.js')() const { jwtGQL, jwtCheck } = require('mm-graphql/jwt') const {anonSchema, anonResolvers} = require('./anon')({Savable, secret: jwtSecret}) let schema = buildSchema(` type User { _id: String createdAt: String login: String nick : String avatar: Image } input UserInput { _id: String login: String nick : String avatar: ImageInput } type Image { _id: ID, text: String, url: String, originalFileName: String, userAvatar: User, owner: User } input ImageInput { _id: ID, text: String, userAvatar: UserInput, } type Track { _id: ID, url: String, originalFileName: String, owner: User id3: ID3 playlists: [Playlist] } type ID3 { title: String artist: String album: String year: String genre: String trackNumber: String } input TrackInput { _id: ID, playlists: [PlaylistInput] } type Playlist { _id: ID, owner: User name: String description: String tracks: [Track] } input PlaylistInput { _id: ID, name: String description: String tracks: [TrackInput] } `); schema = expand(schema) console.log(printSchema(schema)) const app = express(); app.use(cors()) app.use(express.static('public')); app.use('/graphql', express_graphql(jwtGQL({anonSchema, anonResolvers, schema, createContext: getModels, graphiql: true, secret: jwtSecret}))) app.post('/upload', upload.single('photo'), async (req, res, next) => { let decoded; console.log('wtf') if (decoded = jwtCheck(req, jwtSecret)){ console.log('SOME UPLOAD', decoded, req.file) let {models: {Image }} = await getModels(decoded.sub) let image = await Image.fromFileData(req.file) if (image.error){ res.end(JSON.stringify(image)) return } res.end(JSON.stringify({_id: image._id, url: image.url})) } else { res.status(503).send('permission denied') } }) app.post('/track', uploadTrack.single('track'), async (req, res, next) => { let decoded; if (decoded = jwtCheck(req, jwtSecret)){ console.log('SOME UPLOAD OF TRACK', decoded, req.file) let {models: { Track }} = await getModels(decoded.sub) let track = await Track.fromFileData(req.file) if (track.error){ console.log('error', track.error) res.end(JSON.stringify(track)) return } res.end(JSON.stringify({_id: track._id, url: track.url})) } else { res.status(503).send('permission denied') } }) app.use(express.static('public')); let socketPath = "/home/asmer/node_hosts/player" app.listen(socketPath, () => { console.log('Express GraphQL Server Now Running On localhost:4000/graphql'); fs.chmodSync(socketPath, '777'); }); })()