Skip to content

Commit

Permalink
Merge pull request #1 from stevent-team/refactor/express
Browse files Browse the repository at this point in the history
Refactor as node library using express
  • Loading branch information
giraugh authored May 16, 2022
2 parents f374cae + f08f753 commit feb3220
Show file tree
Hide file tree
Showing 17 changed files with 2,751 additions and 271 deletions.
2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

24 changes: 5 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk


# Added by cargo

/target


# Custom
static/
.parcel-cache
.vscode
dist
node_modules
.DS_Store
4 changes: 4 additions & 0 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["parcel-resolver-ts-base-url", "..."]
}
14 changes: 0 additions & 14 deletions Cargo.toml

This file was deleted.

31 changes: 0 additions & 31 deletions Dockerfile

This file was deleted.

36 changes: 0 additions & 36 deletions README.md

This file was deleted.

56 changes: 56 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node
import yargs from 'yargs'

import { loadRoutesFile } from 'routes'
import startServer from 'server'
import path from 'path'

const serve = async ({ target, routes, host, port, index }) => {
// Load routes
const resolvedRoutes = routes ? path.resolve(__dirname, routes) : ''
const routesMap = routes ? (await loadRoutesFile(resolvedRoutes)
.catch(() => { throw new Error(`Failed to load routes file: ${resolvedRoutes}`) })) : {}

if (!Object.keys(routesMap)?.length) {
console.warn('No dynamic routes configured')
} else {
console.log(`🪴 Configured ${Object.keys(routesMap).length} dynamic route(s)`)
}

// Start server
return startServer({
host,
port,
target,
routes: routesMap,
index: path.join(target, index),
})
}

yargs
.scriptName("epoxy")
.usage('$0 serve <target> [routes] [options]')
.command(['serve <target> [routes]', '$0 <target>'], 'Serve the provided static folder', (yargs) => {
yargs
.positional('target', { describe: 'Path to static directory', require: true })
.positional('routes', { describe: 'Path to routes script' })
}, serve)
.option('port', {
alias: 'p',
type: 'string',
description: 'port to use for http server',
default: 8080,
})
.option('host', {
alias: 'h',
type: 'string',
description: 'host to use for http server',
default: '0.0.0.0',
})
.option('index', {
alias: 'i',
type: 'string',
description: 'path to index html inside of target',
default: 'index.html',
})
.argv
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "epoxy",
"version": "1.0.0",
"description": "Lightweight server-side per-route html injection",
"repository": "https://github.com/stevent-team/epoxy",
"author": "Stevent",
"license": "MIT",
"private": false,
"bin": "./dist/index.js",
"main": "./dist/index.js",
"source": "./index.ts",
"scripts": {
"build": "parcel build"
},
"devDependencies": {
"@types/node": "^17.0.33",
"nodemon": "^2.0.16",
"parcel": "^2.5.0",
"parcel-resolver-ts-base-url": "^1.1.5",
"typescript": "^4.6.4"
},
"dependencies": {
"@types/express": "^4.17.13",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"yargs": "^17.5.1"
}
}
50 changes: 0 additions & 50 deletions src/database.rs

This file was deleted.

8 changes: 8 additions & 0 deletions src/inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const injectHTML = (htmlText, { head, body }) => {
htmlText = inject(htmlText, '</head>', head)
htmlText = inject(htmlText, '</body>', body)
return htmlText
}

const inject = (text, on, insert) =>
insert ? text.split(on).join(`${insert}${on}`) : text
66 changes: 0 additions & 66 deletions src/main.rs

This file was deleted.

53 changes: 0 additions & 53 deletions src/meta.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const loadRoutesFile = async routes => {
const module = await import(routes)
return module?.default
}
Loading

0 comments on commit feb3220

Please sign in to comment.