Added leveldb
This commit is contained in:
		@@ -4,7 +4,6 @@
 | 
				
			|||||||
  "main": "index.js",
 | 
					  "main": "index.js",
 | 
				
			||||||
  "license": "MIT",
 | 
					  "license": "MIT",
 | 
				
			||||||
  "private": false,
 | 
					  "private": false,
 | 
				
			||||||
  "type": "module",
 | 
					 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
    "dev": "nodemon src/server.js",
 | 
					    "dev": "nodemon src/server.js",
 | 
				
			||||||
    "start": "node src/server.js"
 | 
					    "start": "node src/server.js"
 | 
				
			||||||
@@ -12,6 +11,7 @@
 | 
				
			|||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "fastify": "^3.20.1",
 | 
					    "fastify": "^3.20.1",
 | 
				
			||||||
    "knex": "^0.21.21",
 | 
					    "knex": "^0.21.21",
 | 
				
			||||||
 | 
					    "level": "^7.0.0",
 | 
				
			||||||
    "sqlite3": "^5.0.2"
 | 
					    "sqlite3": "^5.0.2"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,32 +1,45 @@
 | 
				
			|||||||
const fastify = require('fastify')({ logger: true })
 | 
					const fastify = require('fastify')({ logger: true })
 | 
				
			||||||
 | 
					const level = require('level')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const db = level('./db', { valueEncoding: 'json' })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Declare a route
 | 
					// Declare a route
 | 
				
			||||||
fastify.get('/', async (request, reply) => {
 | 
					fastify.get('/', async (request, reply) => {
 | 
				
			||||||
  return { hello: 'world' }
 | 
					    return { hello: 'world' }
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fastify.get('/a/:id', async (req, res)=>{
 | 
					//Automagic Amazn redirects on /a/
 | 
				
			||||||
 | 
					fastify.get('/a/:id', async (req, res) => {
 | 
				
			||||||
    res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
 | 
					    res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fastify.get('/:id', async (req, res)=>{
 | 
					//Automagic Youtube redirects on /yt/
 | 
				
			||||||
    res.statusCode = 301;
 | 
					fastify.get('/yt/:id', async (req, res) => {
 | 
				
			||||||
    return req.params.id;
 | 
					    res.redirect(302, `https://youtu.be/${req.params.id}`)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fastify.get('/:id', async (req, res) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        const target = await db.get(req.params.id);
 | 
				
			||||||
 | 
					        res.redirect(302, target);
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					        res.statusCode = 404;
 | 
				
			||||||
 | 
					        return 404;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fastify.post('/new', async (req, res) =>{
 | 
					fastify.post('/new', async (req, res) => {
 | 
				
			||||||
    console.log(req.body);
 | 
					    console.log(req.body);
 | 
				
			||||||
    return "";
 | 
					    return "";
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Run the server!
 | 
					// Run the server!
 | 
				
			||||||
const start = async () => {
 | 
					const start = async () => {
 | 
				
			||||||
  try {
 | 
					    try {
 | 
				
			||||||
    await fastify.listen(3000)
 | 
					        await fastify.listen(3000)
 | 
				
			||||||
  } catch (err) {
 | 
					    } catch (err) {
 | 
				
			||||||
    fastify.log.error(err)
 | 
					        fastify.log.error(err)
 | 
				
			||||||
    process.exit(1)
 | 
					        process.exit(1)
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
start()
 | 
					start()
 | 
				
			||||||
		Reference in New Issue
	
	Block a user