|
| 1 | +## 🪿 Website Uptime Monitor |
| 2 | + |
| 3 | +A serverless website monitoring application built with the HONC stack (Hono, OpenTelemetry, and Cloudflare). This application allows you to monitor the uptime of websites by performing periodic health checks and storing the results in a D1 database. |
| 4 | + |
| 5 | +### Features |
| 6 | + |
| 7 | +- Monitor multiple websites simultaneously |
| 8 | +- Configurable check intervals per website |
| 9 | +- Track response times and HTTP status codes |
| 10 | +- Calculate uptime percentages |
| 11 | +- RESTful API for managing monitored websites |
| 12 | +- Simple web interface to view monitored sites |
| 13 | + |
| 14 | +### Technology Stack |
| 15 | + |
| 16 | +- **Hono**: Lightweight web framework for Cloudflare Workers |
| 17 | +- **D1**: Cloudflare's serverless SQL database |
| 18 | +- **Drizzle ORM**: Type-safe database toolkit |
| 19 | +- **Durable Objects**: For managing persistent monitoring schedules |
| 20 | +- **OpenTelemetry**: For observability and monitoring |
| 21 | + |
| 22 | +### Getting Started |
| 23 | + |
| 24 | +[D1](https://developers.cloudflare.com/d1/) is Cloudflare's serverless SQL database. Running this application involves two key steps: first, setting up the project locally, and second, deploying it in production. |
| 25 | + |
| 26 | +### Project Structure |
| 27 | + |
| 28 | +```# |
| 29 | +├── src |
| 30 | +│ ├── index.tsx # Hono app entry point |
| 31 | +│ └── db |
| 32 | +│ └── schema.ts # Database schema |
| 33 | +├── .dev.vars.example # Example .dev.vars file |
| 34 | +├── .prod.vars.example # Example .prod.vars file |
| 35 | +├── seed.ts # Optional script to seed the db |
| 36 | +├── drizzle.config.ts # Drizzle configuration |
| 37 | +├── package.json |
| 38 | +├── tsconfig.json # TypeScript configuration |
| 39 | +└── wrangler.toml # Cloudflare Workers configuration |
| 40 | +``` |
| 41 | + |
| 42 | +### Commands for local development |
| 43 | + |
| 44 | +Create a `.dev.vars` file from the example file: |
| 45 | + |
| 46 | +```sh |
| 47 | +cp .dev.vars.example .dev.vars |
| 48 | +``` |
| 49 | + |
| 50 | +Run the migrations and (optionally) seed the database: |
| 51 | + |
| 52 | +```sh |
| 53 | +# this is a convenience script that runs db:touch, db:generate, db:migrate, and db:seed |
| 54 | +npm run db:setup |
| 55 | +``` |
| 56 | + |
| 57 | +Run the development server: |
| 58 | + |
| 59 | +```sh |
| 60 | +npm run dev |
| 61 | +``` |
| 62 | + |
| 63 | +As you iterate on the database schema, you'll need to generate a new migration file and apply it like so: |
| 64 | + |
| 65 | +```sh |
| 66 | +npm run db:generate |
| 67 | +npm run db:migrate |
| 68 | +``` |
| 69 | + |
| 70 | +### Commands for deployment |
| 71 | + |
| 72 | +Before deploying your worker to Cloudflare, ensure that you have a running D1 instance on Cloudflare to connect your worker to. |
| 73 | + |
| 74 | +You can create a D1 instance by navigating to the `Workers & Pages` section and selecting `D1 SQL Database.` |
| 75 | + |
| 76 | +Alternatively, you can create a D1 instance using the CLI: |
| 77 | + |
| 78 | +```sh |
| 79 | +npx wrangler d1 create <database-name> |
| 80 | +``` |
| 81 | + |
| 82 | +After creating the database, update the `wrangler.toml` file with the database id. |
| 83 | + |
| 84 | +```toml |
| 85 | +[[d1_databases]] |
| 86 | +binding = "DB" |
| 87 | +database_name = "uptime-d1-database" |
| 88 | +database_id = "<database-id-you-just-created>" |
| 89 | +migrations_dir = "drizzle/migrations" |
| 90 | +``` |
| 91 | + |
| 92 | +Include the following information in a `.prod.vars` file: |
| 93 | + |
| 94 | +```sh |
| 95 | +CLOUDFLARE_D1_TOKEN="" # An API token with D1 edit permissions. You can create API tokens from your Cloudflare profile |
| 96 | +CLOUDFLARE_ACCOUNT_ID="" # Find your Account id on the Workers & Pages overview (upper right) |
| 97 | +CLOUDFLARE_DATABASE_ID="" # Find the database ID under workers & pages under D1 SQL Database and by selecting the created database |
| 98 | +``` |
| 99 | + |
| 100 | +If you haven’t generated the latest migration files yet, run: |
| 101 | +```shell |
| 102 | +npm run db:generate |
| 103 | +``` |
| 104 | + |
| 105 | +Afterwards, run the migration script for production: |
| 106 | +```shell |
| 107 | +npm run db:migrate:prod |
| 108 | +``` |
| 109 | + |
| 110 | +Change the name of the project in `wrangler.toml` if you want to, but for now it is: |
| 111 | + |
| 112 | +```toml |
| 113 | +name = "uptime-monitor" |
| 114 | +``` |
| 115 | + |
| 116 | +Finally, deploy your worker |
| 117 | + |
| 118 | +```shell |
| 119 | +npm run deploy |
| 120 | +``` |
| 121 | + |
| 122 | + |
0 commit comments