Debian Codegraph @

docs/develop.md

raw
1 # Development
2 Hurray 🥳 🎉, you are interested in writing code for ntfy! **That's awesome.** 😎
3
4 I tried my very best to write up detailed instructions, but if at any point in time you run into issues, don't
5 hesitate to reach out via one of the channels listed on the [contact page](contact.md).
6
7 ## ntfy server
8 The ntfy server source code is available [on GitHub](https://github.com/binwiederhier/ntfy). The codebase for the
9 server consists of three components:
10
11 * **The main server/client** is written in [Go](https://go.dev/) (so you'll need Go). Its main entrypoint is at
12 [main.go](https://github.com/binwiederhier/ntfy/blob/main/main.go), and the meat you're likely interested in is
13 in [server.go](https://github.com/binwiederhier/ntfy/blob/main/server/server.go). Notably, the server uses a
14 [SQLite](https://sqlite.org) library called [go-sqlite3](https://github.com/mattn/go-sqlite3), which requires
15 [Cgo](https://go.dev/blog/cgo) and `CGO_ENABLED=1` to be set. Otherwise things will not work (see below).
16 * **The documentation** is generated by [MkDocs](https://www.mkdocs.org/) and [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/),
17 which is written in [Python](https://www.python.org/). You'll need Python and MkDocs (via `pip`) only if you want to
18 build the docs.
19 * **The web app** is written in [React](https://reactjs.org/), using [MUI](https://mui.com/). It uses [Vite](https://vitejs.dev/)
20 to build the production build. If you want to modify the web app, you need [nodejs](https://nodejs.org/en/) (for `npm`)
21 and install all the 100,000 dependencies (*sigh*).
22
23 All of these components are built and then **baked into one binary**.
24
25 ### Navigating the code
26 Code:
27
28 * [main.go](https://github.com/binwiederhier/ntfy/blob/main/main.go) - Main entrypoint into the CLI, for both server and client
29 * [cmd/](https://github.com/binwiederhier/ntfy/tree/main/cmd) - CLI commands, such as `serve` or `publish`
30 * [server/](https://github.com/binwiederhier/ntfy/tree/main/server) - The meat of the server logic
31 * [docs/](https://github.com/binwiederhier/ntfy/tree/main/docs) - The [MkDocs](https://www.mkdocs.org/) documentation, also see `mkdocs.yml`
32 * [web/](https://github.com/binwiederhier/ntfy/tree/main/web) - The [React](https://reactjs.org/) application, also see `web/package.json`
33
34 Build related:
35
36 * [Makefile](https://github.com/binwiederhier/ntfy/blob/main/Makefile) - Main entrypoint for all things related to building
37 * [.goreleaser.yml](https://github.com/binwiederhier/ntfy/blob/main/.goreleaser.yml) - Describes all build outputs (for [GoReleaser](https://goreleaser.com/))
38 * [go.mod](https://github.com/binwiederhier/ntfy/blob/main/go.mod) - Go modules dependency file
39 * [mkdocs.yml](https://github.com/binwiederhier/ntfy/blob/main/mkdocs.yml) - Config file for the docs (for [MkDocs](https://www.mkdocs.org/))
40 * [web/package.json](https://github.com/binwiederhier/ntfy/blob/main/web/package.json) - Build and dependency file for web app (for npm)
41
42
43 The `web/` and `docs/` folder are the sources for web app and documentation. During the build process,
44 the generated output is copied to `server/site` (web app and landing page) and `server/docs` (documentation).
45
46 ### Build/test on Gitpod
47 To get a quick working development environment you can use [Gitpod](https://gitpod.io), an in-browser IDE
48 that makes it easy to develop ntfy without having to set up a desktop IDE. For any real development,
49 I do suggest a proper IDE like [IntelliJ IDEA](https://www.jetbrains.com/idea/).
50
51 [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/binwiederhier/ntfy)
52
53 ### Build requirements
54
55 * [Go](https://go.dev/) (required for main server)
56 * [gcc](https://gcc.gnu.org/) (required main server, for SQLite cgo-based bindings)
57 * [Make](https://www.gnu.org/software/make/) (required for convenience)
58 * [libsqlite3/libsqlite3-dev](https://www.sqlite.org/) (required for main server, for SQLite cgo-based bindings)
59 * [GoReleaser](https://goreleaser.com/) (required for a proper main server build)
60 * [Python](https://www.python.org/) (for `pip`, only to build the docs)
61 * [nodejs](https://nodejs.org/en/) (for `npm`, only to build the web app)
62
63 ### Install dependencies
64 These steps **assume Ubuntu**. Steps may vary on different Linux distributions.
65
66 First, install [Go](https://go.dev/) (see [official instructions](https://go.dev/doc/install)):
67 ``` shell
68 wget https://go.dev/dl/go1.25.8.linux-amd64.tar.gz
69 sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.25.8.linux-amd64.tar.gz
70 export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
71 go version # verifies that it worked
72 ```
73
74 Install [GoReleaser](https://goreleaser.com/) (see [official instructions](https://goreleaser.com/install/)):
75 ``` shell
76 go install github.com/goreleaser/goreleaser@latest
77 goreleaser -v # verifies that it worked
78 ```
79
80 Install [nodejs](https://nodejs.org/en/) (see [official instructions](https://nodejs.org/en/download/package-manager/)).
81 Use a current LTS release (Node 24 is what CI builds with; anything older than Node 20 will not work
82 with the current Vite-based web build):
83 ``` shell
84 curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
85 sudo apt-get install -y nodejs
86 npm -v # verifies that it worked
87 ```
88
89 Then install a few other things required:
90 ``` shell
91 sudo apt install \
92 build-essential \
93 libsqlite3-dev \
94 gcc-arm-linux-gnueabi \
95 gcc-aarch64-linux-gnu \
96 python3-pip \
97 git
98 ```
99
100 ### Check out code
101 Now check out via git from the [GitHub repository](https://github.com/binwiederhier/ntfy):
102
103 === "via HTTPS"
104 ``` shell
105 git clone https://github.com/binwiederhier/ntfy.git
106 cd ntfy
107 ```
108
109 === "via SSH"
110 ``` shell
111 git clone [email protected]:binwiederhier/ntfy.git
112 cd ntfy
113 ```
114
115 ### Build all the things
116 Now you can finally build everything. There are tons of `make` targets, so maybe just review what's there first
117 by typing `make`:
118
119 ``` shell
120 $ make
121 Typical commands (more see below):
122 make build - Build web app, documentation and server/client (sloowwww)
123 make cli-linux-amd64 - Build server/client binary (amd64, no web app or docs)
124 make install-linux-amd64 - Install ntfy binary to /usr/bin/ntfy (amd64)
125 make web - Build the web app
126 make docs - Build the documentation
127 make check - Run all tests, vetting/formatting checks and linters
128 ...
129 ```
130
131 If you want to build the **ntfy binary including web app and docs for all supported architectures** (amd64, armv7, and arm64),
132 you can simply run `make build`:
133
134 ``` shell
135 $ make build
136 ...
137 # This builds web app, docs, and the ntfy binary (for amd64, armv7 and arm64).
138 # This will be SLOW (5+ minutes on my laptop on the first run). Maybe look at the other make targets?
139 ```
140
141 You'll see all the outputs in the `dist/` folder afterwards:
142
143 ``` bash
144 $ find dist
145 dist
146 dist/metadata.json
147 dist/ntfy_arm64_linux_arm64
148 dist/ntfy_arm64_linux_arm64/ntfy
149 dist/ntfy_armv7_linux_arm_7
150 dist/ntfy_armv7_linux_arm_7/ntfy
151 dist/ntfy_amd64_linux_amd64
152 dist/ntfy_amd64_linux_amd64/ntfy
153 dist/config.yaml
154 dist/artifacts.json
155 ```
156
157 If you also want to build the **Debian/RPM packages and the Docker images for all supported architectures**, you can
158 use the `make release-snapshot` target:
159
160 ``` shell
161 $ make release-snapshot
162 ...
163 # This will be REALLY SLOW (sometimes 5+ minutes on my laptop)
164 ```
165
166 During development, you may want to be more picky and build only certain things. Here are a few examples.
167
168 ### Build a Docker image only for Linux
169
170 This is useful to test the final build with web app, docs, and server without any dependencies locally
171
172 ``` shell
173 $ make docker-dev
174 $ docker run --rm -p 80:80 binwiederhier/ntfy:dev serve
175 ```
176
177 ### Build the ntfy binary
178 To build only the `ntfy` binary **without the web app or documentation**, use the `make cli-...` targets:
179
180 ``` shell
181 $ make
182 Build server & client (using GoReleaser, not release version):
183 make cli - Build server & client (all architectures)
184 make cli-linux-amd64 - Build server & client (Linux, amd64 only)
185 make cli-linux-armv6 - Build server & client (Linux, armv6 only)
186 make cli-linux-armv7 - Build server & client (Linux, armv7 only)
187 make cli-linux-arm64 - Build server & client (Linux, arm64 only)
188 make cli-windows-amd64 - Build client (Windows, amd64 only)
189 make cli-darwin-all - Build client (macOS, arm64+amd64 universal binary)
190 ```
191
192 So if you're on an amd64/x86_64-based machine, you may just want to run `make cli-linux-amd64` during testing. On a modern
193 system, this shouldn't take longer than 5-10 seconds. I often combine it with `install-linux-amd64` so I can run the binary
194 right away:
195
196 ``` shell
197 $ make cli-linux-amd64 install-linux-amd64
198 $ ntfy serve
199 ```
200
201 **During development of the main app, you can also just use `go run main.go`**, as long as you run
202 `make cli-deps-static-sites`at least once and `CGO_ENABLED=1`:
203
204 ``` shell
205 $ export CGO_ENABLED=1
206 $ make cli-deps-static-sites
207 $ go run main.go serve
208 2022/03/18 08:43:55 Listening on :2586[http]
209 ...
210 ```
211
212 If you don't run `cli-deps-static-sites`, you may see an error *`pattern ...: no matching files found`*:
213 ```
214 $ go run main.go serve
215 server/server.go:85:13: pattern docs: no matching files found
216 ```
217
218 This is because we use `go:embed` to embed the documentation and web app, so the Go code expects files to be
219 present at `server/docs` and `server/site`. If they are not, you'll see the above error. The `cli-deps-static-sites`
220 target creates dummy files that ensure that you'll be able to build.
221
222 While not officially supported (or released), you can build and run the server **on macOS** as well. Simply run
223 `make cli-darwin-server` to build a binary, or `go run main.go serve` (see above) to run it.
224
225 ### Build the web app
226 The sources for the web app live in `web/`. As long as you have `npm` installed (see above), building the web app
227 is really simple. Just type `make web` and you're in business:
228
229 ``` shell
230 $ make web
231 ...
232 ```
233
234 This will build the web app using Create React App and then **copy the production build to the `server/site` folder**, so
235 that when you `make cli` (or `make cli-linux-amd64`, ...), you will have the web app included in the `ntfy` binary.
236
237 If you're developing on the web app, it's best to just `cd web` and run `npm start` manually. This will open your browser
238 at `http://127.0.0.1:3000` with the web app, and as you edit the source files, they will be recompiled and the browser
239 will automatically refresh:
240
241 ``` shell
242 $ cd web
243 $ npm start
244 ```
245
246 ### Testing Web Push locally
247
248 Reference: <https://stackoverflow.com/questions/34160509/options-for-testing-service-workers-via-http>
249
250 #### With the dev servers
251
252 1. Get web push keys `go run main.go webpush keys`
253
254 2. Run the server with web push enabled
255
256 ```sh
257 go run main.go \
258 --log-level debug \
259 serve \
260 --base-url http://localhost \
261 --web-push-public-key KEY \
262 --web-push-private-key KEY \
263 --web-push-email-address <email> \
264 --web-push-file=/tmp/webpush.db
265 ```
266
267 3. In `web/public/config.js`:
268
269 - Set `base_url` to `http://localhost`, This is required as web push can only be used with the server matching the `base_url`.
270
271 - Set the `web_push_public_key` correctly.
272
273 4. Run `npm run start`
274
275 #### With a built package
276
277 1. Run `make web-build`
278
279 2. Run the server (step 2 above)
280
281 3. Open <http://localhost/>
282 ### Build the docs
283 The sources for the docs live in `docs/`. Similarly to the web app, you can simply run `make docs` to build the
284 documentation. As long as you have `mkdocs` installed (see above), this should work fine:
285
286 ``` shell
287 $ make docs
288 ...
289 ```
290
291 If you are changing the documentation, you should be running `mkdocs serve` directly. This will build the documentation,
292 serve the files at `http://127.0.0.1:8000/`, and rebuild every time you save the source files:
293
294 ```
295 $ mkdocs serve
296 INFO - Building documentation...
297 INFO - Cleaning site directory
298 INFO - Documentation built in 5.53 seconds
299 INFO - [16:28:14] Serving on http://127.0.0.1:8000/
300 ```
301
302 Then you can navigate to http://127.0.0.1:8000/ and whenever you change a markdown file in your text editor it'll automatically update.
303
304 ## Android app
305 The ntfy Android app source code is available [on GitHub](https://github.com/binwiederhier/ntfy-android).
306 The Android app has two flavors:
307
308 * **Google Play:** The `play` flavor includes [Firebase (FCM)](https://firebase.google.com/) and requires a Firebase account
309 * **F-Droid:** The `fdroid` flavor does not include Firebase or Google dependencies
310
311 ### Navigating the code
312 * [main/](https://github.com/binwiederhier/ntfy-android/tree/main/app/src/main) - Main Android app source code
313 * [play/](https://github.com/binwiederhier/ntfy-android/tree/main/app/src/play) - Google Play / Firebase specific code
314 * [fdroid/](https://github.com/binwiederhier/ntfy-android/tree/main/app/src/fdroid) - F-Droid Firebase stubs
315 * [build.gradle](https://github.com/binwiederhier/ntfy-android/blob/main/app/build.gradle) - Main build file
316
317 ### IDE/Environment
318 You should download [Android Studio](https://developer.android.com/studio) (or [IntelliJ IDEA](https://www.jetbrains.com/idea/)
319 with the relevant Android plugins). Everything else will just be a pain for you. Do yourself a favor. 😀
320
321 ### Check out the code
322 First check out the repository:
323
324 === "via HTTPS"
325 ``` shell
326 git clone https://github.com/binwiederhier/ntfy-android.git
327 cd ntfy-android
328 ```
329
330 === "via SSH"
331 ``` shell
332 git clone [email protected]:binwiederhier/ntfy-android.git
333 cd ntfy-android
334 ```
335
336 Then either follow the steps for building with or without Firebase.
337
338 ### Build F-Droid flavor (no FCM)
339 !!! info
340 I do build the ntfy Android app using IntelliJ IDEA (Android Studio), so I don't know if these Gradle commands will
341 work without issues. Please give me feedback if it does/doesn't work for you.
342
343 Without Firebase, you may want to still change the default `app_base_url` in [values.xml](https://github.com/binwiederhier/ntfy-android/blob/main/app/src/main/res/values/values.xml)
344 if you're self-hosting the server. Then run:
345 ```
346 # To build an unsigned .apk (app/build/outputs/apk/fdroid/*.apk)
347 ./gradlew assembleFdroidRelease
348
349 # To build a bundle .aab (app/fdroid/release/*.aab)
350 ./gradlew bundleFdroidRelease
351 ```
352
353 The F-Droid flavor automatically excludes Google Services dependencies.
354
355 ### Build Play flavor (FCM)
356 !!! info
357 I do build the ntfy Android app using IntelliJ IDEA (Android Studio), so I don't know if these Gradle commands will
358 work without issues. Please give me feedback if it does/doesn't work for you.
359
360 To build your own version with Firebase, you must:
361
362 * Create a Firebase/FCM account
363 * Place your account file at `app/google-services.json`
364 * And change `app_base_url` in [values.xml](https://github.com/binwiederhier/ntfy-android/blob/main/app/src/main/res/values/values.xml)
365 * Then run:
366 ```
367 # To build an unsigned .apk (app/build/outputs/apk/play/release/*.apk)
368 ./gradlew assemblePlayRelease
369
370 # To build a bundle .aab (app/play/release/*.aab)
371 ./gradlew bundlePlayRelease
372 ```
373
374 ## iOS app
375 Building the iOS app is very involved. Please report any inconsistencies or issues with it. The requirements are
376 strictly based off of my development on this app. There may be other versions of macOS / XCode that work.
377
378 ### Requirements
379 1. macOS Monterey or later
380 1. XCode 13.2+
381 1. A physical iOS device (for push notifications, Firebase does not work in the XCode simulator)
382 1. Firebase account
383 1. Apple Developer license? (I forget if it's possible to do testing without purchasing the license)
384
385 ### Apple setup
386
387 !!! info
388 Along with this step, the [PLIST Deployment](#plist-config) step is also required
389 for these changes to take effect in the iOS app.
390
391 1. [Create a new key in Apple Developer Member Center](https://developer.apple.com/account/resources/authkeys/add)
392 1. Select "Apple Push Notifications service (APNs)"
393 1. Download the newly created key (should have a file name similar to `AuthKey_ZZZZZZ.p8`, where `ZZZZZZ` is the **Key ID**)
394 1. Record your **Team ID** - it can be seen in the top-right corner of the page, or on your Account > Membership page
395 1. Next, navigate to "Project Settings" in the firebase console for your project, and select the iOS app you created. Then, click "Cloud Messaging" in the left sidebar, and scroll down to the "APNs Authentication Key" section. Click "Upload Key", and upload the key you downloaded from Apple Developer.
396
397 !!! warning
398 If you don't do the above setups for APNS, **notifications will not post instantly or sometimes at all**. This is because of the missing APNS key, which is required for firebase to send notifications to the iOS app. See below for a snip from the firebase docs.
399
400 If you don't have an APNs authentication key, you can still send notifications to iOS devices, but they won't be delivered
401 instantly. Instead, they'll be delivered when the device wakes up to check for new notifications or when your application
402 sends a firebase request to check for them. The time to check for new notifications can vary from a few seconds to hours,
403 days or even weeks. Enabling APNs authentication keys ensures that notifications are delivered instantly and is strongly
404 recommended.
405
406 ### Firebase setup
407
408 1. If you haven't already, create a Google / Firebase account
409 1. Visit the [Firebase console](https://console.firebase.google.com)
410 1. Create a new Firebase project:
411 1. Enter a project name
412 1. Disable Google Analytics (currently iOS app does not support analytics)
413 1. On the "Project settings" page, add an iOS app
414 1. Apple bundle ID - "com.copephobia.ntfy-ios" (this can be changed to match XCode's ntfy.sh target > "Bundle Identifier" value)
415 1. Register the app
416 1. Download the config file - GoogleInfo.plist (this will need to be included in the ntfy-ios repository / XCode)
417 1. Generate a new service account private key for the ntfy server
418 1. Go to "Project settings" > "Service accounts"
419 1. Click "Generate new private key" to generate and download a private key to use for sending messages via the ntfy server
420
421 ### ntfy server
422 Note that the ntfy server is not officially supported on macOS. It should, however, be able to run on macOS using these
423 steps:
424
425 1. If not already made, make the `/etc/ntfy/` directory and move the service account private key to that folder
426 1. Copy the `server/server.yml` file from the ntfy repository to `/etc/ntfy/`
427 1. Modify the `/etc/ntfy/server.yml` file `firebase-key-file` value to the path of the private key
428 1. Install go: `brew install go`
429 1. In the ntfy repository, run `make cli-darwin-server`.
430
431 ### XCode setup
432
433 1. Follow step 4 of [Add Firebase to your Apple project](https://firebase.google.com/docs/ios/setup) to install the
434 `firebase-ios-sdk` in XCode, if it's not already present - you can select any packages in addition to Firebase Core / Firebase Messaging
435 1. Similarly, install the SQLite.swift package dependency in XCode
436 1. When running the debug build, ensure XCode is pointed to the connected iOS device - registering for push notifications does not work in the iOS simulators
437
438 ### PLIST config
439 To have instant notifications/better notification delivery when using firebase, you will need to add the
440 `GoogleService-Info.plist` file to your project. Here's how to do that:
441
442 1. In XCode, find the NTFY app target. **Not** the NSE app target.
443 1. Find the Asset/ folder in the project navigator
444 1. Drag the `GoogleService-Info.plist` file into the Asset/ folder that you get from the firebase console. It can be
445 found in the "Project settings" > "General" > "Your apps" with a button labeled "GoogleService-Info.plist"
446
447 After that, you should be all set!