Skip to content

Commit

Permalink
build: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kajyr committed Nov 4, 2021
0 parents commit c1da95e
Show file tree
Hide file tree
Showing 27 changed files with 7,439 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binaries
build/Release
build-backend

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm
.eslintcache

# Optional REPL history
.node_repl_history

# Environment config
.env

# Migrations
migrations/.migrate

# Maps
*.map

# Public
public

# Stats
stats.json

# Bloody MacOs
.DS_Store

# Data
data/
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
Empty file added README.md
Empty file.
19 changes: 19 additions & 0 deletions backend/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.1.2](https://github.com/kajyr/diario.blue/compare/v2.1.1...v2.1.2) (2021-10-19)


### Bug Fixes

* countries and locations api ([3c2c396](https://github.com/kajyr/diario.blue/commit/3c2c39674e2726ee1980f6fb73ec1da258cecf8d))





## [2.1.1](https://github.com/kajyr/diario.blue/compare/v2.1.0...v2.1.1) (2021-04-28)

**Note:** Version bump only for package backend
13 changes: 13 additions & 0 deletions backend/app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import build from './app';

describe("Basic App things", () => {
test('Requests the "/" route', async () => {
const app = await build();

const response = await app.inject({
method: "GET",
url: "/",
});
expect(response.statusCode).toBe(200);
});
});
41 changes: 41 additions & 0 deletions backend/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fastify from 'fastify';
import path from 'path';

const PUBLIC_PATH = path.join(__dirname, "..", "public");

async function build(opts = {}) {
const nodeEnv = (process.env.NODE_ENV || "").toLowerCase();

const app = await fastify(opts);
app.register(require("fastify-log"));
app.register(require("fastify-routes"));

await app.register(require("./journal"));

// Block other api
app.route({
method: ["DELETE", "GET", "HEAD", "PATCH", "POST", "PUT", "OPTIONS"],
url: "/api/*",
handler: (req, reply) => {
reply.code(404).send();
},
});

app.register(require("fastify-static"), {
root: PUBLIC_PATH,
});

app.setNotFoundHandler((_, reply) => {
//@ts-ignore This is added by a fastify plugin
reply.sendFile("index.html");
});

app.get("/", (_, reply) => {
//@ts-ignore This is added by a fastify plugin
reply.sendFile("index.html");
});

return app;
}

export default build;
2 changes: 2 additions & 0 deletions backend/helpers/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const omit = (obj: any, blacklist: string[]) =>
Object.fromEntries(Object.entries(obj).filter(([key]) => !blacklist.includes(key)));
32 changes: 32 additions & 0 deletions backend/journal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const RESOURCE_ID = "dives";

export default function (fastify, opts, done) {
const routes = [
{
method: "GET",
url: `/api/journal`,
handler: async function () {
return { test: 5 };
},
},
{
method: "POST",
path: `/api/journal`,
handler: async function (request, reply) {
/* const { guid, ...item } = request.body;
if (!guid) {
return reply.status(405).send({ error: "Missing item id" });
}
*/
return reply.status(200).send();
},
},
];

for (const route of routes) {
fastify.route(route);
}

done();
}
44 changes: 44 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "backend",
"version": "2.1.2",
"main": "index.js",
"repository": "https://github.com/kajyr/diario.blue",
"author": "Carlo Panzi <carlo.panzi@gmail.com>",
"license": "MIT",
"dependencies": {
"@types/node": "^16.11.6",
"colors": "^1.1.2",
"dotenv": "^10.0.0",
"fastify": "^3.15.0",
"fastify-log": "^1.2.1",
"fastify-routes": "^3.0.1",
"fastify-static": "^4.0.1",
"luxon": "^2.0.2",
"pino-pretty": "^7.2.0",
"ts-node": "^10.4.0",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"devDependencies": {
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"nodemon": "^2.0.6"
},
"scripts": {
"start": "ts-node server.ts",
"build": "tsc",
"watch": "nodemon"
},
"nodemonConfig": {
"watch": [
"*"
],
"ext": "ts",
"ignore": [
"**/*.test.ts"
],
"delay": "500",
"exec": "ts-node ./server.ts"
}
}
55 changes: 55 additions & 0 deletions backend/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require("dotenv").config({ path: "../" });

import colors from 'colors/safe';

import app from './app';
import { Server } from './types';

const PORT = 4445;

const spaces = (num: number): string => Array(num).join(" ");

function logRoutes(routes: Server.FastifyRoutesMap) {
console.log(colors.blue("Routes"));

// @ts-ignore routes is a ES6 Map
const obj = Object.fromEntries(routes);

Object.keys(obj).forEach((url) => {
const list = obj[url];
const methods = list
.flatMap((m) => m.method)
.sort((a, b) => a.localeCompare(b))
.join(", ");
console.log(`${colors.blue(url)}${spaces(20 - url.length)}${methods}`);
});
}

const nodeEnv = (process.env.NODE_ENV || "development").toLowerCase();
const isDev = nodeEnv === "development";

const init = async () => {
const server = await app({
logger: isDev
? {
prettyPrint: {
translateTime: "HH:MM:ss",
colorize: true,
ignore: "pid,hostname,reqId",
},
}
: false,
});

try {
await server.listen({ port: PORT, host: "0.0.0.0" });
} catch (err) {
server.log.fatal(err);
process.exit(1);
}

//@ts-ignore This is added by a fastify plugin
logRoutes(server.routes);
};

init();
24 changes: 24 additions & 0 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"esModuleInterop": true,
"importHelpers": true,
"jsx": "react",
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
"paths": {
"helpers/*": ["./helpers/*"],
"dal/*": ["./dal/*"]
},
"outDir": "../build-backend",
"lib": ["es2019"],
"sourceMap": true,
"strict": true,
"target": "es5",
"types": ["jest", "node"]
},
"include": ["./"],
"exclude": ["node_modules", "**/*.test.ts"]
}
23 changes: 23 additions & 0 deletions backend/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export namespace Server {
export type Route = {
method: "GET";
schema: any;
url: string;
logLevel: string;
prefix: string;
bodyLimit: unknown;
handler: () => void;
};

export type RouteBlock = {
get?: Route;
post?: Route;
put?: Route;
options?: Route;
patch?: Route;
head?: Route;
delete?: Route;
};

export type FastifyRoutesMap = Record<string, RouteBlock>;
}
Loading

0 comments on commit c1da95e

Please sign in to comment.