Creating a new project
The pmesh create
command is used to create a new pmesh project by initializing a pm3.yml
file in the current directory based on the files nearby.
However you can also do this manually by creating a pm3.yml
file with your own settings.
Usage
For the sake of demonstration, let’s create a simple Fastify app and use pmesh to serve it. Generally you start with a monorepo structure where a directory has subdirectories for each service, assets, whatever it may be.
In this example, we have a directory called my-api
which contains a Fastify app and a hi.txt
file in the assets
directory.
Directoryassets
- hi.txt
Directorymy-api
- app.ts
- package.json
import Fastify from "fastify";
// Declare a routeconst fastify = Fastify({ logger: true, trustProxy: true });fastify.get('/', function (request, reply) { reply.send({ hello: 'world' })})
// Run the server!fastify.listen({ host: process.env.HOST, port: process.env.PORT }, err => { if (err) { fastify.log.error(err) process.exit(1) }})
{ "name": "my-api", "scripts": { "start": "tsx app.ts" }, "type": "module", "dependencies": { "@types/node": "^20.11.17", "fastify": "^4.25.2", "typescript": "^5.3.3", "tsx": "4.6.2" }}
Let’s run pmesh create
to create a new pmesh project.
By default it will infer the type of wrapper to use based on the files in the directory but you can always choose Skip if you want to do it manually later.
Here’s the generated pm3.yml
file.
# What we want to run?services: # <Name of the service>: !<Tag of the service wrapper> ...Configuration assets: !FS {} my-api: !Pnpm {}
# What we want to serve on the HTTP/HTTPS ports?server: # The domain we want to serve our services on myapi.org, myapi.local: # Pattern based routing to each service router: - assets.myapi.org/: assets - my-api.myapi.org/: my-api
# Additional hosts we want to map apart from the top-level domainhosts: - assets.myapi.local - my-api.myapi.local
You can now run pmesh go
to start the server and confirm everything works:
# Start the server> pmesh go &
# Test our app and shutdown> curl my-api.myapi.local{"hello":"world"}> curl assets.myapi.local/hi.txthello> pmesh shutdown
Note that we did not have to pnpm install
, build anything, or go through any other setup steps. pmesh automatically does this for you based on the directives in the pm3.yml
file, neither will your team members or the CI/CD pipeline
you will no longer need, or the server you deploy to.