Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
really fast and rough api implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jukefr committed Oct 11, 2018
1 parent 2385a03 commit 79401fc
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
106 changes: 106 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const {readdir, readFile, stat} = require("fs").promises; // Experimental API (will show a warning)
const {join} = require("path");
const express = require("express");

/**
* Returns an array of folder names as strings for a given path
*
* @param path {String}
* @returns {Promise<Array>}
*/
const get_folders = async path => {
let folders = [];
for (const file of await get_files(path)) {
if ((await stat(join(path, file))).isDirectory()) {
folders = [...folders, file]
}
}
return folders
};

/**
* Returns an array of file names as strings for a given path
*
* @param path {String}
* @returns {Promise<Array>}
*/
const get_files = async path => {
return await readdir(path);
};


/**
* Return a file paths for a given query (not recursive)
*
* @param path {String}
* @param query {String}
* @returns {Array}
*/
const send_file = async (path, query) => {
const first_letter = query.charAt(0);
for (const file of await get_files(join(path, first_letter))) {
if (file.match(new RegExp(`^${query}`, 'gi'))) { // Match from start
return JSON.parse(await readFile(join(__dirname, first_letter, file)))
}
}
};

/**
* Return file paths for a given query (recursive)
*
* @param folders {Array}
* @param query {String}
* @returns {Array}
*/
const search_files = async (folders, query) => {
const files = [];
for (const folder of folders) {
for (const file of await get_files(folder)) {
if (file.match(new RegExp(query, 'gi'))) {
files.push(file)
}
}
}
return files;
};

(async () => { // Self-Executing Anonymous Async Function (main)

// Get an array of our letters
let folders = await get_folders(__dirname);
folders = folders.filter(d => d.length === 1);

// Bootstrap express server
const api = express();

// Routes
api.get('/', (req, res) => {
return res.send('hello world');
});

api.get('/:query', async (req, res) => {
const query = req.params.query;

switch (query.length) {

// If the query length is one then serve a list of words
case 1:
return res.send(await get_files(join(__dirname, query.toUpperCase())));

// If the query is longer than one then assume we want a word
default:
return res.send(await send_file(__dirname, query));
}
});

api.get('/search/:query', async (req, res) => { // Fuzzy search
const query = req.params.query;
return res.send(await search_files(folders, query))
});

// Launch
api.listen(process.env.PORT || 3000, function () {
console.log('API Started')
})
})();

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Hacktoberfest",
"version": "1.0.0",
"description": "[![Build Status](https://travis-ci.org/Showndarya/Hacktoberfest.svg?branch=master)](https://travis-ci.org/Showndarya/Hacktoberfest) ![Open Source Love](https://img.shields.io/badge/Open%20Source-%E2%9D%A4-red.svg) ![GitHub](https://img.shields.io/github/license/Showndarya/Hacktoberfest.svg) ![GitHub forks](https://img.shields.io/github/forks/Showndarya/Hacktoberfest.svg) ![GitHub issues](https://img.shields.io/github/issues/Showndarya/Hacktoberfest.svg) ![GitHub pull requests](https://img.shields.io/github/issues-pr/Showndarya/Hacktoberfest.svg) ![GitHub contributors](https://img.shields.io/github/contributors/Showndarya/Hacktoberfest.svg) ![GitHub top language](https://img.shields.io/github/languages/top/Showndarya/Hacktoberfest.svg) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/Showndarya/Hacktoberfest.svg)](http://isitmaintained.com/project/Showndarya/Hacktoberfest \"Average time to resolve an issue\")",
"main": "api.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Showndarya/Hacktoberfest.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Showndarya/Hacktoberfest/issues"
},
"homepage": "https://github.com/Showndarya/Hacktoberfest#readme",
"dependencies": {
"express": "^4.16.4"
}
}

0 comments on commit 79401fc

Please sign in to comment.