Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Mar 5, 2024
1 parent 2cfb363 commit 6bb85ac
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 54 deletions.
103 changes: 50 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,64 @@
<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
<!-- [![Codecov][codecov-src]][codecov-href] -->

# bun-ts-starter
# Reverse Proxy

This is an opinionated TypeScript Starter kit to help kick-start development of your next Bun package.
A modern reverse proxy. Pretty dev URLs for your local projects, with simplicity and flexibility.

## Get Started
## ☘️ Features

It's rather simple to get your package development started:
- Reverse Proxy
- SSL Support
- Custom Domains
- Auto HTTP to HTTPS Redirection
- /etc/hosts Management
- Dependency-free Binary

## 🤖 Usage

```bash
# you may use this GitHub template or the following command:
bunx degit stacksjs/bun-ts-starter my-pkg
cd my-pkg
bun install -d bun-reverse-proxy
# brew install reverse-proxy
# pkgx install reverse-proxy
```

Now, you can use it in your project:

# if you don't have pnpm installed, run `npm i -g pnpm`
bun i # install all deps
bun run build # builds the library for production-ready use
```js
import { startProxy } from 'bun-reverse-proxy'

startProxy({
from: 'localhost:3000',
to: 'my-project.localhost'
})
```

# how to create a git commit?
git add . # select the changes you want to commit
bun run commit # then simply answer the questions
### CLI

# after you have successfully committed, you may create a "release"
bun run release # automates git commits, versioning, and changelog generations
```bash
reverse-proxy --from localhost:3000 --to my-project.localhost
reverse-proxy --from localhost:3000 --to my-project.test --keyPath ./key.pem --certPath ./cert.pem
reverse-proxy --help
reverse-proxy --version
```

_Check out the package.json scripts for more commands._
### Configuration

You can also use a configuration file:

### Developer Experience (DX)
```ts
export default {
'localhost:3000': 'stacks.localhost'
}
```

This Starter Kit comes pre-configured with the following:
Then run:

- [Powerful Build Process](https://github.com/oven-sh/bun) - via Bun
- [Fully Typed APIs](https://www.typescriptlang.org/) - via TypeScript 5.1
- [Be a Good Commitizen](https://www.npmjs.com/package/git-cz) - pre-configured Commitizen & git-cz setup to simplify semantic git commits, versioning, and changelog generations
- [Built With Testing In Mind](https://bun.sh/docs/cli/test) - pre-configured unit-testing powered by [Bun](https://bun.sh/docs/cli/test)
- [Renovate](https://renovatebot.com/) - optimized & automated PR dependency updates
- [GitHub Actions](https://github.com/features/actions) - runs your CI _(fixes code style issues, tags releases & creates its changelogs, runs the test suite, etc.)_
```bash
reverse-proxy start
```

And your config will be loaded from `reverse-proxy.config.ts` _(or `reverse-proxy.config.js`)_. Learn more in the docs.

## Testing

Expand All @@ -52,48 +73,24 @@ bun test

## Changelog

Please see our [releases](https://github.com/stackjs/bun-ts-starter/releases) page for more information on what has changed recently.
Please see our [releases](https://github.com/stacksjs/stacks/releases) page for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
Please review the [Contributing Guide](https://github.com/stacksjs/contributing) for details.

## Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

[Discussions on GitHub](https://github.com/stacksjs/bun-ts-starter/discussions)
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)

For casual chit-chat with others using this package:

[Join the Stacks Discord Server](https://discord.gg/stacksjs)

## Postcardware

You will always be free to use any of the Stacks OSS software. We would also love to see which parts of the world Stacks ends up in. _Receiving postcards makes us happy—andwe will publish them on our website._

Our address is: Stacks.js, 5710 Crescent Park #107, Playa Vista 90094, CA.

## Sponsors

We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
## License

- [JetBrains](https://www.jetbrains.com/)
- [The Solana Foundation](https://solana.com/)

## 📄 License

The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.

Made with 💙

<!-- Badges -->
[npm-version-src]: https://img.shields.io/npm/v/bun-ts-starter?style=flat-square
[npm-version-href]: https://npmjs.com/package/bun-ts-starter
[npm-downloads-src]: https://img.shields.io/npm/dm/bun-ts-starter?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/bun-ts-starter
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/bun-ts-starter/ci.yml?style=flat-square&branch=main
[github-actions-href]: https://github.com/stacksjs/bun-ts-starter/actions?query=workflow%3Aci

<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/bun-ts-starter/main?style=flat-square
[codecov-href]: https://codecov.io/gh/stacksjs/bun-ts-starter -->
116 changes: 116 additions & 0 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import os from 'node:os'
import { cli as command, log } from '@stacksjs/cli'
import { fs } from '@stacksjs/storage'
import { startProxy } from '../src/start'
import { config } from '../src/config'
import { version } from '../package.json'

const cli = command('reverse-proxy')

interface Options {
from?: string
to?: string
keyPath?: string
certPath?: string
}

cli
.command('start', 'Start the Reverse Proxy Server')
.option('--from <from>', 'The URL to proxy from')
.option('--to <to>', 'The URL to proxy to')
.option('--keyPath <path>', 'Absolute path to the SSL key')
.option('--certPath <path>', 'Absolute path to the SSL certificate')
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost')
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
.example('reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert')
.action(async (options?: Options) => {
if (options?.from || options?.to) {
startProxy({
from: options?.from ?? 'localhost:3000',
to: options?.to ?? 'stacks.localhost',
keyPath: options?.keyPath,
certPath: options?.certPath,
})

return
}

// loop over the config and start all the proxies
if (config) {
for (const [from, to] of Object.entries(config)) {
startProxy({
from,
to,
keyPath: options?.keyPath,
certPath: options?.certPath,
})
}
}
else {
// eslint-disable-next-line no-console
console.log('No proxies found in the config')
}
})

cli
.command('update:etc-hosts', 'Update the /etc/hosts file with the proxy domains. Please note, this command requires sudo/admin permissions.')
.alias('update-etc-hosts')
.example('sudo reverse-proxy update:etc-hosts')
.example('sudo reverse-proxy update-etc-hosts')
.action(async () => {
log.info('Ensuring /etc/hosts file covers the proxy domains...')
const hostsFilePath = os.platform() === 'win32'
? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
: '/etc/hosts'

if (config && typeof config === 'object') {
const entriesToAdd = Object.entries(config).map(([from, to]) => `127.0.0.1 ${to} # reverse-proxy mapping for ${from}`)
try {
let currentHostsContent = fs.readFileSync(hostsFilePath, 'utf8')
let updated = false

for (const entry of entriesToAdd) {
const to = entry.split(' ')[1]
// Check if the entry (domain) is already in the file
if (!currentHostsContent.includes(to)) {
// If not, append it
currentHostsContent += `\n${entry}`
updated = true
}
else {
log.info(`Entry for ${to} already exists in the hosts file.`)
}
}

if (updated) {
fs.writeFileSync(hostsFilePath, currentHostsContent, 'utf8')

log.success('Hosts file updated with latest proxy domains.')
}
else {
log.info('No new entries were added to the hosts file.')
}
}
catch (error) {
if (error.code === 'EACCES')
console.error('Permission denied. Please run this command with administrative privileges.')
else
console.error(`An error occurred: ${error.message}`)
}
}
else {
// eslint-disable-next-line no-console
console.log('No proxies found. Is your config configured properly?')
}
})

cli
.command('version', 'Show the version of the Reverse Proxy CLI')
.action(() => {
// eslint-disable-next-line no-console
console.log(version)
})

cli.version(version)
cli.help()
cli.parse()
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"exports": {
".": {
"bun": "./src/index.ts",
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./*": {
Expand All @@ -33,6 +32,9 @@
},
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"reverse-proxy": "./dist/cli.js"
},
"files": [
"dist",
"src"
Expand Down
2 changes: 2 additions & 0 deletions reverse-proxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bun
import('./bin/cli')

0 comments on commit 6bb85ac

Please sign in to comment.