Open patlux opened 1 year ago
Thanks for the submission.
@LucasRoesler can you please make all the necessary suggestions - like:
Example of how to use a package manager within the template, and how to consume a package within a handler.
Making the Dockerfile use a non-root user
And all that kind of stuff?
Hi @patlux there are a couple of things that I notice in your template
Dockerfile
. This content is hidden from the template users can contains the standard server bootstrap logic. We even do this in compiled languages like Go, see the main.go in this template I would expect your Dockerfile to have multiple COPY statements here one that copies the user's handler implementation and at least one that copies the core template Typescript files, probably and index.ts
. This index.ts
would mostly look like the one you already have, but it would import the user's handler function and pass it here during configuration.bun init
, but things like this log statement should generally be avoided. Relatedly, the handler readme is a great opportunity to provide instructions on how to use the template. The current default text from bun init
is likely to cause confusion because it likely doesn't apply very well when the project is templated @LucasRoesler Thank you for the detailed answer!
Understood. I will update it 👍
I tried, but how does the development cycle works here?
As a user, I want to have a nice DX where I can run my server with live-reload when I update my code.
With the current approach I do the following steps:
faas-cli new my-fancy-bun --lang bun
tree my-fancy-bun
├── my-fancy-bun
│ ├── README.md
│ ├── bun.lockb
│ ├── index.ts
│ ├── package.json
│ └── tsconfig.json
cat index.ts
# export const bunServeOptions: Serve = {
# port: 3000,
# fetch: (request: Request) => {
# return new Response('Hello via Bun!', { status: 200 })
# },
# }
cd my-fancy-bun
bun run --watch index.ts
Now I can make changes to index.ts
and it reloads the updated code automatically and I can code my awesome things like I do with any other project which works with bun.
When I finish I just run
cd ..
faas-cli up -f my-fancy-bun.yml
But how do I this with your suggested approach?
faas-cli new my-fancy-bun --lang bun
tree my-fancy-bun
├── my-fancy-bun
│ ├── README.md
│ ├── bun.lockb
│ ├── handler.ts
│ ├── package.json
│ └── tsconfig.json
cd my-fancy-bun
cat handler.ts
# export const handler = (request: Request) => {
# return new Response(`Hello from bun via openfaas!`)
# }
Now what? How do I develop now my function without having to rebuild the docker image on each change?
I can't just run bun run handler.ts
.
Ok I think I found a solution to my problem mentioned in my previous comment.
I hope I could address all your comments :)
@patlux it is looking pretty good. But I have a question about dependency management. The template has two package.json files in it. When i look at the Dockerfile, it isn't obvious to me when the index.ts
or the package.json
from the template is copied into the Docker image. I see it only copy and build the handler code. is this expected?
I think it would be interesting to see an example of a project with a non-trivial dependency.
I would be also curious to see an example where the user accidentally sets the port in their handler and then we see the warning message from index.ts
in the logs.
I would like to suggest adding a bun template to the official store.
https://github.com/patlux/openfaas-bun-template
Proposed templates.json:
What is bun?