aboutsummaryrefslogtreecommitdiff
path: root/api/index.js
blob: 9bd064b4d563fdf940de97eaf01aa6d21c69bcc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
/*
 * Quick API
 * Author: Kaotisk Hund <kaotisk@arching-kaos.com>
 * Description: Provides a quick API implementation.
 * License: MIT
 */

const config = require('./config');
const akLogMessage = require('./lib/akLogMessage');

const DEFAULT_PORT = 8610;
const PORT = config.port || DEFAULT_PORT;

akLogMessage("INFO", "Daemon started")
/*
 * Split the prefix of each API call in segments for better management
 *
 * To add a new route, use URL_PREFIX and start your route with '/'
 *
 * LOCAL_IP and DEF_PROTO as well as PORT and URL_PREFIX are used to generate
 * visitable links.
 *
 */
const API_VERSION = "v0";
const URL_PREFIX = "/"+API_VERSION;
const DEF_PROTO="http://";
const LOCAL_IP="127.0.0.1";

/* Requiring packages */
const { spawn } = require('child_process');
const logger = require('morgan');
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
const multer = require('multer');
const upload = multer();
const session = require('express-session');
// Delete this (maybe)
const cookieParser = require('cookie-parser');

// We start our server ... 
const app = express();
app.disable('x-powered-by');

// Set logger format output
app.use(logger('combined'));

// Port number to listen to
const server = app.listen(PORT);

// Use CORS
app.use(cors());

// Whitelist of IPs
// var iplist = fs.readFileSync(config.ipList)

// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

// for parsing multipart/form-data
app.use(upload.array()); 

// Cookie!
app.use(cookieParser());

app.use(session({
    secret: config.session.secret,
    resave: false,
    saveUninitialized: true
}));

// Function for adding stuff...
function genericaddtest(req,res){
    console.table(req.body)
    var myobj = req.body;
    res.set('Content-Type', 'application/json');
    res.send(myobj);
}
// POST data
app.post('/test', cors(corsOptions), genericaddtest);

const routes = require('./routes');

//Routes.provideTheAppHere(app);
app.use('/', routes);



/*
 * Adds a latest resolved IPFS path for a given IPNS link
 * to a file.
 *
 * Returns:
 *     - error if error
 *     - success:
 *         - adds the entry to the file
 *         - returns the whole file to the client
 *
 */
function addNSEntriesToFile(entry,res){
    var data = JSON.parse(fs.readFileSync(config.pairsFile));
    var duplicate_entry = 0;
    data.forEach(a=>{
        if ( a.zchain === entry.zchain && a.latest=== entry.latest ){
            duplicate_entry = 1;
            res.send({error:"already there"});
        }
    });

    if ( duplicate_entry === 0 ) {
        // Store it as the first array element of our new list
        var all = [entry];

        // Append the previous entries
        for (var i = 0; i < data.length; i++){
            all[i+1] = data[i];
        }

        // Turn additional back into text
        var json = JSON.stringify(all);

        // Write out the file
        fs.writeFile(config.pairsFile, json, 'utf8', finished);

        // Callback for when file is finished
        function finished(err) {
            console.log('finished writing file');
        }
        res.send(json);
    }
}

/*
 * Adds a latest given zblock to a file.
 *
 * Returns:
 *     - error if error
 *     - success:
 *         - adds the entry to the file
 *         - returns the whole file to the client
 *
 */
function addEntriesToFile(entry,res){
    var data = JSON.parse(fs.readFileSync(config.blocksFile));

    var duplicate_entry = 0;
    data.forEach(a=>{
        if ( a.zblock === entry.zblock ){
            duplicate_entry = 1;
            res.send({error:"already there"});
        }
    });

    if ( duplicate_entry === 0 ) {
        var all = [entry];
        for (var i = 0; i < data.length; i++){
            all[i+1] = data[i];
        }
        var json = JSON.stringify(all);
        fs.writeFile(config.blocksFile, json, 'utf8', finished);
        // wtf is dis?
        function finished(err) {
            console.log('finished writing file');
        }
        res.send(json);
    }
}

app.use(cors);
var corsOptions = {
    origin: '*',
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}