Compare commits

..
58 Commits
Author SHA1 Message Date
Svilen Markov 233de7fc37 Add new functions to custom-api widget
Create release / release (push) Has been cancelled
2025-03-26 19:44:35 +00:00
Svilen Markov c6e0230e5d Allow sending headers in extension widget 2025-03-26 17:59:41 +00:00
Svilen Markov ca2732668e Update docs 2025-03-26 17:58:52 +00:00
Svilen Markov 456b76ac96 Allow specifying method and request body 2025-03-26 17:58:35 +00:00
Svilen Markov a2c58bea82 Add parseRelativeTime function 2025-03-26 11:28:05 +00:00
Svilen Markov 7276b3d5ef Add allow-insecure to custom-api widget 2025-03-26 11:27:47 +00:00
Svilen Markov 1ef8315462 Only override extension url query if parameters property present 2025-03-26 11:27:12 +00:00
Svilen Markov 2f88ac419b Add parseTime to custom-api 2025-03-26 11:27:07 +00:00
Ralph OcdolandSvilen Markov cee7c85780 Add subrequests to custom-api (#385)
* feat: custom-api multiple API queries

* fix template check

* refactor

* Update implementation & docs

* Swap statement

* Update docs

---------

Co-authored-by: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com>
2025-03-26 11:26:16 +00:00
Svilen Markov b2c5205035 Add support for bool in query params fields 2025-03-26 11:25:47 +00:00
Svilen Markov 2be59c3eb6 Fix failing to parse empty response body in custom api widget 2025-03-26 11:25:34 +00:00
Svilen Markov 176fc896ae Also apply to extension widget 2025-03-26 11:24:43 +00:00
Svilen Markov a943555d86 Add note to docs 2025-03-26 11:24:27 +00:00
Svilen Markov 58bbfbbc42 Make query parameters field reusable 2025-03-26 11:24:15 +00:00
Ralph Ocdol 026b644630 feat: add parameters and array parameters support 2025-03-26 11:24:08 +00:00
Svilen Markov 1512718d41 Update readme
Added "Common issues"
2025-03-20 19:27:29 +00:00
Svilen Markov 4731b90899 Merge pull request #456 from slashformotion/slashformotion/refactor
refacto: remove duplicate
2025-03-17 20:49:10 +00:00
slashformotion 844e32a11e refacto: remove duplicate 2025-03-17 16:26:43 +01:00
Svilen Markov 0b2cfc45c1 Docker containers accessibility improvements
Create release / release (push) Has been cancelled
2025-03-17 02:06:32 +00:00
Svilen Markov c8be41ce6c Reduce contrast of calendar dates 2025-03-17 00:45:23 +00:00
Svilen Markov ec2f710d06 Use constant for the prefix 2025-03-16 23:17:07 +00:00
Svilen Markov 71112173b9 Fix title link not opening in new tab 2025-03-16 23:15:19 +00:00
Svilen Markov 1615c20e66 Change custom api int64 types to int
Some of Go's native template functions return int and
having to juggle between int and int64 might get messy
so we'll try to stick to having a single int type
2025-03-16 01:32:35 +00:00
Svilen Markov fbcea12786 Add more info to logged message 2025-03-16 01:25:13 +00:00
Svilen Markov 047d13afd1 Fix summary triangle showing on Safari 2025-03-16 01:24:56 +00:00
Svilen Markov 4c1165533c Define service strings as consts to avoid forgetting to change them in all places
Create release / release (push) Has been cancelled
2025-03-15 19:41:08 +00:00
Svilen Markov 14bdcf71fe Remove debug code 2025-03-15 19:10:21 +00:00
Svilen Markov 1b3f022b2d Rename service to reduce ambiguity
Create release / release (push) Has been cancelled
2025-03-15 19:03:48 +00:00
Svilen Markov b6d93a7f09 Merge pull request #371 from KallanX/enhancement/add-support-pihole-v6
Enhancement/add support pihole v6
2025-03-15 18:45:22 +00:00
Svilen Markov 4b2438c298 Refactor DNS stats widget 2025-03-15 18:42:58 +00:00
Svilen Markov 3b79c8e09f Remove symbol-link-template 2025-03-15 10:27:39 +00:00
Svilen Markov d7bbf2b8e2 Accessibility improvements
Create release / release (push) Has been cancelled
2025-03-12 18:01:35 +00:00
Svilen Markov b8df34309f Add note to docs about page width 2025-03-12 18:01:35 +00:00
Svilen Markov 278fa3c397 Add gap between domain and percent 2025-03-12 18:01:35 +00:00
Svilen Markov 6f48ee98e5 Uppercase currency before looking for matching symbol #365 2025-03-12 18:01:35 +00:00
Svilen Markov 774b0c104b Merge pull request #399 from KadoBOT/patch-1
Update configuration.md
2025-03-08 09:46:37 +00:00
Ricardo Ambrogi fbd4d9a74e Update configuration.md
Correct `title` to `name`.

I'm unsure if `title` is also accepted, I haven't tested it. Please close this PR if this fix is not valid.
2025-03-07 09:07:10 +01:00
Svilen Markov 3f2fefe4f7 Merge pull request #382 from legoraft/main
Added gruvbox theme
2025-03-01 22:44:22 +00:00
Legoraft fb8513bc9f Added gruvbox theme code and screenshot 2025-03-01 14:19:36 +01:00
Legoraft e4ec958edb Added gruvbox screenshot 2025-03-01 14:17:17 +01:00
Keith Carichner Jr 78ad88a245 Merge pull request #1 from ralphocdol/fix-graph
Fix Graph and move SID to header
2025-02-27 10:33:20 -05:00
Ralph Ocdol 99660aeee8 Fix Graph and move SID to header 2025-02-27 23:02:10 +08:00
Keith Carichner Jr 65a412eb59 Pushing latest. Add ability to fetch series data from new api. 2025-02-26 16:20:43 -05:00
Keith Carichner Jr 2002ed1c9c Pushing latest changes. 2025-02-24 13:52:27 -05:00
Svilen Markov 488a1f6070 Update link to point to repo instead of pkg.go.dev 2025-02-22 13:32:50 +00:00
Svilen Markov 1e12d937aa Merge pull request #372 from DavisYe/main
Correct document
2025-02-22 12:58:56 +00:00
Keith Carichner Jr e643a44b59 Pushing latest fixes to handle the two different JSON responses. 2025-02-20 14:54:11 -05:00
DavisYe 7319870289 Correct the docker compose environment format of GITHUB_TOKEN in the configuration document. 2025-02-20 21:30:34 +08:00
Keith Carichner Jr 72c1ebf66d Minor README adjustment. 2025-02-19 23:23:22 -05:00
Keith Carichner Jr 008c4a3ab7 Removing SID check func. 2025-02-19 23:04:45 -05:00
Keith Carichner Jr 38d3d11571 Attempting to add support for Pi-hole v6. 2025-02-19 17:30:14 -05:00
Svilen Markov d4565acfe7 Markets widget rate limit fix
Create release / release (push) Has been cancelled
2025-02-19 02:25:07 +00:00
Svilen Markov 16129c53bd Merge pull request #358 from rubiojr/rubiojr/reload-on-rename2
Auto-reload config file on RENAME
2025-02-19 01:25:08 +00:00
Svilen Markov cbf1961510 Don't try to get sensor info on openbsd 2025-02-17 23:48:16 +00:00
Svilen Markov c76a4d4be7 Increase docker containers widget timeout 2025-02-17 23:45:57 +00:00
Svilen Markov 27af0400c0 Tweak impl for handling config renames 2025-02-17 22:28:10 +00:00
Sergio Rubio 76a80ff034 Add clarifying comment 2025-02-17 19:17:49 +01:00
Sergio Rubio f7f333ad52 Auto-reload config file on RENAME
Some editors (like Vim), create a temp file when saving, then replace
the file being edited with the temp file. This causes the FS notify
event to be RENAME, not WRITE, so auto-reload misses this.

In addition to that, the file is removed from the watcher and the
auto-reload functionality stops working entirely after the first RENAME.

https://github.com/fsnotify/fsnotify/issues/255 describes this.
2025-02-17 19:08:54 +01:00
29 changed files with 1149 additions and 253 deletions
+71 -42
View File
@@ -264,59 +264,31 @@ Glance can also be installed through the following 3rd party channels:
<br> <br>
## Building from source ## Common issues
Choose one of the following methods:
<details> <details>
<summary><strong>Build binary with Go</strong></summary> <summary><strong>Requests timing out</strong></summary>
<br>
Requirements: [Go](https://go.dev/dl/) >= v1.23 The most common cause of this is when using Pi-Hole, AdGuard Home or other ad-blocking DNS services, which by default have a fairly low rate limit. Depending on the number of widgets you have in a single page, this limit can very easily be exceeded. To fix this, increase the rate limit in the settings of your DNS service.
To build the project for your current OS and architecture, run: If using Podman, in some rare cases the timeout can be caused by an unknown issue, in which case it may be resolved by adding the following to the bottom of your `docker-compose.yml` file:
```yaml
```bash networks:
go build -o build/glance . podman:
external: true
``` ```
To build for a specific OS and architecture, run:
```bash
GOOS=linux GOARCH=amd64 go build -o build/glance .
```
[*click here for a full list of GOOS and GOARCH combinations*](https://go.dev/doc/install/source#:~:text=$GOOS%20and%20$GOARCH)
Alternatively, if you just want to run the app without creating a binary, like when you're testing out changes, you can run:
```bash
go run .
```
<hr>
</details> </details>
<details> <details>
<summary><strong>Build project and Docker image with Docker</strong></summary> <summary><strong>Broken layout for markets, bookmarks or other widgets</strong></summary>
<br>
Requirements: [Docker](https://docs.docker.com/engine/install/) This is almost always caused by the browser extension Dark Reader. To fix this, disable dark mode for the domain where Glance is hosted.
</details>
To build the project and image using just Docker, run: <details>
<summary><strong>cannot unmarshal !!map into []glance.page</strong></summary>
*(replace `owner` with your name or organization)* The most common cause of this is having a `pages` key in your `glance.yml` and then also having a `pages` key inside one of your included pages. To fix this, remove the `pages` key from the top of your included pages.
```bash
docker build -t owner/glance:latest .
```
If you wish to push the image to a registry (by default Docker Hub), run:
```bash
docker push owner/glance:latest
```
<hr>
</details> </details>
<br> <br>
@@ -375,6 +347,63 @@ Feature requests are tagged with one of the following:
<br> <br>
## Building from source
Choose one of the following methods:
<details>
<summary><strong>Build binary with Go</strong></summary>
<br>
Requirements: [Go](https://go.dev/dl/) >= v1.23
To build the project for your current OS and architecture, run:
```bash
go build -o build/glance .
```
To build for a specific OS and architecture, run:
```bash
GOOS=linux GOARCH=amd64 go build -o build/glance .
```
[*click here for a full list of GOOS and GOARCH combinations*](https://go.dev/doc/install/source#:~:text=$GOOS%20and%20$GOARCH)
Alternatively, if you just want to run the app without creating a binary, like when you're testing out changes, you can run:
```bash
go run .
```
<hr>
</details>
<details>
<summary><strong>Build project and Docker image with Docker</strong></summary>
<br>
Requirements: [Docker](https://docs.docker.com/engine/install/)
To build the project and image using just Docker, run:
*(replace `owner` with your name or organization)*
```bash
docker build -t owner/glance:latest .
```
If you wish to push the image to a registry (by default Docker Hub), run:
```bash
docker push owner/glance:latest
```
<hr>
</details>
<br>
## Contributing guidelines ## Contributing guidelines
* Before working on a new feature it's preferable to submit a feature request first and state that you'd like to implement it yourself * Before working on a new feature it's preferable to submit a feature request first and state that you'd like to implement it yourself
+106 -12
View File
@@ -9,7 +9,7 @@
- [Document](#document) - [Document](#document)
- [Branding](#branding) - [Branding](#branding)
- [Theme](#theme) - [Theme](#theme)
- [Themes](#themes) - [Available themes](#available-themes)
- [Pages & Columns](#pages--columns) - [Pages & Columns](#pages--columns)
- [Widgets](#widgets) - [Widgets](#widgets)
- [RSS](#rss) - [RSS](#rss)
@@ -274,7 +274,7 @@ theme:
contrast-multiplier: 1.1 contrast-multiplier: 1.1
``` ```
### Themes ### Available themes
If you don't want to spend time configuring your own theme, there are [several available themes](themes.md) which you can simply copy the values for. If you don't want to spend time configuring your own theme, there are [several available themes](themes.md) which you can simply copy the values for.
### Properties ### Properties
@@ -356,7 +356,7 @@ pages:
### Properties ### Properties
| Name | Type | Required | Default | | Name | Type | Required | Default |
| ---- | ---- | -------- | ------- | | ---- | ---- | -------- | ------- |
| title | string | yes | | | name | string | yes | |
| slug | string | no | | | slug | string | no | |
| width | string | no | | | width | string | no | |
| center-vertically | boolean | no | false | | center-vertically | boolean | no | false |
@@ -374,7 +374,7 @@ The URL friendly version of the title which is used to access the page. For exam
#### `width` #### `width`
The maximum width of the page on desktop. Possible values are `slim` and `wide`. The maximum width of the page on desktop. Possible values are `slim` and `wide`.
* default: `1600px` * default: `1600px` (when no value is specified)
* slim: `1100px` * slim: `1100px`
* wide: `1920px` * wide: `1920px`
@@ -1292,8 +1292,14 @@ Examples:
| ---- | ---- | -------- | ------- | | ---- | ---- | -------- | ------- |
| url | string | yes | | | url | string | yes | |
| headers | key (string) & value (string) | no | | | headers | key (string) & value (string) | no | |
| method | string | no | GET |
| body-type | string | no | json |
| body | any | no | |
| frameless | boolean | no | false | | frameless | boolean | no | false |
| allow-insecure | boolean | no | false |
| template | string | yes | | | template | string | yes | |
| parameters | key (string) & value (string|array) | no | |
| subrequests | map of requests | no | |
##### `url` ##### `url`
The URL to fetch the data from. It must be accessible from the server that Glance is running on. The URL to fetch the data from. It must be accessible from the server that Glance is running on.
@@ -1307,11 +1313,88 @@ headers:
Accept: application/json Accept: application/json
``` ```
##### `method`
The HTTP method to use when making the request. Possible values are `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `OPTIONS` and `HEAD`.
##### `body-type`
The type of the body that will be sent with the request. Possible values are `json`, and `string`.
##### `body`
The body that will be sent with the request. It can be a string or a map. Example:
```yaml
body-type: json
body:
key1: value1
key2: value2
multiple-items:
- item1
- item2
```
```yaml
body-type: string
body: |
key1=value1&key2=value2
```
##### `frameless` ##### `frameless`
When set to `true`, removes the border and padding around the widget. When set to `true`, removes the border and padding around the widget.
##### `allow-insecure`
Whether to ignore invalid/self-signed certificates.
##### `template` ##### `template`
The template that will be used to display the data. It relies on Go's `html/template` package so it's recommended to go through [its documentation](https://pkg.go.dev/text/template) to understand how to do basic things such as conditionals, loops, etc. In addition, it also uses [tidwall's gjson](https://pkg.go.dev/github.com/tidwall/gjson) package to parse the JSON data so it's worth going through its documentation if you want to use more advanced JSON selectors. You can view additional examples with explanations and function definitions [here](custom-api.md). The template that will be used to display the data. It relies on Go's `html/template` package so it's recommended to go through [its documentation](https://pkg.go.dev/text/template) to understand how to do basic things such as conditionals, loops, etc. In addition, it also uses [tidwall's gjson](https://github.com/tidwall/gjson) package to parse the JSON data so it's worth going through its documentation if you want to use more advanced JSON selectors. You can view additional examples with explanations and function definitions [here](custom-api.md).
##### `parameters`
A list of keys and values that will be sent to the custom-api as query paramters.
##### `subrequests`
A map of additional requests that will be executed concurrently and then made available in the template via the `.Subrequest` property. Example:
```yaml
- type: custom-api
cache: 2h
subrequests:
another-one:
url: https://uselessfacts.jsph.pl/api/v2/facts/random
title: Random Fact
url: https://uselessfacts.jsph.pl/api/v2/facts/random
template: |
<p class="size-h4 color-paragraph">{{ .JSON.String "text" }}</p>
<p class="size-h4 color-paragraph margin-top-15">{{ (.Subrequest "another-one").JSON.String "text" }}</p>
```
The subrequests support all the same properties as the main request, except for `subrequests` itself, so you can use `headers`, `parameters`, etc.
`(.Subrequest "key")` can be a little cumbersome to write, so you can define a variable to make it easier:
```yaml
template: |
{{ $anotherOne := .Subrequest "another-one" }}
<p>{{ $anotherOne.JSON.String "text" }}</p>
```
You can also access the `.Response` property of a subrequest as you would with the main request:
```yaml
template: |
{{ $anotherOne := .Subrequest "another-one" }}
<p>{{ $anotherOne.Response.StatusCode }}</p>
```
> [!NOTE]
>
> Setting this property will override any query parameters that are already in the URL.
```yaml
parameters:
param1: value1
param2:
- item1
- item2
```
### Extension ### Extension
Display a widget provided by an external source (3rd party). If you want to learn more about developing extensions, checkout the [extensions documentation](extensions.md) (WIP). Display a widget provided by an external source (3rd party). If you want to learn more about developing extensions, checkout the [extensions documentation](extensions.md) (WIP).
@@ -1330,6 +1413,7 @@ Display a widget provided by an external source (3rd party). If you want to lear
| url | string | yes | | | url | string | yes | |
| fallback-content-type | string | no | | | fallback-content-type | string | no | |
| allow-potentially-dangerous-html | boolean | no | false | | allow-potentially-dangerous-html | boolean | no | false |
| headers | key & value | no | |
| parameters | key & value | no | | | parameters | key & value | no | |
##### `url` ##### `url`
@@ -1338,6 +1422,14 @@ The URL of the extension. **Note that the query gets stripped from this URL and
##### `fallback-content-type` ##### `fallback-content-type`
Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`. Optionally specify the fallback content type of the extension if the URL does not return a valid `Widget-Content-Type` header. Currently the only supported value for this property is `html`.
##### `headers`
Optionally specify the headers that will be sent with the request. Example:
```yaml
headers:
x-api-key: ${SECRET_KEY}
```
##### `allow-potentially-dangerous-html` ##### `allow-potentially-dangerous-html`
Whether to allow the extension to display HTML. Whether to allow the extension to display HTML.
@@ -1608,7 +1700,7 @@ services:
glance: glance:
image: glanceapp/glance image: glanceapp/glance
environment: environment:
- GITHUB_TOKEN: <your token> - GITHUB_TOKEN=<your token>
``` ```
and then use it in your `glance.yml` like this: and then use it in your `glance.yml` like this:
@@ -1765,29 +1857,31 @@ Preview:
| allow-insecure | bool | no | false | | allow-insecure | bool | no | false |
| url | string | yes | | | url | string | yes | |
| username | string | when service is `adguard` | | | username | string | when service is `adguard` | |
| password | string | when service is `adguard` | | | password | string | when service is `adguard` or `pihole-v6` | |
| token | string | when service is `pihole` | | | token | string | when service is `pihole` | |
| hide-graph | bool | no | false | | hide-graph | bool | no | false |
| hide-top-domains | bool | no | false | | hide-top-domains | bool | no | false |
| hour-format | string | no | 12h | | hour-format | string | no | 12h |
##### `service` ##### `service`
Either `adguard` or `pihole`. Either `adguard`, or `pihole` (major version 5 and below) or `pihole-v6` (major version 6 and above).
##### `allow-insecure` ##### `allow-insecure`
Whether to allow invalid/self-signed certificates when making the request to the service. Whether to allow invalid/self-signed certificates when making the request to the service.
##### `url` ##### `url`
The base URL of the service. Can be specified from an environment variable using the syntax `${VARIABLE_NAME}`. The base URL of the service.
##### `username` ##### `username`
Only required when using AdGuard Home. The username used to log into the admin dashboard. Can be specified from an environment variable using the syntax `${VARIABLE_NAME}`. Only required when using AdGuard Home. The username used to log into the admin dashboard.
##### `password` ##### `password`
Only required when using AdGuard Home. The password used to log into the admin dashboard. Can be specified from an environment variable using the syntax `${VARIABLE_NAME}`. Required when using AdGuard Home, where the password is the one used to log into the admin dashboard.
Also requried when using Pi-hole major version 6 and above, where the password is the one used to log into the admin dashboard or the application password, which can be found in `Settings -> Web Interface / API -> Configure app password`.
##### `token` ##### `token`
Only required when using Pi-hole. The API token which can be found in `Settings -> API -> Show API token`. Can be specified from an environment variable using the syntax `${VARIABLE_NAME}`. Only required when using Pi-hole major version 5 or earlier. The API token which can be found in `Settings -> API -> Show API token`.
##### `hide-graph` ##### `hide-graph`
Whether to hide the graph showing the number of queries over time. Whether to hide the graph showing the number of queries over time.
+64
View File
@@ -242,6 +242,57 @@ Other operations include `add`, `mul`, and `div`.
<hr> <hr>
JSON response:
```json
{
"posts": [
{
"title": "Exploring the Depths of Quantum Computing",
"date": "2023-10-27T10:00:00Z"
},
{
"title": "A Beginner's Guide to Sustainable Living",
"date": "2023-11-15T14:30:00+01:00"
},
{
"title": "The Art of Baking Sourdough Bread",
"date": "2023-12-03T08:45:22-08:00"
}
]
}
```
To parse the date and display the relative time (e.g. 2h, 1d, etc), you would use the following:
```
{{ range .JSON.Array "posts" }}
<div>{{ .String "title" }}</div>
<div {{ .String "date" | parseTime "rfc3339" | toRelativeTime }}></div>
{{ end }}
```
The `parseTime` function takes two arguments: the layout of the date string and the date string itself. The layout can be one of the following: "RFC3339", "RFC3339Nano", "DateTime", "DateOnly", "TimeOnly" or a custom layout in Go's [date format](https://pkg.go.dev/time#pkg-constants).
Output:
```html
<div>Exploring the Depths of Quantum Computing</div>
<div data-dynamic-relative-time="1698400800"></div>
<div>A Beginner's Guide to Sustainable Living</div>
<div data-dynamic-relative-time="1700055000"></div>
<div>The Art of Baking Sourdough Bread</div>
<div data-dynamic-relative-time="1701621922"></div>
```
You don't have to worry about the internal implementation, this will then be dynamically populated by Glance on the client side to show the correct relative time.
The important thing to notice here is that the return value of `toRelativeTime` must be used as an attribute in an HTML tag, be it a `div`, `li`, `span`, etc.
<hr>
In some instances, you may want to know the status code of the response. This can be done using the following: In some instances, you may want to know the status code of the response. This can be done using the following:
```html ```html
@@ -273,12 +324,25 @@ The following helper functions provided by Glance are available:
- `toFloat(i int) float`: Converts an integer to a float. - `toFloat(i int) float`: Converts an integer to a float.
- `toInt(f float) int`: Converts a float to an integer. - `toInt(f float) int`: Converts a float to an integer.
- `toRelativeTime(t time.Time) template.HTMLAttr`: Converts Time to a relative time such as 2h, 1d, etc which dynamically updates. **NOTE:** the value of this function should be used as an attribute in an HTML tag, e.g. `<span {{ toRelativeTime .Time }}></span>`.
- `parseTime(layout string, s string) time.Time`: Parses a string into time.Time. The layout must be provided in Go's [date format](https://pkg.go.dev/time#pkg-constants). You can alternatively use these values instead of the literal format: "unix", "RFC3339", "RFC3339Nano", "DateTime", "DateOnly".
- `parseRelativeTime(layout string, s string) time.Time`: A shorthand for `{{ .String "date" | parseTime "rfc3339" | toRelativeTime }}`.
- `add(a, b float) float`: Adds two numbers. - `add(a, b float) float`: Adds two numbers.
- `sub(a, b float) float`: Subtracts two numbers. - `sub(a, b float) float`: Subtracts two numbers.
- `mul(a, b float) float`: Multiplies two numbers. - `mul(a, b float) float`: Multiplies two numbers.
- `div(a, b float) float`: Divides two numbers. - `div(a, b float) float`: Divides two numbers.
- `formatApproxNumber(n int) string`: Formats a number to be more human-readable, e.g. 1000 -> 1k. - `formatApproxNumber(n int) string`: Formats a number to be more human-readable, e.g. 1000 -> 1k.
- `formatNumber(n float|int) string`: Formats a number with commas, e.g. 1000 -> 1,000. - `formatNumber(n float|int) string`: Formats a number with commas, e.g. 1000 -> 1,000.
- `trimPrefix(prefix string, str string) string`: Trims the prefix from a string.
- `trimSuffix(suffix string, str string) string`: Trims the suffix from a string.
- `trimSpace(str string) string`: Trims whitespace from a string on both ends.
- `replaceAll(old string, new string, str string) string`: Replaces all occurrences of a string in a string.
- `findMatch(pattern string, str string) string`: Finds the first match of a regular expression in a string.
- `findSubmatch(pattern string, str string) string`: Finds the first submatch of a regular expression in a string.
- `sortByString(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a string key in either ascending or descending order.
- `sortByInt(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by an integer key in either ascending or descending order.
- `sortByFloat(key string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a float key in either ascending or descending order.
- `sortByTime(key string, layout string, order string, arr []JSON): []JSON`: Sorts an array of JSON objects by a time key in either ascending or descending order. The format must be provided in Go's [date format](https://pkg.go.dev/time#pkg-constants).
The following helper functions provided by Go's `text/template` are available: The following helper functions provided by Go's `text/template` are available:
-3
View File
@@ -66,9 +66,6 @@ pages:
# hide-location: true # hide-location: true
- type: markets - type: markets
# The link to go to when clicking on the symbol in the UI,
# {SYMBOL} will be substituded with the symbol for each market
symbol-link-template: https://www.tradingview.com/symbols/{SYMBOL}/news
markets: markets:
- symbol: SPY - symbol: SPY
name: S&P 500 name: S&P 500
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

+10
View File
@@ -53,6 +53,16 @@ theme:
primary-color: 97 13 80 primary-color: 97 13 80
``` ```
### Gruvbox Dark
![screenshot](images/themes/gruvbox.png)
```yaml
theme:
background-color: 0 0 16
primary-color: 43 59 81
positive-color: 61 66 44
negative-color: 6 96 59
```
### Kanagawa Dark ### Kanagawa Dark
![screenshot](images/themes/kanagawa-dark.png) ![screenshot](images/themes/kanagawa-dark.png)
```yaml ```yaml
+55
View File
@@ -219,3 +219,58 @@ func (p *proxyOptionsField) UnmarshalYAML(node *yaml.Node) error {
return nil return nil
} }
type queryParametersField map[string][]string
func (q *queryParametersField) UnmarshalYAML(node *yaml.Node) error {
var decoded map[string]any
if err := node.Decode(&decoded); err != nil {
return err
}
*q = make(queryParametersField)
// TODO: refactor the duplication in the switch cases if any more types get added
for key, value := range decoded {
switch v := value.(type) {
case string:
(*q)[key] = []string{v}
case int, int8, int16, int32, int64, float32, float64:
(*q)[key] = []string{fmt.Sprintf("%v", v)}
case bool:
(*q)[key] = []string{fmt.Sprintf("%t", v)}
case []string:
(*q)[key] = append((*q)[key], v...)
case []any:
for _, item := range v {
switch item := item.(type) {
case string:
(*q)[key] = append((*q)[key], item)
case int, int8, int16, int32, int64, float32, float64:
(*q)[key] = append((*q)[key], fmt.Sprintf("%v", item))
case bool:
(*q)[key] = append((*q)[key], fmt.Sprintf("%t", item))
default:
return fmt.Errorf("invalid query parameter value type: %T", item)
}
}
default:
return fmt.Errorf("invalid query parameter value type: %T", value)
}
}
return nil
}
func (q *queryParametersField) toQueryString() string {
query := url.Values{}
for key, values := range *q {
for _, value := range values {
query.Add(key, value)
}
}
return query.Encode()
}
+36 -12
View File
@@ -242,7 +242,7 @@ func configFilesWatcher(
// needed for lastContents and lastIncludes because they get updated in multiple goroutines // needed for lastContents and lastIncludes because they get updated in multiple goroutines
mu := sync.Mutex{} mu := sync.Mutex{}
checkForContentChangesBeforeCallback := func() { parseAndCompareBeforeCallback := func() {
currentContents, currentIncludes, err := parseYAMLIncludes(mainFilePath) currentContents, currentIncludes, err := parseYAMLIncludes(mainFilePath)
if err != nil { if err != nil {
onErr(fmt.Errorf("parsing main file contents for comparison: %w", err)) onErr(fmt.Errorf("parsing main file contents for comparison: %w", err))
@@ -268,15 +268,22 @@ func configFilesWatcher(
const debounceDuration = 500 * time.Millisecond const debounceDuration = 500 * time.Millisecond
var debounceTimer *time.Timer var debounceTimer *time.Timer
debouncedCallback := func() { debouncedParseAndCompareBeforeCallback := func() {
if debounceTimer != nil { if debounceTimer != nil {
debounceTimer.Stop() debounceTimer.Stop()
debounceTimer.Reset(debounceDuration) debounceTimer.Reset(debounceDuration)
} else { } else {
debounceTimer = time.AfterFunc(debounceDuration, checkForContentChangesBeforeCallback) debounceTimer = time.AfterFunc(debounceDuration, parseAndCompareBeforeCallback)
} }
} }
deleteLastInclude := func(filePath string) {
mu.Lock()
defer mu.Unlock()
fileAbsPath, _ := filepath.Abs(filePath)
delete(lastIncludes, fileAbsPath)
}
go func() { go func() {
for { for {
select { select {
@@ -285,16 +292,33 @@ func configFilesWatcher(
return return
} }
if event.Has(fsnotify.Write) { if event.Has(fsnotify.Write) {
debouncedCallback() debouncedParseAndCompareBeforeCallback()
} else if event.Has(fsnotify.Remove) { } else if event.Has(fsnotify.Rename) {
func() { // on linux the file will no longer be watched after a rename, on windows
mu.Lock() // it will continue to be watched with the new name but we have no access to
defer mu.Unlock() // the new name in this event in order to stop watching it manually and match the
fileAbsPath, _ := filepath.Abs(event.Name) // behavior in linux, may lead to weird unintended behaviors on windows as we're
delete(lastIncludes, fileAbsPath) // only handling renames from linux's perspective
}() // see https://github.com/fsnotify/fsnotify/issues/255
debouncedCallback() // remove the old file from our manually tracked includes, calling
// debouncedParseAndCompareBeforeCallback will re-add it if it's still
// required after it triggers
deleteLastInclude(event.Name)
// wait for file to maybe get created again
// see https://github.com/glanceapp/glance/pull/358
for i := 0; i < 10; i++ {
if _, err := os.Stat(event.Name); err == nil {
break
}
time.Sleep(200 * time.Millisecond)
}
debouncedParseAndCompareBeforeCallback()
} else if event.Has(fsnotify.Remove) {
deleteLastInclude(event.Name)
debouncedParseAndCompareBeforeCallback()
} }
case err, isOpen := <-watcher.Errors: case err, isOpen := <-watcher.Errors:
if !isOpen { if !isOpen {
+2 -2
View File
@@ -103,7 +103,7 @@ func runDiagnostic() {
fmt.Println("Glance version: " + buildVersion) fmt.Println("Glance version: " + buildVersion)
fmt.Println("Go version: " + runtime.Version()) fmt.Println("Go version: " + runtime.Version())
fmt.Printf("Platform: %s / %s / %d CPUs\n", runtime.GOOS, runtime.GOARCH, runtime.NumCPU()) fmt.Printf("Platform: %s / %s / %d CPUs\n", runtime.GOOS, runtime.GOARCH, runtime.NumCPU())
fmt.Println("In Docker container: " + boolToString(isRunningInsideDockerContainer(), "yes", "no")) fmt.Println("In Docker container: " + ternary(isRunningInsideDockerContainer(), "yes", "no"))
fmt.Printf("\nChecking network connectivity, this may take up to %d seconds...\n\n", int(httpTestRequestTimeout.Seconds())) fmt.Printf("\nChecking network connectivity, this may take up to %d seconds...\n\n", int(httpTestRequestTimeout.Seconds()))
@@ -129,7 +129,7 @@ func runDiagnostic() {
fmt.Printf( fmt.Printf(
"%s %s %s| %dms\n", "%s %s %s| %dms\n",
boolToString(step.err == nil, "✓ Can", "✗ Can't"), ternary(step.err == nil, "✓ Can", "✗ Can't"),
step.name, step.name,
extraInfo, extraInfo,
step.elapsed.Milliseconds(), step.elapsed.Milliseconds(),
+1 -1
View File
@@ -68,7 +68,7 @@ func newApplication(config *config) (*application, error) {
for w := range column.Widgets { for w := range column.Widgets {
widget := column.Widgets[w] widget := column.Widgets[w]
app.widgetByID[widget.id()] = widget app.widgetByID[widget.GetID()] = widget
widget.setProviders(providers) widget.setProviders(providers)
} }
+5
View File
@@ -284,7 +284,9 @@ function setupGroups() {
for (let i = 0; i < titles.length; i++) { for (let i = 0; i < titles.length; i++) {
titles[i].classList.remove("widget-group-title-current"); titles[i].classList.remove("widget-group-title-current");
titles[i].setAttribute("aria-selected", "false");
tabs[i].classList.remove("widget-group-content-current"); tabs[i].classList.remove("widget-group-content-current");
tabs[i].setAttribute("aria-hidden", "true");
} }
if (current < t) { if (current < t) {
@@ -296,7 +298,9 @@ function setupGroups() {
current = t; current = t;
title.classList.add("widget-group-title-current"); title.classList.add("widget-group-title-current");
title.setAttribute("aria-selected", "true");
tabs[t].classList.add("widget-group-content-current"); tabs[t].classList.add("widget-group-content-current");
tabs[t].setAttribute("aria-hidden", "false");
}); });
} }
} }
@@ -670,6 +674,7 @@ async function setupPage() {
setupLazyImages(); setupLazyImages();
} finally { } finally {
pageElement.classList.add("content-ready"); pageElement.classList.add("content-ready");
pageElement.setAttribute("aria-busy", "false");
for (let i = 0; i < contentReadyCallbacks.length; i++) { for (let i = 0; i < contentReadyCallbacks.length; i++) {
contentReadyCallbacks[i](); contentReadyCallbacks[i]();
+23 -5
View File
@@ -110,7 +110,7 @@
.visited-indicator:not(.text-truncate)::after, .visited-indicator:not(.text-truncate)::after,
.visited-indicator.text-truncate::before, .visited-indicator.text-truncate::before,
.bookmarks-link:not(.bookmarks-link-no-arrow)::after { .bookmarks-link:not(.bookmarks-link-no-arrow)::after {
content: '↗'; content: '↗' / "";
margin-left: 0.5em; margin-left: 0.5em;
display: inline-block; display: inline-block;
position: relative; position: relative;
@@ -189,7 +189,7 @@
} }
.expand-toggle-button-icon::before { .expand-toggle-button-icon::before {
content: ''; content: '' / "";
font-size: 0.8rem; font-size: 0.8rem;
transform: rotate(90deg); transform: rotate(90deg);
line-height: 1; line-height: 1;
@@ -341,6 +341,19 @@ html, body, .body-content {
height: 100%; height: 100%;
} }
h1, h2, h3, h4, h5 {
font: inherit;
}
.visually-hidden {
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
a { a {
text-decoration: none; text-decoration: none;
color: inherit; color: inherit;
@@ -539,6 +552,10 @@ kbd:active {
z-index: 1; z-index: 1;
} }
.summary::-webkit-details-marker {
display: none;
}
.details[open] .summary { .details[open] .summary {
margin-bottom: .8rem; margin-bottom: .8rem;
} }
@@ -563,7 +580,7 @@ kbd:active {
} }
.summary::after { .summary::after {
content: "◀"; content: "◀" / "";
font-size: 1.2em; font-size: 1.2em;
position: absolute; position: absolute;
top: 0; top: 0;
@@ -822,7 +839,7 @@ details[open] .summary::after {
} }
.list-horizontal-text > *:not(:last-child)::after { .list-horizontal-text > *:not(:last-child)::after {
content: '•'; content: '•' / "";
color: var(--color-text-subdue); color: var(--color-text-subdue);
margin: 0 0.4rem; margin: 0 0.4rem;
position: relative; position: relative;
@@ -1115,7 +1132,7 @@ details[open] .summary::after {
.calendar-date { .calendar-date {
padding: 0.4rem 0; padding: 0.4rem 0;
color: var(--color-text-paragraph); color: var(--color-text-base);
position: relative; position: relative;
border-radius: var(--border-radius); border-radius: var(--border-radius);
background: none; background: none;
@@ -1256,6 +1273,7 @@ details[open] .summary::after {
.dns-stats-graph-bar > .blocked { .dns-stats-graph-bar > .blocked {
background-color: var(--color-negative); background-color: var(--color-negative);
flex-basis: calc(var(--percent) - 1px);
} }
.dns-stats-graph-column:nth-child(even) .dns-stats-graph-time { .dns-stats-graph-column:nth-child(even) .dns-stats-graph-time {
+5 -3
View File
@@ -27,9 +27,7 @@ var globalTemplateFunctions = template.FuncMap{
"formatPrice": func(price float64) string { "formatPrice": func(price float64) string {
return intl.Sprintf("%.2f", price) return intl.Sprintf("%.2f", price)
}, },
"dynamicRelativeTimeAttrs": func(t interface{ Unix() int64 }) template.HTMLAttr { "dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs,
return template.HTMLAttr(`data-dynamic-relative-time="` + strconv.FormatInt(t.Unix(), 10) + `"`)
},
"formatServerMegabytes": func(mb uint64) template.HTML { "formatServerMegabytes": func(mb uint64) template.HTML {
var value string var value string
var label string var label string
@@ -81,3 +79,7 @@ func formatApproxNumber(count int) string {
return strconv.FormatFloat(float64(count)/1_000_000, 'f', 1, 64) + "m" return strconv.FormatFloat(float64(count)/1_000_000, 'f', 1, 64) + "m"
} }
func dynamicRelativeTimeAttrs(t interface{ Unix() int64 }) template.HTMLAttr {
return template.HTMLAttr(`data-dynamic-relative-time="` + strconv.FormatInt(t.Unix(), 10) + `"`)
}
+3 -3
View File
@@ -59,8 +59,8 @@
{{ if ne $column.Queries $column.Blocked }} {{ if ne $column.Queries $column.Blocked }}
<div class="queries"></div> <div class="queries"></div>
{{ end }} {{ end }}
{{ if or (gt $column.Blocked 0) (and (lt $column.PercentTotal 15) (lt $column.PercentBlocked 10)) }} {{ if gt $column.PercentBlocked 0 }}
<div class="blocked" style="flex-basis: {{ $column.PercentBlocked }}%"></div> <div class="blocked" style="--percent: {{ $column.PercentBlocked }}%"></div>
{{ end }} {{ end }}
</div> </div>
{{ end }} {{ end }}
@@ -76,7 +76,7 @@
<summary class="summary">Top blocked domains</summary> <summary class="summary">Top blocked domains</summary>
<ul class="list list-gap-4 list-with-transition size-h5"> <ul class="list list-gap-4 list-with-transition size-h5">
{{ range .Stats.TopBlockedDomains }} {{ range .Stats.TopBlockedDomains }}
<li class="flex justify-between"> <li class="flex justify-between gap-10">
<div class="text-truncate rtl">{{ .Domain }}</div> <div class="text-truncate rtl">{{ .Domain }}</div>
<div class="text-right" style="width: 4rem;"><span class="color-highlight">{{ .PercentBlocked }}</span>%</div> <div class="text-right" style="width: 4rem;"><span class="color-highlight">{{ .PercentBlocked }}</span>%</div>
</li> </li>
@@ -1,10 +1,10 @@
{{ template "widget-base.html" . }} {{ template "widget-base.html" . }}
{{- define "widget-content" }} {{- define "widget-content" }}
<div class="dynamic-columns list-gap-20 list-with-separator"> <ul class="dynamic-columns list-gap-20 list-with-separator">
{{- range .Containers }} {{- range .Containers }}
<div class="docker-container flex items-center gap-15"> <li class="docker-container flex items-center gap-15">
<div class="shrink-0" data-popover-type="html" data-popover-position="above" data-popover-offset="0.25" data-popover-margin="0.1rem" data-popover-max-width="400px"> <div class="shrink-0" data-popover-type="html" data-popover-position="above" data-popover-offset="0.25" data-popover-margin="0.1rem" data-popover-max-width="400px" aria-hidden="true">
<img class="docker-container-icon{{ if .Icon.IsFlatIcon }} flat-icon{{ end }}" src="{{ .Icon.URL }}" alt="" loading="lazy"> <img class="docker-container-icon{{ if .Icon.IsFlatIcon }} flat-icon{{ end }}" src="{{ .Icon.URL }}" alt="" loading="lazy">
<div data-popover-html> <div data-popover-html>
<div class="color-highlight text-truncate block">{{ .Image }}</div> <div class="color-highlight text-truncate block">{{ .Image }}</div>
@@ -33,31 +33,33 @@
{{- end }} {{- end }}
</div> </div>
<div class="margin-left-auto shrink-0" data-popover-type="text" data-popover-position="above" data-popover-text="{{ .State }}"> <div class="margin-left-auto shrink-0" data-popover-type="text" data-popover-position="above" data-popover-text="{{ .State }}" aria-label="{{ .State }}">
{{ template "state-icon" .StateIcon }} {{ template "state-icon" .StateIcon }}
</div> </div>
</div>
<div class="visually-hidden" aria-label="{{ .StateText }}"></div>
</li>
{{- else }} {{- else }}
<div class="text-center">No containers available to show.</div> <div class="text-center">No containers available to show.</div>
{{- end }} {{- end }}
</div> </ul>
{{- end }} {{- end }}
{{- define "state-icon" }} {{- define "state-icon" }}
{{- if eq . "ok" }} {{- if eq . "ok" }}
<svg class="docker-container-status-icon" fill="var(--color-positive)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"> <svg class="docker-container-status-icon" fill="var(--color-positive)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z" clip-rule="evenodd" />
</svg> </svg>
{{- else if eq . "warn" }} {{- else if eq . "warn" }}
<svg class="docker-container-status-icon" fill="var(--color-negative)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"> <svg class="docker-container-status-icon" fill="var(--color-negative)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" aria-hidden="true">
<path fill-rule="evenodd" d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z" clip-rule="evenodd" />
</svg> </svg>
{{- else if eq . "paused" }} {{- else if eq . "paused" }}
<svg class="docker-container-status-icon" fill="var(--color-text-base)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"> <svg class="docker-container-status-icon" fill="var(--color-text-base)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" aria-hidden="true">
<path fill-rule="evenodd" d="M2 10a8 8 0 1 1 16 0 8 8 0 0 1-16 0Zm5-2.25A.75.75 0 0 1 7.75 7h.5a.75.75 0 0 1 .75.75v4.5a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1-.75-.75v-4.5Zm4 0a.75.75 0 0 1 .75-.75h.5a.75.75 0 0 1 .75.75v4.5a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1-.75-.75v-4.5Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M2 10a8 8 0 1 1 16 0 8 8 0 0 1-16 0Zm5-2.25A.75.75 0 0 1 7.75 7h.5a.75.75 0 0 1 .75.75v4.5a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1-.75-.75v-4.5Zm4 0a.75.75 0 0 1 .75-.75h.5a.75.75 0 0 1 .75.75v4.5a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1-.75-.75v-4.5Z" clip-rule="evenodd" />
</svg> </svg>
{{- else }} {{- else }}
<svg class="docker-container-status-icon" fill="var(--color-text-base)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"> <svg class="docker-container-status-icon" fill="var(--color-text-base)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" aria-hidden="true">
<path fill-rule="evenodd" d="M18 10a8 8 0 1 1-16 0 8 8 0 0 1 16 0ZM8.94 6.94a.75.75 0 1 1-1.061-1.061 3 3 0 1 1 2.871 5.026v.345a.75.75 0 0 1-1.5 0v-.5c0-.72.57-1.172 1.081-1.287A1.5 1.5 0 1 0 8.94 6.94ZM10 15a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z" clip-rule="evenodd" /> <path fill-rule="evenodd" d="M18 10a8 8 0 1 1-16 0 8 8 0 0 1 16 0ZM8.94 6.94a.75.75 0 1 1-1.061-1.061 3 3 0 1 1 2.871 5.026v.345a.75.75 0 0 1-1.5 0v-.5c0-.72.57-1.172 1.081-1.287A1.5 1.5 0 1 0 8.94 6.94ZM10 15a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z" clip-rule="evenodd" />
</svg> </svg>
{{- end }} {{- end }}
+9 -8
View File
@@ -4,17 +4,18 @@
{{ define "widget-content" }} {{ define "widget-content" }}
<div class="widget-group-header"> <div class="widget-group-header">
<div class="widget-header gap-20"> <div class="widget-header gap-20" role="tablist">
{{ range $i, $widget := .Widgets }} {{- range $i, $widget := .Widgets }}
<button class="widget-group-title{{ if eq $i 0 }} widget-group-title-current{{ end }}"{{ if ne "" .TitleURL }} data-title-url="{{ .TitleURL }}"{{ end }}>{{ $widget.Title }}</button> <button class="widget-group-title{{ if eq $i 0 }} widget-group-title-current{{ end }}"{{ if ne "" .TitleURL }} data-title-url="{{ .TitleURL }}"{{ end }} aria-selected="{{ if eq $i 0 }}true{{ else }}false{{ end }}" arial-level="2" role="tab" aria-controls="widget-{{ .GetID }}-tabpanel-{{ $i }}" id="widget-{{ .GetID }}-tab-{{ $i }}">{{ $widget.Title }}</button>
{{ end }} {{- end }}
</div> </div>
</div> </div>
<div class="widget-group-contents"> <div class="widget-group-contents">
{{ range $i, $widget := .Widgets }} {{- range $i, $widget := .Widgets }}
<div class="widget-group-content{{ if eq $i 0 }} widget-group-content-current{{ end }}">{{ .Render }}</div> <div class="widget-group-content{{ if eq $i 0 }} widget-group-content-current{{ end }}" id="widget-{{ .GetID }}-tabpanel-{{ $i }}" role="tabpanel" aria-labelledby="widget-{{ .GetID }}-tab-{{ $i }}" aria-hidden="{{ if eq $i 0 }}false{{ else }}true{{ end }}">
{{ end }} {{- .Render -}}
</div>
{{- end }}
</div> </div>
{{ end }} {{ end }}
+11 -9
View File
@@ -25,7 +25,7 @@
{{ define "navigation-links" }} {{ define "navigation-links" }}
{{ range .App.Config.Pages }} {{ range .App.Config.Pages }}
<a href="{{ $.App.Config.Server.BaseURL }}/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}">{{ .Title }}</a> <a href="{{ $.App.Config.Server.BaseURL }}/{{ .Slug }}" class="nav-item{{ if eq .Slug $.Page.Slug }} nav-item-current{{ end }}"{{ if eq .Slug $.Page.Slug }} aria-current="page"{{ end }}>{{ .Title }}</a>
{{ end }} {{ end }}
{{ end }} {{ end }}
@@ -35,10 +35,10 @@
<div class="header-container content-bounds"> <div class="header-container content-bounds">
<div class="header flex padding-inline-widget widget-content-frame"> <div class="header flex padding-inline-widget widget-content-frame">
<!-- TODO: Replace G with actual logo, first need an actual logo --> <!-- TODO: Replace G with actual logo, first need an actual logo -->
<div class="logo">{{ if ne "" .App.Config.Branding.LogoURL }}<img src="{{ .App.Config.Branding.LogoURL }}" alt="">{{ else if ne "" .App.Config.Branding.LogoText }}{{ .App.Config.Branding.LogoText }}{{ else }}G{{ end }}</div> <div class="logo" aria-hidden="true">{{ if ne "" .App.Config.Branding.LogoURL }}<img src="{{ .App.Config.Branding.LogoURL }}" alt="">{{ else if ne "" .App.Config.Branding.LogoText }}{{ .App.Config.Branding.LogoText }}{{ else }}G{{ end }}</div>
<div class="nav flex grow"> <nav class="nav flex grow">
{{ template "navigation-links" . }} {{ template "navigation-links" . }}
</div> </nav>
</div> </div>
</div> </div>
{{ end }} {{ end }}
@@ -57,17 +57,19 @@
</div> </div>
<div class="content-bounds grow"> <div class="content-bounds grow">
<div class="page" id="page"> <main class="page" id="page" aria-live="polite" aria-busy="true">
<h1 class="visually-hidden">{{ .Page.Title }}</h1>
<div class="page-content" id="page-content"></div> <div class="page-content" id="page-content"></div>
<div class="page-loading-container"> <div class="page-loading-container">
<!-- TODO: add a bigger/better loading indicator --> <!-- TODO: add a bigger/better loading indicator -->
<div class="loading-icon"></div> <div class="visually-hidden">Loading</div>
<div class="loading-icon" aria-hidden="true"></div>
</div> </div>
</div> </main>
</div> </div>
{{ if not .App.Config.Branding.HideFooter }} {{ if not .App.Config.Branding.HideFooter }}
<div class="footer flex items-center flex-column"> <footer class="footer flex items-center flex-column">
{{ if eq "" .App.Config.Branding.CustomFooter }} {{ if eq "" .App.Config.Branding.CustomFooter }}
<div> <div>
<a class="size-h3" href="https://github.com/glanceapp/glance" target="_blank" rel="noreferrer">Glance</a> {{ if ne "dev" .App.Version }}<a class="visited-indicator" title="Release notes" href="https://github.com/glanceapp/glance/releases/tag/{{ .App.Version }}" target="_blank" rel="noreferrer">{{ .App.Version }}</a>{{ else }}({{ .App.Version }}){{ end }} <a class="size-h3" href="https://github.com/glanceapp/glance" target="_blank" rel="noreferrer">Glance</a> {{ if ne "dev" .App.Version }}<a class="visited-indicator" title="Release notes" href="https://github.com/glanceapp/glance/releases/tag/{{ .App.Version }}" target="_blank" rel="noreferrer">{{ .App.Version }}</a>{{ else }}({{ .App.Version }}){{ end }}
@@ -75,7 +77,7 @@
{{ else }} {{ else }}
{{ .App.Config.Branding.CustomFooter }} {{ .App.Config.Branding.CustomFooter }}
{{ end }} {{ end }}
</div> </footer>
{{ end }} {{ end }}
<div class="mobile-navigation-offset"></div> <div class="mobile-navigation-offset"></div>
@@ -6,7 +6,7 @@
<li class="flex thumbnail-parent gap-10 items-center"> <li class="flex thumbnail-parent gap-10 items-center">
<img class="video-horizontal-list-thumbnail thumbnail" loading="lazy" src="{{ .ThumbnailUrl }}" alt=""> <img class="video-horizontal-list-thumbnail thumbnail" loading="lazy" src="{{ .ThumbnailUrl }}" alt="">
<div class="min-width-0"> <div class="min-width-0">
<a class="block text-truncate color-primary-if-not-visited" href="{{ .Url }}">{{ .Title }}</a> <a class="block text-truncate color-primary-if-not-visited" href="{{ .Url }}" target="_blank" rel="noreferrer">{{ .Title }}</a>
<ul class="list-horizontal-text flex-nowrap"> <ul class="list-horizontal-text flex-nowrap">
<li class="shrink-0" {{ dynamicRelativeTimeAttrs .TimePosted }}></li> <li class="shrink-0" {{ dynamicRelativeTimeAttrs .TimePosted }}></li>
<li class="min-width-0"> <li class="min-width-0">
+2 -2
View File
@@ -2,9 +2,9 @@
{{- if not .HideHeader}} {{- if not .HideHeader}}
<div class="widget-header"> <div class="widget-header">
{{- if ne "" .TitleURL }} {{- if ne "" .TitleURL }}
<a href="{{ .TitleURL | safeURL }}" target="_blank" rel="noreferrer" class="uppercase">{{ .Title }}</a> <h2><a href="{{ .TitleURL | safeURL }}" target="_blank" rel="noreferrer" class="uppercase">{{ .Title }}</a></h2>
{{- else }} {{- else }}
<div class="uppercase">{{ .Title }}</div> <h2 class="uppercase">{{ .Title }}</h2>
{{- end }} {{- end }}
{{- if .IsWIP }} {{- if .IsWIP }}
<div data-popover-type="html" data-popover-position="above"> <div data-popover-type="html" data-popover-position="above">
+4 -8
View File
@@ -119,14 +119,6 @@ func parseRFC3339Time(t string) time.Time {
return parsed return parsed
} }
func boolToString(b bool, trueValue, falseValue string) string {
if b {
return trueValue
}
return falseValue
}
func normalizeVersionFormat(version string) string { func normalizeVersionFormat(version string) string {
version = strings.ToLower(strings.TrimSpace(version)) version = strings.ToLower(strings.TrimSpace(version))
@@ -186,3 +178,7 @@ func ternary[T any](condition bool, a, b T) T {
return b return b
} }
// Having compile time errors about unused variables is cool and all, but I don't want to
// have to constantly comment out my code while I'm working on it and testing things out
func ItsUsedTrustMeBro(...any) {}
+336 -45
View File
@@ -3,6 +3,7 @@ package glance
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
@@ -10,6 +11,11 @@ import (
"log/slog" "log/slog"
"math" "math"
"net/http" "net/http"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time" "time"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@@ -17,22 +23,40 @@ import (
var customAPIWidgetTemplate = mustParseTemplate("custom-api.html", "widget-base.html") var customAPIWidgetTemplate = mustParseTemplate("custom-api.html", "widget-base.html")
// Needs to be exported for the YAML unmarshaler to work
type CustomAPIRequest struct {
URL string `yaml:"url"`
AllowInsecure bool `yaml:"allow-insecure"`
Headers map[string]string `yaml:"headers"`
Parameters queryParametersField `yaml:"parameters"`
Method string `yaml:"method"`
BodyType string `yaml:"body-type"`
Body any `yaml:"body"`
bodyReader io.ReadSeeker `yaml:"-"`
httpRequest *http.Request `yaml:"-"`
}
type customAPIWidget struct { type customAPIWidget struct {
widgetBase `yaml:",inline"` widgetBase `yaml:",inline"`
URL string `yaml:"url"` *CustomAPIRequest `yaml:",inline"` // the primary request
Template string `yaml:"template"` Subrequests map[string]*CustomAPIRequest `yaml:"subrequests"`
Frameless bool `yaml:"frameless"` Template string `yaml:"template"`
Headers map[string]string `yaml:"headers"` Frameless bool `yaml:"frameless"`
APIRequest *http.Request `yaml:"-"` compiledTemplate *template.Template `yaml:"-"`
compiledTemplate *template.Template `yaml:"-"` CompiledHTML template.HTML `yaml:"-"`
CompiledHTML template.HTML `yaml:"-"`
} }
func (widget *customAPIWidget) initialize() error { func (widget *customAPIWidget) initialize() error {
widget.withTitle("Custom API").withCacheDuration(1 * time.Hour) widget.withTitle("Custom API").withCacheDuration(1 * time.Hour)
if widget.URL == "" { if err := widget.CustomAPIRequest.initialize(); err != nil {
return errors.New("URL is required") return fmt.Errorf("initializing primary request: %v", err)
}
for key := range widget.Subrequests {
if err := widget.Subrequests[key].initialize(); err != nil {
return fmt.Errorf("initializing subrequest %q: %v", key, err)
}
} }
if widget.Template == "" { if widget.Template == "" {
@@ -46,22 +70,11 @@ func (widget *customAPIWidget) initialize() error {
widget.compiledTemplate = compiledTemplate widget.compiledTemplate = compiledTemplate
req, err := http.NewRequest(http.MethodGet, widget.URL, nil)
if err != nil {
return err
}
for key, value := range widget.Headers {
req.Header.Add(key, value)
}
widget.APIRequest = req
return nil return nil
} }
func (widget *customAPIWidget) update(ctx context.Context) { func (widget *customAPIWidget) update(ctx context.Context) {
compiledHTML, err := fetchAndParseCustomAPI(widget.APIRequest, widget.compiledTemplate) compiledHTML, err := fetchAndParseCustomAPI(widget.CustomAPIRequest, widget.Subrequests, widget.compiledTemplate)
if !widget.canContinueUpdateAfterHandlingErr(err) { if !widget.canContinueUpdateAfterHandlingErr(err) {
return return
} }
@@ -73,39 +86,196 @@ func (widget *customAPIWidget) Render() template.HTML {
return widget.renderTemplate(widget, customAPIWidgetTemplate) return widget.renderTemplate(widget, customAPIWidgetTemplate)
} }
func fetchAndParseCustomAPI(req *http.Request, tmpl *template.Template) (template.HTML, error) { func (req *CustomAPIRequest) initialize() error {
emptyBody := template.HTML("") if req.URL == "" {
return errors.New("URL is required")
}
resp, err := defaultHTTPClient.Do(req) if req.Body != nil {
if req.Method == "" {
req.Method = http.MethodPost
}
if req.BodyType == "" {
req.BodyType = "json"
}
if req.BodyType != "json" && req.BodyType != "string" {
return errors.New("invalid body type, must be either 'json' or 'string'")
}
switch req.BodyType {
case "json":
encoded, err := json.Marshal(req.Body)
if err != nil {
return fmt.Errorf("marshaling body: %v", err)
}
req.bodyReader = bytes.NewReader(encoded)
case "string":
bodyAsString, ok := req.Body.(string)
if !ok {
return errors.New("body must be a string when body-type is 'string'")
}
req.bodyReader = strings.NewReader(bodyAsString)
}
} else if req.Method == "" {
req.Method = http.MethodGet
}
httpReq, err := http.NewRequest(strings.ToUpper(req.Method), req.URL, req.bodyReader)
if err != nil { if err != nil {
return emptyBody, err return err
}
if len(req.Parameters) > 0 {
httpReq.URL.RawQuery = req.Parameters.toQueryString()
}
if req.BodyType == "json" {
httpReq.Header.Set("Content-Type", "application/json")
}
for key, value := range req.Headers {
httpReq.Header.Add(key, value)
}
req.httpRequest = httpReq
return nil
}
type customAPIResponseData struct {
JSON decoratedGJSONResult
Response *http.Response
}
type customAPITemplateData struct {
*customAPIResponseData
subrequests map[string]*customAPIResponseData
}
func (data *customAPITemplateData) Subrequest(key string) *customAPIResponseData {
req, exists := data.subrequests[key]
if !exists {
// We have to panic here since there's nothing sensible we can return and the
// lack of an error would cause requested data to return zero values which
// would be confusing from the user's perspective. Go's template module
// handles recovering from panics and will return the panic message as an
// error during template execution.
panic(fmt.Sprintf("subrequest with key %q has not been defined", key))
}
return req
}
func fetchCustomAPIRequest(ctx context.Context, req *CustomAPIRequest) (*customAPIResponseData, error) {
if req.bodyReader != nil {
req.bodyReader.Seek(0, io.SeekStart)
}
client := ternary(req.AllowInsecure, defaultInsecureHTTPClient, defaultHTTPClient)
resp, err := client.Do(req.httpRequest.WithContext(ctx))
if err != nil {
return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body) bodyBytes, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return emptyBody, err return nil, err
} }
body := string(bodyBytes) body := strings.TrimSpace(string(bodyBytes))
if !gjson.Valid(body) { if body != "" && !gjson.Valid(body) {
truncatedBody, isTruncated := limitStringLength(body, 100) truncatedBody, isTruncated := limitStringLength(body, 100)
if isTruncated { if isTruncated {
truncatedBody += "... <truncated>" truncatedBody += "... <truncated>"
} }
slog.Error("Invalid response JSON in custom API widget", "url", req.URL.String(), "body", truncatedBody) slog.Error("Invalid response JSON in custom API widget", "url", req.httpRequest.URL.String(), "body", truncatedBody)
return emptyBody, errors.New("invalid response JSON") return nil, errors.New("invalid response JSON")
} }
var templateBuffer bytes.Buffer data := &customAPIResponseData{
data := customAPITemplateData{
JSON: decoratedGJSONResult{gjson.Parse(body)}, JSON: decoratedGJSONResult{gjson.Parse(body)},
Response: resp, Response: resp,
} }
return data, nil
}
func fetchAndParseCustomAPI(
primaryReq *CustomAPIRequest,
subReqs map[string]*CustomAPIRequest,
tmpl *template.Template,
) (template.HTML, error) {
var primaryData *customAPIResponseData
subData := make(map[string]*customAPIResponseData, len(subReqs))
var err error
if len(subReqs) == 0 {
// If there are no subrequests, we can fetch the primary request in a much simpler way
primaryData, err = fetchCustomAPIRequest(context.Background(), primaryReq)
} else {
// If there are subrequests, we need to fetch them concurrently
// and cancel all requests if any of them fail. There's probably
// a more elegant way to do this, but this works for now.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
var mu sync.Mutex // protects subData and err
wg.Add(1)
go func() {
defer wg.Done()
var localErr error
primaryData, localErr = fetchCustomAPIRequest(ctx, primaryReq)
mu.Lock()
if localErr != nil && err == nil {
err = localErr
cancel()
}
mu.Unlock()
}()
for key, req := range subReqs {
wg.Add(1)
go func() {
defer wg.Done()
var localErr error
var data *customAPIResponseData
data, localErr = fetchCustomAPIRequest(ctx, req)
mu.Lock()
if localErr == nil {
subData[key] = data
} else if err == nil {
err = localErr
cancel()
}
mu.Unlock()
}()
}
wg.Wait()
}
emptyBody := template.HTML("")
if err != nil {
return emptyBody, err
}
data := customAPITemplateData{
customAPIResponseData: primaryData,
subrequests: subData,
}
var templateBuffer bytes.Buffer
err = tmpl.Execute(&templateBuffer, &data) err = tmpl.Execute(&templateBuffer, &data)
if err != nil { if err != nil {
return emptyBody, err return emptyBody, err
@@ -118,11 +288,6 @@ type decoratedGJSONResult struct {
gjson.Result gjson.Result
} }
type customAPITemplateData struct {
JSON decoratedGJSONResult
Response *http.Response
}
func gJsonResultArrayToDecoratedResultArray(results []gjson.Result) []decoratedGJSONResult { func gJsonResultArrayToDecoratedResultArray(results []gjson.Result) []decoratedGJSONResult {
decoratedResults := make([]decoratedGJSONResult, len(results)) decoratedResults := make([]decoratedGJSONResult, len(results))
@@ -153,12 +318,12 @@ func (r *decoratedGJSONResult) String(key string) string {
return r.Get(key).String() return r.Get(key).String()
} }
func (r *decoratedGJSONResult) Int(key string) int64 { func (r *decoratedGJSONResult) Int(key string) int {
if key == "" { if key == "" {
return r.Result.Int() return int(r.Result.Int())
} }
return r.Get(key).Int() return int(r.Get(key).Int())
} }
func (r *decoratedGJSONResult) Float(key string) float64 { func (r *decoratedGJSONResult) Float(key string) float64 {
@@ -178,12 +343,28 @@ func (r *decoratedGJSONResult) Bool(key string) bool {
} }
var customAPITemplateFuncs = func() template.FuncMap { var customAPITemplateFuncs = func() template.FuncMap {
var regexpCacheMu sync.Mutex
var regexpCache = make(map[string]*regexp.Regexp)
getCachedRegexp := func(pattern string) *regexp.Regexp {
regexpCacheMu.Lock()
defer regexpCacheMu.Unlock()
regex, exists := regexpCache[pattern]
if !exists {
regex = regexp.MustCompile(pattern)
regexpCache[pattern] = regex
}
return regex
}
funcs := template.FuncMap{ funcs := template.FuncMap{
"toFloat": func(a int64) float64 { "toFloat": func(a int) float64 {
return float64(a) return float64(a)
}, },
"toInt": func(a float64) int64 { "toInt": func(a float64) int {
return int64(a) return int(a)
}, },
"add": func(a, b float64) float64 { "add": func(a, b float64) float64 {
return a + b return a + b
@@ -201,6 +382,89 @@ var customAPITemplateFuncs = func() template.FuncMap {
return a / b return a / b
}, },
"parseTime": customAPIFuncParseTime,
"toRelativeTime": dynamicRelativeTimeAttrs,
"parseRelativeTime": func(layout, value string) template.HTMLAttr {
// Shorthand to do both of the above with a single function call
return dynamicRelativeTimeAttrs(customAPIFuncParseTime(layout, value))
},
// The reason we flip the parameter order is so that you can chain multiple calls together like this:
// {{ .JSON.String "foo" | trimPrefix "bar" | doSomethingElse }}
// instead of doing this:
// {{ trimPrefix (.JSON.String "foo") "bar" | doSomethingElse }}
// since the piped value gets passed as the last argument to the function.
"trimPrefix": func(prefix, s string) string {
return strings.TrimPrefix(s, prefix)
},
"trimSuffix": func(suffix, s string) string {
return strings.TrimSuffix(s, suffix)
},
"trimSpace": strings.TrimSpace,
"replaceAll": func(old, new, s string) string {
return strings.ReplaceAll(s, old, new)
},
"findMatch": func(pattern, s string) string {
if s == "" {
return ""
}
return getCachedRegexp(pattern).FindString(s)
},
"findSubmatch": func(pattern, s string) string {
if s == "" {
return ""
}
regex := getCachedRegexp(pattern)
return itemAtIndexOrDefault(regex.FindStringSubmatch(s), 1, "")
},
"sortByString": func(key, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
sort.Slice(results, func(a, b int) bool {
if order == "asc" {
return results[a].String(key) < results[b].String(key)
}
return results[a].String(key) > results[b].String(key)
})
return results
},
"sortByInt": func(key, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
sort.Slice(results, func(a, b int) bool {
if order == "asc" {
return results[a].Int(key) < results[b].Int(key)
}
return results[a].Int(key) > results[b].Int(key)
})
return results
},
"sortByFloat": func(key, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
sort.Slice(results, func(a, b int) bool {
if order == "asc" {
return results[a].Float(key) < results[b].Float(key)
}
return results[a].Float(key) > results[b].Float(key)
})
return results
},
"sortByTime": func(key, layout, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
sort.Slice(results, func(a, b int) bool {
timeA := customAPIFuncParseTime(layout, results[a].String(key))
timeB := customAPIFuncParseTime(layout, results[b].String(key))
if order == "asc" {
return timeA.Before(timeB)
}
return timeA.After(timeB)
})
return results
},
} }
for key, value := range globalTemplateFunctions { for key, value := range globalTemplateFunctions {
@@ -211,3 +475,30 @@ var customAPITemplateFuncs = func() template.FuncMap {
return funcs return funcs
}() }()
func customAPIFuncParseTime(layout, value string) time.Time {
switch strings.ToLower(layout) {
case "unix":
asInt, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return time.Unix(0, 0)
}
return time.Unix(asInt, 0)
case "rfc3339":
layout = time.RFC3339
case "rfc3339nano":
layout = time.RFC3339Nano
case "datetime":
layout = time.DateTime
case "dateonly":
layout = time.DateOnly
}
parsed, err := time.Parse(layout, value)
if err != nil {
return time.Unix(0, 0)
}
return parsed
}
+356 -63
View File
@@ -1,24 +1,35 @@
package glance package glance
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"html/template" "html/template"
"io"
"log/slog" "log/slog"
"net/http" "net/http"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
) )
var dnsStatsWidgetTemplate = mustParseTemplate("dns-stats.html", "widget-base.html") var dnsStatsWidgetTemplate = mustParseTemplate("dns-stats.html", "widget-base.html")
const (
dnsStatsBars = 8
dnsStatsHoursSpan = 24
dnsStatsHoursPerBar int = dnsStatsHoursSpan / dnsStatsBars
)
type dnsStatsWidget struct { type dnsStatsWidget struct {
widgetBase `yaml:",inline"` widgetBase `yaml:",inline"`
TimeLabels [8]string `yaml:"-"` TimeLabels [8]string `yaml:"-"`
Stats *dnsStats `yaml:"-"` Stats *dnsStats `yaml:"-"`
piholeSessionID string `yaml:"-"`
HourFormat string `yaml:"hour-format"` HourFormat string `yaml:"hour-format"`
HideGraph bool `yaml:"hide-graph"` HideGraph bool `yaml:"hide-graph"`
@@ -31,11 +42,17 @@ type dnsStatsWidget struct {
Password string `yaml:"password"` Password string `yaml:"password"`
} }
const (
dnsServiceAdguard = "adguard"
dnsServicePihole = "pihole"
dnsServicePiholeV6 = "pihole-v6"
)
func makeDNSWidgetTimeLabels(format string) [8]string { func makeDNSWidgetTimeLabels(format string) [8]string {
now := time.Now() now := time.Now()
var labels [8]string var labels [dnsStatsBars]string
for h := 24; h > 0; h -= 3 { for h := dnsStatsHoursSpan; h > 0; h -= dnsStatsHoursPerBar {
labels[7-(h/3-1)] = strings.ToLower(now.Add(-time.Duration(h) * time.Hour).Format(format)) labels[7-(h/3-1)] = strings.ToLower(now.Add(-time.Duration(h) * time.Hour).Format(format))
} }
@@ -48,8 +65,12 @@ func (widget *dnsStatsWidget) initialize() error {
withTitleURL(string(widget.URL)). withTitleURL(string(widget.URL)).
withCacheDuration(10 * time.Minute) withCacheDuration(10 * time.Minute)
if widget.Service != "adguard" && widget.Service != "pihole" { switch widget.Service {
return errors.New("service must be either 'adguard' or 'pihole'") case dnsServiceAdguard:
case dnsServicePiholeV6:
case dnsServicePihole:
default:
return fmt.Errorf("service must be one of: %s, %s, %s", dnsServiceAdguard, dnsServicePihole, dnsServicePiholeV6)
} }
return nil return nil
@@ -59,10 +80,24 @@ func (widget *dnsStatsWidget) update(ctx context.Context) {
var stats *dnsStats var stats *dnsStats
var err error var err error
if widget.Service == "adguard" { switch widget.Service {
case dnsServiceAdguard:
stats, err = fetchAdguardStats(widget.URL, widget.AllowInsecure, widget.Username, widget.Password, widget.HideGraph) stats, err = fetchAdguardStats(widget.URL, widget.AllowInsecure, widget.Username, widget.Password, widget.HideGraph)
} else { case dnsServicePihole:
stats, err = fetchPiholeStats(widget.URL, widget.AllowInsecure, widget.Token, widget.HideGraph) stats, err = fetchPihole5Stats(widget.URL, widget.AllowInsecure, widget.Token, widget.HideGraph)
case dnsServicePiholeV6:
var newSessionID string
stats, newSessionID, err = fetchPiholeStats(
widget.URL,
widget.AllowInsecure,
widget.Password,
widget.piholeSessionID,
!widget.HideGraph,
!widget.HideTopDomains,
)
if err == nil {
widget.piholeSessionID = newSessionID
}
} }
if !widget.canContinueUpdateAfterHandlingErr(err) { if !widget.canContinueUpdateAfterHandlingErr(err) {
@@ -84,11 +119,11 @@ func (widget *dnsStatsWidget) Render() template.HTML {
type dnsStats struct { type dnsStats struct {
TotalQueries int TotalQueries int
BlockedQueries int BlockedQueries int // we don't actually use this anywhere in templates, maybe remove it later?
BlockedPercent int BlockedPercent int
ResponseTime int ResponseTime int
DomainsBlocked int DomainsBlocked int
Series [8]dnsStatsSeries Series [dnsStatsBars]dnsStatsSeries
TopBlockedDomains []dnsStatsBlockedDomain TopBlockedDomains []dnsStatsBlockedDomain
} }
@@ -123,13 +158,7 @@ func fetchAdguardStats(instanceURL string, allowInsecure bool, username, passwor
request.SetBasicAuth(username, password) request.SetBasicAuth(username, password)
var client requestDoer var client = ternary(allowInsecure, defaultInsecureHTTPClient, defaultHTTPClient)
if !allowInsecure {
client = defaultHTTPClient
} else {
client = defaultInsecureHTTPClient
}
responseJson, err := decodeJsonFromRequest[adguardStatsResponse](client, request) responseJson, err := decodeJsonFromRequest[adguardStatsResponse](client, request)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -150,7 +179,7 @@ func fetchAdguardStats(instanceURL string, allowInsecure bool, username, passwor
stats.BlockedPercent = int(float64(responseJson.BlockedQueries) / float64(responseJson.TotalQueries) * 100) stats.BlockedPercent = int(float64(responseJson.BlockedQueries) / float64(responseJson.TotalQueries) * 100)
for i := 0; i < topBlockedDomainsCount; i++ { for i := range topBlockedDomainsCount {
domain := responseJson.TopBlockedDomains[i] domain := responseJson.TopBlockedDomains[i]
var firstDomain string var firstDomain string
@@ -179,31 +208,27 @@ func fetchAdguardStats(instanceURL string, allowInsecure bool, username, passwor
queriesSeries := responseJson.QueriesSeries queriesSeries := responseJson.QueriesSeries
blockedSeries := responseJson.BlockedSeries blockedSeries := responseJson.BlockedSeries
const bars = 8 if len(queriesSeries) > dnsStatsHoursSpan {
const hoursSpan = 24 queriesSeries = queriesSeries[len(queriesSeries)-dnsStatsHoursSpan:]
const hoursPerBar int = hoursSpan / bars } else if len(queriesSeries) < dnsStatsHoursSpan {
queriesSeries = append(make([]int, dnsStatsHoursSpan-len(queriesSeries)), queriesSeries...)
if len(queriesSeries) > hoursSpan {
queriesSeries = queriesSeries[len(queriesSeries)-hoursSpan:]
} else if len(queriesSeries) < hoursSpan {
queriesSeries = append(make([]int, hoursSpan-len(queriesSeries)), queriesSeries...)
} }
if len(blockedSeries) > hoursSpan { if len(blockedSeries) > dnsStatsHoursSpan {
blockedSeries = blockedSeries[len(blockedSeries)-hoursSpan:] blockedSeries = blockedSeries[len(blockedSeries)-dnsStatsHoursSpan:]
} else if len(blockedSeries) < hoursSpan { } else if len(blockedSeries) < dnsStatsHoursSpan {
blockedSeries = append(make([]int, hoursSpan-len(blockedSeries)), blockedSeries...) blockedSeries = append(make([]int, dnsStatsHoursSpan-len(blockedSeries)), blockedSeries...)
} }
maxQueriesInSeries := 0 maxQueriesInSeries := 0
for i := 0; i < bars; i++ { for i := range dnsStatsBars {
queries := 0 queries := 0
blocked := 0 blocked := 0
for j := 0; j < hoursPerBar; j++ { for j := range dnsStatsHoursPerBar {
queries += queriesSeries[i*hoursPerBar+j] queries += queriesSeries[i*dnsStatsHoursPerBar+j]
blocked += blockedSeries[i*hoursPerBar+j] blocked += blockedSeries[i*dnsStatsHoursPerBar+j]
} }
stats.Series[i] = dnsStatsSeries{ stats.Series[i] = dnsStatsSeries{
@@ -220,35 +245,36 @@ func fetchAdguardStats(instanceURL string, allowInsecure bool, username, passwor
} }
} }
for i := 0; i < bars; i++ { for i := range dnsStatsBars {
stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100) stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100)
} }
return stats, nil return stats, nil
} }
type piholeStatsResponse struct { // Legacy Pi-hole stats response (before v6)
TotalQueries int `json:"dns_queries_today"` type pihole5StatsResponse struct {
QueriesSeries piholeQueriesSeries `json:"domains_over_time"` TotalQueries int `json:"dns_queries_today"`
BlockedQueries int `json:"ads_blocked_today"` QueriesSeries pihole5QueriesSeries `json:"domains_over_time"`
BlockedSeries map[int64]int `json:"ads_over_time"` BlockedQueries int `json:"ads_blocked_today"`
BlockedPercentage float64 `json:"ads_percentage_today"` BlockedSeries map[int64]int `json:"ads_over_time"`
TopBlockedDomains piholeTopBlockedDomains `json:"top_ads"` BlockedPercentage float64 `json:"ads_percentage_today"`
DomainsBlocked int `json:"domains_being_blocked"` TopBlockedDomains pihole5TopBlockedDomains `json:"top_ads"`
DomainsBlocked int `json:"domains_being_blocked"`
} }
// If the user has query logging disabled it's possible for domains_over_time to be returned as an // If the user has query logging disabled it's possible for domains_over_time to be returned as an
// empty array rather than a map which will prevent unmashalling the rest of the data so we use // empty array rather than a map which will prevent unmashalling the rest of the data so we use
// custom unmarshal behavior to fallback to an empty map. // custom unmarshal behavior to fallback to an empty map.
// See https://github.com/glanceapp/glance/issues/289 // See https://github.com/glanceapp/glance/issues/289
type piholeQueriesSeries map[int64]int type pihole5QueriesSeries map[int64]int
func (p *piholeQueriesSeries) UnmarshalJSON(data []byte) error { func (p *pihole5QueriesSeries) UnmarshalJSON(data []byte) error {
temp := make(map[int64]int) temp := make(map[int64]int)
err := json.Unmarshal(data, &temp) err := json.Unmarshal(data, &temp)
if err != nil { if err != nil {
*p = make(piholeQueriesSeries) *p = make(pihole5QueriesSeries)
} else { } else {
*p = temp *p = temp
} }
@@ -258,16 +284,16 @@ func (p *piholeQueriesSeries) UnmarshalJSON(data []byte) error {
// If user has some level of privacy enabled on Pihole, `json:"top_ads"` is an empty array // If user has some level of privacy enabled on Pihole, `json:"top_ads"` is an empty array
// Use custom unmarshal behavior to avoid not getting the rest of the valid data when unmarshalling // Use custom unmarshal behavior to avoid not getting the rest of the valid data when unmarshalling
type piholeTopBlockedDomains map[string]int type pihole5TopBlockedDomains map[string]int
func (p *piholeTopBlockedDomains) UnmarshalJSON(data []byte) error { func (p *pihole5TopBlockedDomains) UnmarshalJSON(data []byte) error {
// NOTE: do not change to piholeTopBlockedDomains type here or it will cause a stack overflow // NOTE: do not change to piholeTopBlockedDomains type here or it will cause a stack overflow
// because of the UnmarshalJSON method getting called recursively // because of the UnmarshalJSON method getting called recursively
temp := make(map[string]int) temp := make(map[string]int)
err := json.Unmarshal(data, &temp) err := json.Unmarshal(data, &temp)
if err != nil { if err != nil {
*p = make(piholeTopBlockedDomains) *p = make(pihole5TopBlockedDomains)
} else { } else {
*p = temp *p = temp
} }
@@ -275,7 +301,7 @@ func (p *piholeTopBlockedDomains) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func fetchPiholeStats(instanceURL string, allowInsecure bool, token string, noGraph bool) (*dnsStats, error) { func fetchPihole5Stats(instanceURL string, allowInsecure bool, token string, noGraph bool) (*dnsStats, error) {
if token == "" { if token == "" {
return nil, errors.New("missing API token") return nil, errors.New("missing API token")
} }
@@ -288,14 +314,8 @@ func fetchPiholeStats(instanceURL string, allowInsecure bool, token string, noGr
return nil, err return nil, err
} }
var client requestDoer var client = ternary(allowInsecure, defaultInsecureHTTPClient, defaultHTTPClient)
if !allowInsecure { responseJson, err := decodeJsonFromRequest[pihole5StatsResponse](client, request)
client = defaultHTTPClient
} else {
client = defaultInsecureHTTPClient
}
responseJson, err := decodeJsonFromRequest[piholeStatsResponse](client, request)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -339,7 +359,6 @@ func fetchPiholeStats(instanceURL string, allowInsecure bool, token string, noGr
} }
var lowestTimestamp int64 = 0 var lowestTimestamp int64 = 0
for timestamp := range responseJson.QueriesSeries { for timestamp := range responseJson.QueriesSeries {
if lowestTimestamp == 0 || timestamp < lowestTimestamp { if lowestTimestamp == 0 || timestamp < lowestTimestamp {
lowestTimestamp = timestamp lowestTimestamp = timestamp
@@ -348,11 +367,11 @@ func fetchPiholeStats(instanceURL string, allowInsecure bool, token string, noGr
maxQueriesInSeries := 0 maxQueriesInSeries := 0
for i := 0; i < 8; i++ { for i := range dnsStatsBars {
queries := 0 queries := 0
blocked := 0 blocked := 0
for j := 0; j < 18; j++ { for j := range 18 {
index := lowestTimestamp + int64(i*10800+j*600) index := lowestTimestamp + int64(i*10800+j*600)
queries += responseJson.QueriesSeries[index] queries += responseJson.QueriesSeries[index]
@@ -373,9 +392,283 @@ func fetchPiholeStats(instanceURL string, allowInsecure bool, token string, noGr
} }
} }
for i := 0; i < 8; i++ { for i := range dnsStatsBars {
stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100) stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100)
} }
return stats, nil return stats, nil
} }
func fetchPiholeStats(
instanceURL string,
allowInsecure bool,
password string,
sessionID string,
includeGraph bool,
includeTopDomains bool,
) (*dnsStats, string, error) {
instanceURL = strings.TrimRight(instanceURL, "/")
var client = ternary(allowInsecure, defaultInsecureHTTPClient, defaultHTTPClient)
fetchNewSessionID := func() error {
newSessionID, err := fetchPiholeSessionID(instanceURL, client, password)
if err != nil {
return err
}
sessionID = newSessionID
return nil
}
if sessionID == "" {
if err := fetchNewSessionID(); err != nil {
slog.Error("Failed to fetch Pihole v6 session ID", "error", err)
return nil, "", fmt.Errorf("fetching session ID: %v", err)
}
} else {
isValid, err := checkPiholeSessionIDIsValid(instanceURL, client, sessionID)
if err != nil {
slog.Error("Failed to check Pihole v6 session ID validity", "error", err)
return nil, "", fmt.Errorf("checking session ID: %v", err)
}
if !isValid {
if err := fetchNewSessionID(); err != nil {
slog.Error("Failed to renew Pihole v6 session ID", "error", err)
return nil, "", fmt.Errorf("renewing session ID: %v", err)
}
}
}
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
type statsResponseJson struct {
Queries struct {
Total int `json:"total"`
Blocked int `json:"blocked"`
PercentBlocked float64 `json:"percent_blocked"`
} `json:"queries"`
Gravity struct {
DomainsBlocked int `json:"domains_being_blocked"`
} `json:"gravity"`
}
statsRequest, _ := http.NewRequestWithContext(ctx, "GET", instanceURL+"/api/stats/summary", nil)
statsRequest.Header.Set("x-ftl-sid", sessionID)
var statsResponse statsResponseJson
var statsErr error
wg.Add(1)
go func() {
defer wg.Done()
statsResponse, statsErr = decodeJsonFromRequest[statsResponseJson](client, statsRequest)
if statsErr != nil {
cancel()
}
}()
type seriesResponseJson struct {
History []struct {
Timestamp int64 `json:"timestamp"`
Total int `json:"total"`
Blocked int `json:"blocked"`
} `json:"history"`
}
var seriesResponse seriesResponseJson
var seriesErr error
if includeGraph {
seriesRequest, _ := http.NewRequestWithContext(ctx, "GET", instanceURL+"/api/history", nil)
seriesRequest.Header.Set("x-ftl-sid", sessionID)
wg.Add(1)
go func() {
defer wg.Done()
seriesResponse, seriesErr = decodeJsonFromRequest[seriesResponseJson](client, seriesRequest)
}()
}
type topDomainsResponseJson struct {
Domains []struct {
Domain string `json:"domain"`
Count int `json:"count"`
} `json:"domains"`
TotalQueries int `json:"total_queries"`
BlockedQueries int `json:"blocked_queries"`
Took float64 `json:"took"`
}
var topDomainsResponse topDomainsResponseJson
var topDomainsErr error
if includeTopDomains {
topDomainsRequest, _ := http.NewRequestWithContext(ctx, "GET", instanceURL+"/api/stats/top_domains?blocked=true", nil)
topDomainsRequest.Header.Set("x-ftl-sid", sessionID)
wg.Add(1)
go func() {
defer wg.Done()
topDomainsResponse, topDomainsErr = decodeJsonFromRequest[topDomainsResponseJson](client, topDomainsRequest)
}()
}
wg.Wait()
partialContent := false
if statsErr != nil {
return nil, "", fmt.Errorf("fetching stats: %v", statsErr)
}
if includeGraph && seriesErr != nil {
slog.Error("Failed to fetch Pihole v6 graph data", "error", seriesErr)
partialContent = true
}
if includeTopDomains && topDomainsErr != nil {
slog.Error("Failed to fetch Pihole v6 top domains", "error", topDomainsErr)
partialContent = true
}
stats := &dnsStats{
TotalQueries: statsResponse.Queries.Total,
BlockedQueries: statsResponse.Queries.Blocked,
BlockedPercent: int(statsResponse.Queries.PercentBlocked),
DomainsBlocked: statsResponse.Gravity.DomainsBlocked,
}
if includeGraph && seriesErr == nil {
if len(seriesResponse.History) != 145 {
slog.Error(
"Pihole v6 graph data has unexpected length",
"length", len(seriesResponse.History),
"expected", 145,
)
partialContent = true
} else {
// The API from v5 used to return 144 data points, but v6 returns 145.
// We only show data from the last 24 hours hours, Pihole returns data
// points in a 10 minute interval, 24*(60/10) = 144. Why is there an extra
// data point? I don't know, but we'll just ignore the first one since it's
// the oldest data point.
history := seriesResponse.History[1:]
const interval = 10
const dataPointsPerBar = dnsStatsHoursPerBar * (60 / interval)
maxQueriesInSeries := 0
for i := range dnsStatsBars {
queries := 0
blocked := 0
for j := range dataPointsPerBar {
index := i*dataPointsPerBar + j
queries += history[index].Total
blocked += history[index].Blocked
}
if queries > maxQueriesInSeries {
maxQueriesInSeries = queries
}
stats.Series[i] = dnsStatsSeries{
Queries: queries,
Blocked: blocked,
}
if queries > 0 {
stats.Series[i].PercentBlocked = int(float64(blocked) / float64(queries) * 100)
}
}
for i := range dnsStatsBars {
stats.Series[i].PercentTotal = int(float64(stats.Series[i].Queries) / float64(maxQueriesInSeries) * 100)
}
}
}
if includeTopDomains && topDomainsErr == nil && len(topDomainsResponse.Domains) > 0 {
domains := make([]dnsStatsBlockedDomain, 0, len(topDomainsResponse.Domains))
for i := range topDomainsResponse.Domains {
d := &topDomainsResponse.Domains[i]
domains = append(domains, dnsStatsBlockedDomain{
Domain: d.Domain,
PercentBlocked: int(float64(d.Count) / float64(statsResponse.Queries.Blocked) * 100),
})
}
sort.Slice(domains, func(a, b int) bool {
return domains[a].PercentBlocked > domains[b].PercentBlocked
})
stats.TopBlockedDomains = domains[:min(len(domains), 5)]
}
return stats, sessionID, ternary(partialContent, errPartialContent, nil)
}
func fetchPiholeSessionID(instanceURL string, client *http.Client, password string) (string, error) {
requestBody := []byte(`{"password":"` + password + `"}`)
request, err := http.NewRequest("POST", instanceURL+"/api/auth", bytes.NewBuffer(requestBody))
if err != nil {
return "", fmt.Errorf("creating authentication request: %v", err)
}
request.Header.Set("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
return "", fmt.Errorf("sending authentication request: %v", err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("reading authentication response: %v", err)
}
var jsonResponse struct {
Session struct {
SID string `json:"sid"`
Message string `json:"message"`
} `json:"session"`
}
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return "", fmt.Errorf("parsing authentication response: %v", err)
}
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf(
"authentication request returned status %s with message '%s'",
response.Status, jsonResponse.Session.Message,
)
}
if jsonResponse.Session.SID == "" {
return "", fmt.Errorf(
"authentication response returned empty session ID, status code %d, message '%s'",
response.StatusCode, jsonResponse.Session.Message,
)
}
return jsonResponse.Session.SID, nil
}
func checkPiholeSessionIDIsValid(instanceURL string, client *http.Client, sessionID string) (bool, error) {
request, err := http.NewRequest("GET", instanceURL+"/api/auth", nil)
if err != nil {
return false, fmt.Errorf("creating session ID check request: %v", err)
}
request.Header.Set("x-ftl-sid", sessionID)
response, err := client.Do(request)
if err != nil {
return false, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusUnauthorized {
return false, fmt.Errorf("session ID check request returned status %s", response.Status)
}
return response.StatusCode == http.StatusOK, nil
}
+1 -1
View File
@@ -241,7 +241,7 @@ func isDockerContainerHidden(container *dockerContainerJsonResponse, hideByDefau
func fetchAllDockerContainersFromSock(socketPath string) ([]dockerContainerJsonResponse, error) { func fetchAllDockerContainersFromSock(socketPath string) ([]dockerContainerJsonResponse, error) {
client := &http.Client{ client := &http.Client{
Timeout: 3 * time.Second, Timeout: 5 * time.Second,
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath) return net.Dial("unix", socketPath)
+18 -16
View File
@@ -19,12 +19,13 @@ const extensionWidgetDefaultTitle = "Extension"
type extensionWidget struct { type extensionWidget struct {
widgetBase `yaml:",inline"` widgetBase `yaml:",inline"`
URL string `yaml:"url"` URL string `yaml:"url"`
FallbackContentType string `yaml:"fallback-content-type"` FallbackContentType string `yaml:"fallback-content-type"`
Parameters map[string]string `yaml:"parameters"` Parameters queryParametersField `yaml:"parameters"`
AllowHtml bool `yaml:"allow-potentially-dangerous-html"` Headers map[string]string `yaml:"headers"`
Extension extension `yaml:"-"` AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
cachedHTML template.HTML `yaml:"-"` Extension extension `yaml:"-"`
cachedHTML template.HTML `yaml:"-"`
} }
func (widget *extensionWidget) initialize() error { func (widget *extensionWidget) initialize() error {
@@ -46,6 +47,7 @@ func (widget *extensionWidget) update(ctx context.Context) {
URL: widget.URL, URL: widget.URL,
FallbackContentType: widget.FallbackContentType, FallbackContentType: widget.FallbackContentType,
Parameters: widget.Parameters, Parameters: widget.Parameters,
Headers: widget.Headers,
AllowHtml: widget.AllowHtml, AllowHtml: widget.AllowHtml,
}) })
@@ -82,10 +84,11 @@ const (
) )
type extensionRequestOptions struct { type extensionRequestOptions struct {
URL string `yaml:"url"` URL string `yaml:"url"`
FallbackContentType string `yaml:"fallback-content-type"` FallbackContentType string `yaml:"fallback-content-type"`
Parameters map[string]string `yaml:"parameters"` Parameters queryParametersField `yaml:"parameters"`
AllowHtml bool `yaml:"allow-potentially-dangerous-html"` Headers map[string]string `yaml:"headers"`
AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
} }
type extension struct { type extension struct {
@@ -109,14 +112,13 @@ func convertExtensionContent(options extensionRequestOptions, content []byte, co
func fetchExtension(options extensionRequestOptions) (extension, error) { func fetchExtension(options extensionRequestOptions) (extension, error) {
request, _ := http.NewRequest("GET", options.URL, nil) request, _ := http.NewRequest("GET", options.URL, nil)
if len(options.Parameters) > 0 {
query := url.Values{} request.URL.RawQuery = options.Parameters.toQueryString()
for key, value := range options.Parameters {
query.Set(key, value)
} }
request.URL.RawQuery = query.Encode() for key, value := range options.Headers {
request.Header.Add(key, value)
}
response, err := http.DefaultClient.Do(request) response, err := http.DefaultClient.Do(request)
if err != nil { if err != nil {
+2 -2
View File
@@ -124,6 +124,7 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro
for i := range marketRequests { for i := range marketRequests {
request, _ := http.NewRequest("GET", fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?range=1mo&interval=1d", marketRequests[i].Symbol), nil) request, _ := http.NewRequest("GET", fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?range=1mo&interval=1d", marketRequests[i].Symbol), nil)
setBrowserUserAgentHeader(request)
requests = append(requests, request) requests = append(requests, request)
} }
@@ -165,8 +166,7 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro
points := svgPolylineCoordsFromYValues(100, 50, maybeCopySliceWithoutZeroValues(prices)) points := svgPolylineCoordsFromYValues(100, 50, maybeCopySliceWithoutZeroValues(prices))
currency, exists := currencyToSymbol[response.Chart.Result[0].Meta.Currency] currency, exists := currencyToSymbol[strings.ToUpper(response.Chart.Result[0].Meta.Currency)]
if !exists { if !exists {
currency = response.Chart.Result[0].Meta.Currency currency = response.Chart.Result[0].Meta.Currency
} }
+11 -1
View File
@@ -8,8 +8,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand/v2"
"net/http" "net/http"
"strconv"
"sync" "sync"
"sync/atomic"
"time" "time"
) )
@@ -35,8 +38,15 @@ type requestDoer interface {
Do(*http.Request) (*http.Response, error) Do(*http.Request) (*http.Response, error)
} }
var userAgentPersistentVersion atomic.Int32
func setBrowserUserAgentHeader(request *http.Request) { func setBrowserUserAgentHeader(request *http.Request) {
request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0") if rand.IntN(2000) == 0 {
userAgentPersistentVersion.Store(rand.Int32N(5))
}
version := strconv.Itoa(130 + int(userAgentPersistentVersion.Load()))
request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:"+version+".0) Gecko/20100101 Firefox/"+version+".0")
} }
func decodeJsonFromRequest[T any](client requestDoer, request *http.Request) (T, error) { func decodeJsonFromRequest[T any](client requestDoer, request *http.Request) (T, error) {
+1 -1
View File
@@ -56,7 +56,7 @@ func (widget *videosWidget) initialize() error {
widget.Channels = append(widget.Channels, make([]string, len(widget.Playlists))...) widget.Channels = append(widget.Channels, make([]string, len(widget.Playlists))...)
for i := range widget.Playlists { for i := range widget.Playlists {
widget.Channels[initialLen+i] = "playlist:" + widget.Playlists[i] widget.Channels[initialLen+i] = videosWidgetPlaylistPrefix + widget.Playlists[i]
} }
} }
+2 -2
View File
@@ -121,13 +121,13 @@ type widget interface {
// These need to be exported because they get called in templates // These need to be exported because they get called in templates
Render() template.HTML Render() template.HTML
GetType() string GetType() string
GetID() uint64
initialize() error initialize() error
requiresUpdate(*time.Time) bool requiresUpdate(*time.Time) bool
setProviders(*widgetProviders) setProviders(*widgetProviders)
update(context.Context) update(context.Context)
setID(uint64) setID(uint64)
id() uint64
handleRequest(w http.ResponseWriter, r *http.Request) handleRequest(w http.ResponseWriter, r *http.Request)
setHideHeader(bool) setHideHeader(bool)
} }
@@ -184,7 +184,7 @@ func (w *widgetBase) update(ctx context.Context) {
} }
func (w *widgetBase) id() uint64 { func (w *widgetBase) GetID() uint64 {
return w.ID return w.ID
} }
+2 -1
View File
@@ -202,7 +202,8 @@ func Collect(req *SystemInfoRequest) (*SystemInfo, []error) {
// keeps returning a single sensor with key "ACPI\\ThermalZone\\TZ00_0" which // keeps returning a single sensor with key "ACPI\\ThermalZone\\TZ00_0" which
// doesn't seem to be the CPU sensor or correspond to anything useful when // doesn't seem to be the CPU sensor or correspond to anything useful when
// compared against the temperatures Libre Hardware Monitor reports // compared against the temperatures Libre Hardware Monitor reports
if runtime.GOOS != "windows" { // also disabled on openbsd because it's not implemented by go-psutil
if runtime.GOOS != "windows" && runtime.GOOS != "openbsd" {
sensorReadings, err := sensors.SensorsTemperatures() sensorReadings, err := sensors.SensorsTemperatures()
if err == nil { if err == nil {
if req.CPUTempSensor != "" { if req.CPUTempSensor != "" {