180 lines
4.2 KiB
JavaScript
180 lines
4.2 KiB
JavaScript
const express = require('express');
|
|
const bodyparser = require('body-parser');
|
|
const app = express();
|
|
const postmodel = require('./models/post');
|
|
const usermodel = require('./models/user');
|
|
const ownermodel = require('./models/owner');
|
|
const itemmodel = require('./models/item');
|
|
const mongoose = require('mongoose');
|
|
const stdrpc = require('stdrpc');
|
|
//const RequestIP = require('@supercharge/request-ip');
|
|
|
|
var db = require('./config/db');
|
|
mongoose.connect(db.url).then(() => {
|
|
console.log("connecting-- ", db);
|
|
}).catch(() => {
|
|
console.log("connection failed!");
|
|
});
|
|
|
|
var fullnode = require('./config/fullnode');
|
|
const rpc = stdrpc({
|
|
url: fullnode.url,
|
|
username: fullnode.username,
|
|
password: fullnode.password
|
|
});
|
|
|
|
app.use(bodyparser.json());
|
|
|
|
app.use((req, res, next) => {
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS");
|
|
//req.ip = RequestIP.getClientIp(req);
|
|
next();
|
|
});
|
|
|
|
app.post('/api/posts', (req, res, next) => {
|
|
const post = new postmodel({
|
|
title: req.body.title,
|
|
content: req.body.content
|
|
});
|
|
post.save();
|
|
//console.log(req.ip);
|
|
res.status(201).json({
|
|
message: 'Post added successfully'
|
|
});
|
|
});
|
|
|
|
app.get('/api/posts', (req, res, next) => {
|
|
postmodel.find().
|
|
then((documents) => {
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'Posts Fetched successfully',
|
|
posts: documents
|
|
});
|
|
});
|
|
});
|
|
|
|
/*
|
|
* Add a new user
|
|
* @param address: String. Shielded zcash address
|
|
* @param session: String. Browser session uuid
|
|
* @param blocktime: Number. Blocktime of login zcash transaction
|
|
*/
|
|
|
|
app.post('/api/users', (req, res, next) => {
|
|
console.log('Post: /api/users');
|
|
const user = new usermodel({
|
|
address: req.body.address,
|
|
session: req.body.session,
|
|
blocktime: req.body.blocktime
|
|
});
|
|
user.save();
|
|
res.status(201).json({
|
|
message: 'User added successfully'
|
|
});
|
|
});
|
|
|
|
app.get('/api/users', (req, res, next) => {
|
|
console.log('Get: /api/users');
|
|
usermodel.find({'address': req.query.address, 'session': req.query.session}).
|
|
then((documents) => {
|
|
console.log("Searching");
|
|
res.status(200).json({
|
|
message: 'Users found successfully',
|
|
users: documents
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/getuser', (req, res, next) => {
|
|
console.log('Get: /api/getuser/', req.query.session);
|
|
usermodel.find({'session': req.query.session}).
|
|
then((documents) => {
|
|
console.log("Searching for user...");
|
|
if(documents.length > 0){
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'User found!',
|
|
user: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'User not found!',
|
|
user: null
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
app.get('/api/blockheight', (req, res, next) => {
|
|
console.log('Get: /api/blockheight');
|
|
rpc.getblock("-1", 1).then(block => {
|
|
res.status(200).json({
|
|
message: 'Found block',
|
|
height: block.height
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
app.get('/api/txs', (req, res, next) => {
|
|
console.log('Get: /api/txs');
|
|
rpc.z_listreceivedbyaddress(fullnode.addr, 5).then(txs => {
|
|
res.status(200).json({
|
|
message: 'Transactions found',
|
|
txs: txs
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/api/getaddr', (req, res, next) => {
|
|
console.log('Get: /api/getaddr');
|
|
res.status(200).json({
|
|
message: 'Sending address',
|
|
addr: fullnode.addr
|
|
});
|
|
});
|
|
|
|
app.get('/api/getowner', (req, res, next) => {
|
|
console.log('Get: /api/getowner');
|
|
ownermodel.find({'address': req.query.address}).then((documents) => {
|
|
if(documents.length > 0){
|
|
//console.log(documents);
|
|
res.status(200).json({
|
|
message: 'Owner found!',
|
|
owner: documents
|
|
});
|
|
} else {
|
|
res.status(204).json({
|
|
message: 'Owner not found!',
|
|
owner: null
|
|
});
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
app.post('/api/addowner', (req, res, next) => {
|
|
console.log('Post: /api/addowner');
|
|
const owner = new ownermodel(req.body);
|
|
owner.save();
|
|
res.status(201).json({
|
|
message: 'Owner added successfully'
|
|
});
|
|
});
|
|
|
|
app.post('/api/addownername', (req, res, next) => {
|
|
console.log('Post: /api/addownername');
|
|
const owner = ownermodel.findOne({address: req.body.address});
|
|
|
|
owner.name = req.body.name;
|
|
owner.save();
|
|
res.status(201).json({
|
|
message: 'Ownername added successfully'
|
|
});
|
|
});
|
|
|
|
module.exports = app;
|