Skip to content

Commit cb69f18

Browse files
authoredNov 22, 2021
Merge pull request #12 from nathanaelhoun/custom-url-selfhosted
Use custom url for self-hosted
2 parents 2199039 + fde5fda commit cb69f18

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed
 

‎server/index.gohtml

+15-15
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@
6767
Here's an example showing how to publish a message using <tt>curl</tt> (via POST):
6868
</p>
6969
<code>
70-
curl -d "Backup successful 😀" ntfy.sh/mytopic
70+
curl -d "Backup successful 😀" <span class="ntfyUrl">ntfy.sh</span>/mytopic
7171
</code>
7272
<p class="smallMarginBottom">
7373
And another one using PUT:
7474
</p>
7575
<code>
76-
echo -en "\u26A0\uFE0F Unauthorized login" | curl -sT- ntfy.sh/mytopic
76+
echo -en "\u26A0\uFE0F Unauthorized login" | curl -sT- <span class="ntfyUrl">ntfy.sh</span>/mytopic
7777
</code>
7878
<p class="smallMarginBottom">
7979
Here's an example in JS with <tt>fetch()</tt> (see <a href="https://github.com/binwiederhier/ntfy/tree/main/examples">full example</a>):
8080
</p>
8181
<code>
82-
fetch('https://ntfy.sh/mytopic', {<br/>
82+
fetch('https://<span class="ntfyUrl">ntfy.sh</span>/mytopic', {<br/>
8383
&nbsp;&nbsp;method: 'POST', // PUT works too<br/>
8484
&nbsp;&nbsp;body: 'Hello from the other side.'<br/>
8585
})
@@ -127,7 +127,7 @@
127127
notifications like this (see <a href="example.html">live example</a>):
128128
</p>
129129
<code>
130-
const eventSource = new EventSource('https://ntfy.sh/mytopic/sse');<br/>
130+
const eventSource = new EventSource('<span class="ntfyProtocol">https://</span><span class="ntfyUrl">ntfy.sh</span>/mytopic/sse');<br/>
131131
eventSource.onmessage = (e) => {<br/>
132132
&nbsp;&nbsp;// Do something with e.data<br/>
133133
};
@@ -136,7 +136,7 @@
136136
You can also use the same <tt>/sse</tt> endpoint via <tt>curl</tt> or any other HTTP library:
137137
</p>
138138
<code>
139-
$ curl -s ntfy.sh/mytopic/sse<br/>
139+
$ curl -s <span class="ntfyUrl">ntfy.sh</span>/mytopic/sse<br/>
140140
event: open<br/>
141141
data: {"id":"weSj9RtNkj","time":1635528898,"event":"open","topic":"mytopic"}<br/><br/>
142142

@@ -149,7 +149,7 @@
149149
To consume JSON instead, use the <tt>/json</tt> endpoint, which prints one message per line:
150150
</p>
151151
<code>
152-
$ curl -s ntfy.sh/mytopic/json<br/>
152+
$ curl -s <span class="ntfyUrl">ntfy.sh</span>/mytopic/json<br/>
153153
{"id":"SLiKI64DOt","time":1635528757,"event":"open","topic":"mytopic"}<br/>
154154
{"id":"hwQ2YpKdmg","time":1635528741,"event":"message","topic":"mytopic","message":"Hi!"}<br/>
155155
{"id":"DGUDShMCsc","time":1635528787,"event":"keepalive","topic":"mytopic"}
@@ -158,7 +158,7 @@
158158
Or use the <tt>/raw</tt> endpoint if you need something super simple (empty lines are keepalive messages):
159159
</p>
160160
<code>
161-
$ curl -s ntfy.sh/mytopic/raw<br/>
161+
$ curl -s <span class="ntfyUrl">ntfy.sh</span>/mytopic/raw<br/>
162162
<br/>
163163
This is a notification<br/>
164164
And another one with a smiley face 😀
@@ -173,7 +173,7 @@
173173
cached messages).
174174
</p>
175175
<code>
176-
curl -s "ntfy.sh/mytopic/json?since=10m"
176+
curl -s "<span class="ntfyUrl">ntfy.sh</span>/mytopic/json?since=10m"
177177
</code>
178178

179179
<h3 id="polling" class="anchor">Polling (<tt>poll=1</tt>)</h3>
@@ -183,7 +183,7 @@
183183
combined with <tt>since=</tt> (defaults to <tt>since=all</tt>).
184184
</p>
185185
<code>
186-
curl -s "ntfy.sh/mytopic/json?poll=1"
186+
curl -s "<span class="ntfyUrl">ntfy.sh</span>/mytopic/json?poll=1"
187187
</code>
188188

189189
<h3 id="multiple-topics" class="anchor">Subscribing to multiple topics (<tt>topic1,topic2,...</tt>)</h3>
@@ -192,7 +192,7 @@
192192
comma-separated list of topics in the URL. This allows you to reduce the number of connections you have to maintain:
193193
</p>
194194
<code>
195-
$ curl -s ntfy.sh/mytopic1,mytopic2/json<br/>
195+
$ curl -s <span class="ntfyUrl">ntfy.sh</span>/mytopic1,mytopic2/json<br/>
196196
{"id":"0OkXIryH3H","time":1637182619,"event":"open","topic":"mytopic1,mytopic2,mytopic3"}<br/>
197197
{"id":"dzJJm7BCWs","time":1637182634,"event":"message","topic":"mytopic1","message":"for topic 1"}<br/>
198198
{"id":"Cm02DsxUHb","time":1637182643,"event":"message","topic":"mytopic2","message":"for topic 2"}
@@ -214,8 +214,8 @@
214214
<code>
215215
rsync -a root@laptop /backups/laptop \<br/>
216216
&nbsp;&nbsp;&& zfs snapshot ... \<br/>
217-
&nbsp;&nbsp;&& curl -d "Laptop backup succeeded" ntfy.sh/backups \<br/>
218-
&nbsp;&nbsp;|| echo -en "\u26A0\uFE0F Laptop backup failed" | curl -sT- ntfy.sh/backups
217+
&nbsp;&nbsp;&& curl -d "Laptop backup succeeded" <span class="ntfyUrl">ntfy.sh</span>/backups \<br/>
218+
&nbsp;&nbsp;|| echo -en "\u26A0\uFE0F Laptop backup failed" | curl -sT- <span class="ntfyUrl">ntfy.sh</span>/backups
219219
</code>
220220

221221
<h3 id="example-web" class="anchor">Example: Server-sent messages in your web app</h3>
@@ -242,7 +242,7 @@
242242
<code>
243243
#!/bin/bash<br/>
244244
if [ "${PAM_TYPE}" = "open_session" ]; then<br/>
245-
&nbsp;&nbsp;echo -en "\u26A0\uFE0F SSH login: ${PAM_USER} from ${PAM_RHOST}" | curl -T- ntfy.sh/alerts<br/>
245+
&nbsp;&nbsp;echo -en "\u26A0\uFE0F SSH login: ${PAM_USER} from ${PAM_RHOST}" | curl -T- <span class="ntfyUrl">ntfy.sh</span>/alerts<br/>
246246
fi
247247
</code>
248248

@@ -254,7 +254,7 @@
254254
<code>
255255
while read result; do<br/>
256256
&nbsp;&nbsp;[ -n "$result" ] && echo "$result" >> results.csv<br/>
257-
done < <(stdbuf -i0 -o0 curl -s ntfy.sh/results/raw)
257+
done < <(stdbuf -i0 -o0 curl -s <span class="ntfyUrl">ntfy.sh</span>/results/raw)
258258
</code>
259259

260260
<h2 id="faq" class="anchor">FAQ</h2>
@@ -331,7 +331,7 @@
331331
To send notifications to it, simply PUT or POST to the topic URL. Here's an example using <tt>curl</tt>:
332332
</p>
333333
<code>
334-
curl -d "Backup failed" <span id="detailTopicUrl"></span>
334+
curl -d "Backup failed" <span id="detailTopicUrl">ntfy.sh/topic</span>
335335
</code>
336336
<p id="detailNotificationsDisallowed">
337337
If you'd like to receive desktop notifications when new messages arrive on this topic, you have

‎server/static/js/app.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
let topics = {};
1313
let currentTopic = "";
1414
let currentTopicUnsubscribeOnClose = false;
15+
let currentUrl = window.location.hostname;
16+
if (window.location.port) {
17+
currentUrl += ':' + window.location.port
18+
}
1519

1620
/* Main view */
1721
const main = document.getElementById("main");
@@ -131,15 +135,15 @@ const fetchCachedMessages = async (topic) => {
131135

132136
const showDetail = (topic) => {
133137
currentTopic = topic;
134-
history.replaceState(topic, `ntfy.sh/${topic}`, `/${topic}`);
138+
history.replaceState(topic, `${currentUrl}/${topic}`, `/${topic}`);
135139
window.scrollTo(0, 0);
136140
rerenderDetailView();
137141
return false;
138142
};
139143

140144
const rerenderDetailView = () => {
141-
detailTitle.innerHTML = `ntfy.sh/${currentTopic}`; // document.location.replaceAll(..)
142-
detailTopicUrl.innerHTML = `ntfy.sh/${currentTopic}`;
145+
detailTitle.innerHTML = `${currentUrl}/${currentTopic}`; // document.location.replaceAll(..)
146+
detailTopicUrl.innerHTML = `${currentUrl}/${currentTopic}`;
143147
while (detailEventsList.firstChild) {
144148
detailEventsList.removeChild(detailEventsList.firstChild);
145149
}
@@ -347,3 +351,11 @@ document.querySelectorAll('.anchor').forEach((el) => {
347351
el.appendChild(anchor);
348352
}
349353
});
354+
355+
// Change ntfy.sh url and protocol to match self-hosted one
356+
document.querySelectorAll('.ntfyUrl').forEach((el) => {
357+
el.innerHTML = currentUrl;
358+
});
359+
document.querySelectorAll('.ntfyProtocol').forEach((el) => {
360+
el.innerHTML = window.location.protocol + "//";
361+
});

0 commit comments

Comments
 (0)