126 lines
2.9 KiB
JavaScript
126 lines
2.9 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 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
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
app.post('/api/users', (req, res, next) => {
|
||
|
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(req.query);
|
||
|
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) => {
|
||
|
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) => {
|
||
|
rpc.getblock("-1", 1).then(block => {
|
||
|
res.status(200).json({
|
||
|
message: 'Found block',
|
||
|
height: block.height
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
app.get('/api/txs', (req, res, next) => {
|
||
|
rpc.z_listreceivedbyaddress(fullnode.addr, 5).then(txs => {
|
||
|
res.status(200).json({
|
||
|
message: 'Transactions found',
|
||
|
txs: txs
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
app.get('/api/getaddr', (req, res, next) => {
|
||
|
res.status(200).json({
|
||
|
message: 'Sending address',
|
||
|
addr: fullnode.addr
|
||
|
});
|
||
|
});
|
||
|
|
||
|
module.exports = app;
|