At first glance, What is mean by wildcard domain? And what is it used for? Let's understand it thoroughly.
Wildcard subdomains open up many interesting possibilities for building dynamic, user-centric web applications. Here are some project ideas that could leverage wildcard subdomains effectively:
Each user gets a blog at broisnees.yourblog.com Customizable themes and layouts per subdomain, you can take the example medium. You got what i mean, and you can also leverage wildcard subdomains to host your own domain. we will go through how can we actually use it.
In this blog, I have only used the single service but in your sceneriao you can also use the server, ( ngnix, caddy ) then locate to your different webapp. here is the demo configuration of ngnix and caddy.
Nginx:
Caddy:
ok let's go to todays project.
Projects includes multiple tools and technologies.
Bun, elysia, neon serverless pg, drizzle, flyio, cloudflare, docker
Let's create the project using the starting elysia command **bun create elysia
myapp**, after you initialized your project using elysia, you can run the project then it should run on port 3000.
For database
Today I am going to use the neon serverless postgres database
setup with drizzle orm, which is my favourite goto orm.
You can checkout the refrence repo of my Github.
inside of the src
folder you will find the index.ts
file which is the main
entrypoint file. before it, we are going to create the db
folder inside of the
src folder,
db/index.ts
also you should create the drizzle.config.ts
in your root dir, if you are not
very familier with drizzle checkkout their
docs.
Now we are going to create the db/schema/index.ts
file inside of the db
folder.
Ok we are now finished with the databse files, one last thing is to modify the
main entrypoint file, src/index.ts
If you have noticed we have been using two environment which is port
and
DATABASE_URL
You can find out the database url in the neon project dashboard.
We are done with setting up the code base. now we are going to deploy it.
If you don't have fly cli installed in your pc, use brew install flyctl
to
install fly in your machine, or checkout fly.io for more
When using this command if you are not authorized it will ask you to login, after login it will setup the configuration and as it detects the bun application it setups the fly.toml file.
It will generate the dockerfile for you, if not here is the simple dockerfile for bun
Also fly lauch
will create the fly.toml file in your root directory. which
looks like this
But before that we need to deploy the database, so we can use the deploy flag in the fly.toml to run the migrate command and deploy the database
Here is the package.json script
you can use either db:push or migrate, db:push will push the db server, now we are everything setup for deployment to fly io
After successful deployment you should see and fly.io gives the hostname for your application in my case it's mydocsbackend.fly.dev and you can access your app
Go to your cloudflare account and click on DNS and then Add Record
So where did i get this ipv4
hostname from? It's from fly.io, if you go to
fly.io in your app dashboard you will see certificates
you need to add new certificate pointing to your website here in my case it's *.nischal-dahal.com.np, here I don't have .com domain so i am using the country level domain.
You won't be able to see the green dots in verified and rsa, ecdsa check at the first time, you need to add the _achme_challange CNAME find out in Domain ownership verification , pointing to the value that is given by fly.io, then after sometime it will be verified and ready to go.
if you had looked at the index.ts
in the src directory, you will see some
simple code, it's just a simple api with bun, where i am getting name of user
from the user database from our neon db, and if found show the hello, message
else no authorized
Example
because test doesnot exists in our database.
I haven't created the seed script, you can create or add the data on your user
table using drizzle-kit studio npx drizzle-kit studio
Add record manually, now let's try out nees.localhost:8080
,
You can use the
subdomain
in localhost.
Now you can also check it on production, it should give you the same result. If
you haven't deployed after changes, you can use fly deploy
We setup, deployed and tested our bun, elysia and drizzle projects on fly.io you can use other hosting platforms as well, like railway,coolify and many more