Skip to content

Commit

Permalink
🎉 First Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
foysalit committed Sep 18, 2019
0 parents commit a251768
Show file tree
Hide file tree
Showing 32 changed files with 17,404 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/.babelrc
@@ -0,0 +1,15 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
2 changes: 2 additions & 0 deletions api/.gitignore
@@ -0,0 +1,2 @@
node_modules/
uploads/
5,866 changes: 5,866 additions & 0 deletions api/package-lock.json

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions api/package.json
@@ -0,0 +1,47 @@
{
"name": "api",
"version": "1.0.0",
"description": "Server api for photato",
"main": "dist",
"author": "Foysal Ahamed",
"license": "ISC",
"entry": "src/index.js",
"scripts": {
"dev": "NODE_ENV=development nodemon src/index.js --exec babel-node",
"start": "node dist",
"build": "./node_modules/.bin/babel src --out-dir dist --copy-files",
"prestart": "npm run -s build"
},
"eslintConfig": {
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-unused-vars": 1
}
},
"dependencies": {
"cors": "^2.8.4",
"express": "^4.13.3",
"multer": "^1.4.2",
"mysql2": "^1.6.1",
"sequelize": "^5.18.4"
},
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/node": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"eslint": "^3.1.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"nodemon": "^1.9.2"
}
}
74 changes: 74 additions & 0 deletions api/src/index.js
@@ -0,0 +1,74 @@
import http from 'http';
import cors from 'cors';
import multer from 'multer';
import express from 'express';
import { resolve, join } from 'path';
import { Sequelize } from 'sequelize';

import PhotoModel from './photo.model';

const config = {
port: 3001,
uploadDir: `${resolve(__dirname, '..')}/uploads/`,
database: {
username: "root",
password: "admin",
host: "localhost",
port: "3306",
dialect: "mysql",
database: "photato",
}
};

let app = express();
app.server = http.createServer(app);

// 3rd party middlewares
app.use(cors({}));

// connect to db
const database = new Sequelize(config.database);

// initialize models
const Photo = PhotoModel.init(database);

// setup multer
const uploadMiddleware = multer({
dest: config.uploadDir,
fileFilter: function (req, file, cb) {
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('Only image files are allowed!'));
}
cb(null, true);
},
}).single('photo');

database.sync().then(() => {
app.get('/', (req, res) => {
res.json({app: 'photato'});
});

app.get("/photo/:filename", (req, res) => {
res.sendFile(join(config.uploadDir, `/${req.params.filename}`));
});

app.get('/photo', async (req, res) => {
const photos = await Photo.findAndCountAll();
res.json({success: true, photos});
});

app.post('/photo', uploadMiddleware, async (req, res) => {
try {
const photo = await Photo.create(req.file);
res.json({success: true, photo});
} catch (err) {
res.status(400).json({success: false, message: err.message});
}
});

app.server.listen(process.env.PORT || config.port, () => {
console.log(`Started on port ${app.server.address().port}`);
});
});

export default app;
32 changes: 32 additions & 0 deletions api/src/photo.model.js
@@ -0,0 +1,32 @@
import { Model, DataTypes } from 'sequelize';

const PhotoSchema = {
originalname: {
type: DataTypes.STRING,
allowNull: false,
},
mimetype: {
type: DataTypes.STRING,
allowNull: false,
},
size: {
type: DataTypes.INTEGER,
allowNull: false,
},
filename: {
type: DataTypes.STRING,
allowNull: false,
},
path: {
type: DataTypes.STRING,
allowNull: false,
},
};

class Photo extends Model {
static init (sequelize) {
return super.init(PhotoSchema, { sequelize });
}
};

export default Photo;

0 comments on commit a251768

Please sign in to comment.