diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1f9515c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +# https://docs.docker.com/build/building/context/#dockerignore-files +# Ignore all files by default +* + +# Only add necessary files to the Docker build context (Dockerfiles are always included implicitly) +!/build/ +!/internal/ +!/pkg/ +!/go.mod +!/go.sum +!main.go diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..d1b4aaa --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +glanceapp@duck.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..cda2741 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [glanceapp] diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml new file mode 100644 index 0000000..bdd4fe6 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -0,0 +1,37 @@ +name: Bug report +description: Let us know if something isn't working as expected +labels: ["bug report"] +body: + - type: markdown + attributes: + value: | + > [!NOTE] + > + > Do not prefix your title with "[BUG]", "[Bug report]", etc., a label will be added automatically. + + If you're unsure whether you're experiencing a bug or not, consider using the [Discussions](https://github.com/glanceapp/glance/discussions) or [Discord](https://discord.com/invite/7KQ7Xa9kJd) to ask for help. + + Please include only the information you think is relevant to the bug: + + * How did you install Glance? (Docker container, manual binary install, etc) + * Which version of Glance are you using? + * Include the relevant parts of your `glance.yml` if applicable (widget, data source, properties used, etc) + * Include any relevant logs or screenshots if applicable + * Is the issue specific to a certain browser or OS? + * Steps to reliably reproduce the issue + * Are you hosting Glance on a VPS? + * Anything else you think might be relevant + + **No need to copy the above list into your description, it's just a guide to help you provide the most useful information.** + + - type: textarea + id: description + validations: + required: true + attributes: + label: Description + + - type: markdown + attributes: + value: | + Thank you for taking the time to submit a bug report. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..e8c34af --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: Discussions + url: https://github.com/glanceapp/glance/discussions + about: For help, feedback, guides, resources and more + - name: Discord + url: https://discord.com/invite/7KQ7Xa9kJd + about: Much like the discussions but more chatty diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml new file mode 100644 index 0000000..d8f5343 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -0,0 +1,33 @@ +name: Feature request +description: Share your ideas for new features or improvements +labels: ["feature request"] +body: + - type: markdown + attributes: + value: | + > [!NOTE] + > + > Do not prefix your title with "[REQUEST]", "[Feature request]", etc., a label will be added automatically. + + Please provide a detailed description of what the feature would do and what it would look like: + + * What problem would this feature solve? + * Are there any potential downsides to this feature? + * If applicable, what would the configuration for this feature look like? + * Are there any existing examples of this feature in other software? + * If applicable, include any external documentation required to implement this feature + * Anything else you think might be relevant + + **No need to copy the above list into your description, it's just a guide to help you provide the most useful information.** + + - type: textarea + id: description + validations: + required: true + attributes: + label: Description + + - type: markdown + attributes: + value: | + Thank you for taking the time to submit your idea. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..690586f --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1 @@ + diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..b06a6e3 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,9 @@ +# Security Policy + +## Supported Versions + +Security updates will be applied to the latest as well as previous minor version release depending on severity and if applicable. + +## Reporting a Vulnerability + +Please report any suspected security vulnerabilities to [glanceapp@duck.com](mailto:glanceapp@duck.com) and do not disclose them publicly. You should receive a response within a few days and if confirmed the issue will be resolved as soon as possible. diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..608d82e --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,39 @@ +name: Create release + +permissions: + contents: write + +on: + push: + tags: + - 'v*' + +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout the target Git reference + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Set up Golang + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Set up Docker buildx + uses: docker/setup-buildx-action@v3 + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: release diff --git a/.gitignore b/.gitignore index 062999d..2cd84fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /assets /build /playground -glance.yml +/.idea +/glance*.yml diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..7153a4f --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,69 @@ +project_name: glanceapp/glance + +checksum: + disable: true + +builds: + - binary: glance + env: + - CGO_ENABLED=0 + goos: + - linux + - openbsd + - freebsd + - windows + - darwin + goarch: + - amd64 + - arm64 + - arm + - 386 + goarm: + - 7 + ldflags: + - -s -w -X github.com/glanceapp/glance/internal/glance.buildVersion={{ .Tag }} + +archives: + - + name_template: "glance-{{ .Os }}-{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}" + files: + - nothing* + format_overrides: + - goos: windows + format: zip + +dockers: + - image_templates: + - &amd64_image "{{ .ProjectName }}:{{ .Tag }}-amd64" + build_flag_templates: + - --platform=linux/amd64 + goarch: amd64 + use: buildx + dockerfile: Dockerfile.goreleaser + + - image_templates: + - &arm64v8_image "{{ .ProjectName }}:{{ .Tag }}-arm64" + build_flag_templates: + - --platform=linux/arm64 + goarch: arm64 + use: buildx + dockerfile: Dockerfile.goreleaser + + - image_templates: + - &armv7_image "{{ .ProjectName }}:{{ .Tag }}-armv7" + build_flag_templates: + - --platform=linux/arm/v7 + goarch: arm + goarm: 7 + use: buildx + dockerfile: Dockerfile.goreleaser + +docker_manifests: + - name_template: "{{ .ProjectName }}:{{ .Tag }}" + image_templates: &multiarch_images + - *amd64_image + - *arm64v8_image + - *armv7_image + - name_template: "{{ .ProjectName }}:latest" + skip_push: auto + image_templates: *multiarch_images diff --git a/Dockerfile b/Dockerfile index 17156a2..0c4cc63 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,16 @@ -FROM alpine:3.19 - -ARG TARGETOS -ARG TARGETARCH +FROM golang:1.24.2-alpine3.21 AS builder WORKDIR /app -COPY build/glance-$TARGETOS-$TARGETARCH /app/glance +COPY . /app +RUN CGO_ENABLED=0 go build . + +FROM alpine:3.21 + +WORKDIR /app +COPY --from=builder /app/glance . + +HEALTHCHECK --timeout=10s --start-period=60s --interval=60s \ + CMD wget --spider -q http://localhost:8080/api/healthz EXPOSE 8080/tcp -ENTRYPOINT ["/app/glance"] +ENTRYPOINT ["/app/glance", "--config", "/app/config/glance.yml"] diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..bbfa8ad --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,10 @@ +FROM alpine:3.21 + +WORKDIR /app +COPY glance . + +HEALTHCHECK --timeout=10s --start-period=60s --interval=60s \ + CMD wget --spider -q http://localhost:8080/api/healthz + +EXPOSE 8080/tcp +ENTRYPOINT ["/app/glance", "--config", "/app/config/glance.yml"] diff --git a/README.md b/README.md index 23f085b..ca16c49 100644 --- a/README.md +++ b/README.md @@ -1,90 +1,436 @@
What if you could see everything at a...
Install • Configuration • Themes
+Install • Configuration • Discord • Sponsor
+Community widgets • Preconfigured pages • Themes
- + -### Features -#### Various widgets +## Features +### Various widgets * RSS feeds * Subreddit posts -* Weather -* Bookmarks -* Latest YouTube videos from specific channels -* Calendar -* Stocks -* iframe -* Twitch channels & top games -* GitHub releases -* Site monitor +* Hacker News posts +* Weather forecasts +* YouTube channel uploads +* Twitch channels +* Market prices +* Docker containers status +* Server stats +* Custom widgets +* [and many more...](docs/configuration.md) -#### Themeable - +### Fast and lightweight +* Low memory usage +* Few dependencies +* Minimal vanilla JS +* Single <20mb binary available for multiple OSs & architectures and just as small Docker container +* Uncached pages usually load within ~1s (depending on internet speed and number of widgets) -#### Optimized for mobile devices - +### Tons of customizability +* Different layouts +* As many pages/tabs as you need +* Numerous configuration options for each widget +* Multiple styles for some widgets +* Custom CSS -#### Fast and lightweight -* Minimal JS, no bloated frameworks -* Very few dependencies -* Single, easily distributed <15mb binary and just as small docker container -* All requests are parallelized, uncached pages usually load within ~1s (depending on internet speed and number of widgets) +### Optimized for mobile devices +Because you'll want to take it with you on the go. -### Configuration -Checkout the [configuration docs](docs/configuration.md) to learn more. A [preconfigured page](docs/configuration.md#preconfigured-page) is also available to get you started quickly. + -### Installation -> [!CAUTION] -> -> The project is under active development, expect things to break every once in a while. +### Themeable +Easily create your own theme by tweaking a few numbers or choose from one of the [already available themes](docs/themes.md). -#### Manual -Checkout the [releases page](https://github.com/glanceapp/glance/releases) for available binaries. You can place the binary inside `/opt/glance/` and have it start with your server via a [systemd service](https://linuxhandbook.com/create-systemd-services/). To specify a different path for the config file use the `--config` option: + +Powered by Glance
+ logo-url: /assets/logo.png + favicon-url: /assets/logo.png +``` + +### Properties + +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| hide-footer | bool | no | false | +| custom-footer | string | no | | +| logo-text | string | no | G | +| logo-url | string | no | | +| favicon-url | string | no | | + +#### `hide-footer` +Hides the footer when set to `true`. + +#### `custom-footer` +Specify custom HTML to use for the footer. + +#### `logo-text` +Specify custom text to use instead of the "G" found in the navigation. + +#### `logo-url` +Specify a URL to a custom image to use instead of the "G" found in the navigation. If both `logo-text` and `logo-url` are set, only `logo-url` will be used. + +#### `favicon-url` +Specify a URL to a custom image to use for the favicon. + ## Theme Theming is done through a top level `theme` property. Values for the colors are in [HSL](https://giggster.com/guide/basics/hue-saturation-lightness/) (hue, saturation, lightness) format. You can use a color picker [like this one](https://hslpicker.com/) to convert colors from other formats to HSL. The values are separated by a space and `%` is not required for any of the numbers. @@ -173,13 +274,13 @@ theme: 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. ### Properties | Name | Type | Required | Default | | ---- | ---- | -------- | ------- | -| light | bool | no | false | +| light | boolean | no | false | | background-color | HSL | no | 240 8 9 | | primary-color | HSL | no | 43 50 70 | | positive-color | HSL | no | same as `primary-color` | @@ -227,6 +328,9 @@ theme: > .widget-type-rss a { > font-size: 1.5rem; > } +> ``` +> +> In addition, you can also use the `css-class` property which is available on every widget to set custom class names for individual widgets. ## Pages & Columns @@ -250,18 +354,50 @@ pages: ``` ### Properties -| Name | Type | Required | -| ---- | ---- | -------- | -| title | string | yes | -| slug | string | no | -| columns | array | yes | +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| name | string | yes | | +| slug | string | no | | +| width | string | no | | +| center-vertically | boolean | no | false | +| hide-desktop-navigation | boolean | no | false | +| expand-mobile-page-navigation | boolean | no | false | +| show-mobile-header | boolean | no | false | +| columns | array | yes | | -#### `title` +#### `name` The name of the page which gets shown in the navigation bar. #### `slug` The URL friendly version of the title which is used to access the page. For example if the title of the page is "RSS Feeds" you can make the page accessible via `localhost:8080/feeds` by setting the slug to `feeds`. If not defined, it will automatically be generated from the title. +#### `width` +The maximum width of the page on desktop. Possible values are `slim` and `wide`. + +* default: `1600px` (when no value is specified) +* slim: `1100px` +* wide: `1920px` + +> [!NOTE] +> +> When using `slim`, the maximum number of columns allowed for that page is `2`. + +#### `center-vertically` +When set to `true`, vertically centers the content on the page. Has no effect if the content is taller than the height of the viewport. + +#### `hide-desktop-navigation` +Whether to show the navigation links at the top of the page on desktop. + +#### `expand-mobile-page-navigation` +Whether the mobile page navigation should be expanded by default. + +#### `show-mobile-header` +Whether to show a header displaying the name of the page on mobile. The header purposefully has a lot of vertical whitespace in order to push the content down and make it easier to reach on tall devices. + +Preview: + + + ### Columns Columns are defined for each page using a `columns` property. There are two types of columns - `full` and `small`, which refers to their width. A small column takes up a fixed amount of width (300px) and a full column takes up the all of the remaining width. You can have up to 3 columns per page and you must have either 1 or 2 full columns. Example: @@ -339,7 +475,9 @@ pages: | ---- | ---- | -------- | | type | string | yes | | title | string | no | +| title-url | string | no | | cache | string | no | +| css-class | string | no | #### `type` Used to specify the widget. @@ -347,6 +485,9 @@ Used to specify the widget. #### `title` The title of the widget. If left blank it will be defined by the widget. +#### `title-url` +The URL to go to when clicking on the widget's title. If left blank it will be defined by the widget (if available). + #### `cache` How long to keep the fetched data in memory. The value is a string and must be a number followed by one of s, m, h, d. Examples: @@ -361,6 +502,9 @@ cache: 1d # 1 day > > Not all widgets can have their cache duration modified. The calendar and weather widgets update on the hour and this cannot be changed. +#### `css-class` +Set custom CSS classes for the specific widget instance. + ### RSS Display a list of articles from multiple RSS feeds. @@ -384,35 +528,88 @@ Example: | ---- | ---- | -------- | ------- | | style | string | no | vertical-list | | feeds | array | yes | +| thumbnail-height | float | no | 10 | +| card-height | float | no | 27 | | limit | integer | no | 25 | +| preserve-order | bool | no | false | +| single-line-titles | boolean | no | false | | collapse-after | integer | no | 5 | -##### `style` -Used to change the appearance of the widget. Possible values are `vertical-list` and `horizontal-cards` where the former is intended to be used within a small column and the latter a full column. Below are previews of each style. - -`vertical-list` - - - -`horizontal-cards` - - - -##### `feeds` -An array of RSS/atom feeds. The title can optionally be changed. - -###### Properties for each feed -| Name | Type | Required | Default | -| ---- | ---- | -------- | ------- | -| url | string | yes | | -| title | string | no | the title provided by the feed | - ##### `limit` The maximum number of articles to show. ##### `collapse-after` How many articles are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse. +##### `preserve-order` +When set to `true`, the order of the articles will be preserved as they are in the feeds. Useful if a feed uses its own sorting order which denotes the importance of the articles. If you use this property while having a lot of feeds, it's recommended to set a `limit` to each individual feed since if the first defined feed has 15 articles, the articles from the second feed will start after the 15th article in the list. + +##### `single-line-titles` +When set to `true`, truncates the title of each post if it exceeds one line. Only applies when the style is set to `vertical-list`. + +##### `style` +Used to change the appearance of the widget. Possible values are: + +* `vertical-list` - suitable for `full` and `small` columns +* `detailed-list` - suitable for `full` columns +* `horizontal-cards` - suitable for `full` columns +* `horizontal-cards-2` - suitable for `full` columns + +Below is a preview of each style: + +`vertical-list` + + + +`detailed-list` + + + +`horizontal-cards` + + + +`horizontal-cards-2` + + + +##### `thumbnail-height` +Used to modify the height of the thumbnails. Works only when the style is set to `horizontal-cards`. The default value is `10` and the units are `rem`, if you want to for example double the height of the thumbnails you can set it to `20`. + +##### `card-height` +Used to modify the height of cards when using the `horizontal-cards-2` style. The default value is `27` and the units are `rem`. + +##### `feeds` +An array of RSS/atom feeds. The title can optionally be changed. + +###### Properties for each feed +| Name | Type | Required | Default | Notes | +| ---- | ---- | -------- | ------- | ----- | +| url | string | yes | | | +| title | string | no | the title provided by the feed | | +| hide-categories | boolean | no | false | Only applicable for `detailed-list` style | +| hide-description | boolean | no | false | Only applicable for `detailed-list` style | +| limit | integer | no | | | +| item-link-prefix | string | no | | | +| headers | key (string) & value (string) | no | | | + +###### `limit` +The maximum number of articles to show from that specific feed. Useful if you have a feed which posts a lot of articles frequently and you want to prevent it from excessively pushing down articles from other feeds. + +###### `item-link-prefix` +If an RSS feed isn't returning item links with a base domain and Glance has failed to automatically detect the correct domain you can manually add a prefix to each link with this property. + +###### `headers` +Optionally specify the headers that will be sent with the request. Example: + +```yaml +- type: rss + feeds: + - url: https://domain.com/rss + headers: + User-Agent: Custom User Agent +``` + ### Videos Display a list of the latest videos from specific YouTube channels. @@ -433,10 +630,18 @@ Preview: | Name | Type | Required | Default | | ---- | ---- | -------- | ------- | | channels | array | yes | | +| playlists | array | no | | | limit | integer | no | 25 | +| style | string | no | horizontal-cards | +| collapse-after | integer | no | 7 | +| collapse-after-rows | integer | no | 4 | +| include-shorts | boolean | no | false | +| video-url-template | string | no | https://www.youtube.com/watch?v={VIDEO-ID} | ##### `channels` -A list of channel IDs. One way of getting the ID of a channel is going to the channel's page and clicking on its description: +A list of channels IDs. + +One way of getting the ID of a channel is going to the channel's page and clicking on its description:  @@ -444,9 +649,48 @@ Then scroll down and click on "Share channel", then "Copy channel ID":  +##### `playlists` + +A list of playlist IDs: + +```yaml +- type: videos + playlists: + - PL8mG-RkN2uTyZZ00ObwZxxoG_nJbs3qec + - PL8mG-RkN2uTxTK4m_Vl2dYR9yE41kRdBg +``` + ##### `limit` The maximum number of videos to show. +##### `collapse-after` +Specify the number of videos to show when using the `vertical-list` style before the "SHOW MORE" button appears. + +##### `collapse-after-rows` +Specify the number of rows to show when using the `grid-cards` style before the "SHOW MORE" button appears. + +##### `style` +Used to change the appearance of the widget. Possible values are `horizontal-cards`, `vertical-list` and `grid-cards`. + +Preview of `vertical-list`: + + + +Preview of `grid-cards`: + + + +##### `video-url-template` +Used to replace the default link for videos. Useful when you're running your own YouTube front-end. Example: + +```yaml +video-url-template: https://invidious.your-domain.com/watch?v={VIDEO-ID} +``` + +Placeholders: + +`{VIDEO-ID}` - the ID of the video + ### Hacker News Display a list of posts from [Hacker News](https://news.ycombinator.com/). @@ -466,13 +710,86 @@ Preview: | ---- | ---- | -------- | ------- | | limit | integer | no | 15 | | collapse-after | integer | no | 5 | +| comments-url-template | string | no | https://news.ycombinator.com/item?id={POST-ID} | +| sort-by | string | no | top | +| extra-sort-by | string | no | | + +##### `comments-url-template` +Used to replace the default link for post comments. Useful if you want to use an alternative front-end. Example: + +```yaml +comments-url-template: https://www.hckrnws.com/stories/{POST-ID} +``` + +Placeholders: + +`{POST-ID}` - the ID of the post + +##### `sort-by` +Used to specify the order in which the posts should get returned. Possible values are `top`, `new`, and `best`. + +##### `extra-sort-by` +Can be used to specify an additional sort which will be applied on top of the already sorted posts. By default does not apply any extra sorting and the only available option is `engagement`. + +The `engagement` sort tries to place the posts with the most points and comments on top, also prioritizing recent over old posts. + +### Lobsters +Display a list of posts from [Lobsters](https://lobste.rs). + +Example: + +```yaml +- type: lobsters + sort-by: hot + tags: + - go + - security + - linux + limit: 15 + collapse-after: 5 +``` + +Preview: + + +#### Properties +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| instance-url | string | no | https://lobste.rs/ | +| custom-url | string | no | | +| limit | integer | no | 15 | +| collapse-after | integer | no | 5 | +| sort-by | string | no | hot | +| tags | array | no | | + +##### `instance-url` +The base URL for a lobsters instance hosted somewhere other than on lobste.rs. Example: + +```yaml +instance-url: https://www.journalduhacker.net/ +``` + +##### `custom-url` +A custom URL to retrieve lobsters posts from. If this is specified, the `instance-url`, `sort-by` and `tags` properties are ignored. + +##### `limit` +The maximum number of posts to show. + +##### `collapse-after` +How many posts are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse. + +##### `sort-by` +The sort order in which posts are returned. Possible options are `hot` and `new`. + +##### `tags` +Limit to posts containing one of the given tags. **You cannot specify a sort order when filtering by tags, it will default to `hot`.** ### Reddit Display a list of posts from a specific subreddit. > [!WARNING] > -> Reddit does not allow unauthorized API access from VPS IPs, if you're hosting Glance on a VPS you will get a 403 response. As a workaround you can route the traffic from Glance through a VPN. +> Reddit does not allow unauthorized API access from VPS IPs, if you're hosting Glance on a VPS you will get a 403 response. As a workaround you can route the traffic from Glance through a VPN or your own HTTP proxy using the `request-url-template` property. Example: @@ -486,8 +803,17 @@ Example: | ---- | ---- | -------- | ------- | | subreddit | string | yes | | | style | string | no | vertical-list | +| show-thumbnails | boolean | no | false | +| show-flairs | boolean | no | false | | limit | integer | no | 15 | | collapse-after | integer | no | 5 | +| comments-url-template | string | no | https://www.reddit.com/{POST-PATH} | +| request-url-template | string | no | | +| proxy | string or multiple parameters | no | | +| sort-by | string | no | hot | +| top-period | string | no | day | +| search | string | no | | +| extra-sort-by | string | no | | ##### `subreddit` The subreddit for which to fetch the posts from. @@ -507,12 +833,617 @@ Used to change the appearance of the widget. Possible values are `vertical-list`  +##### `show-thumbnails` +Shows or hides thumbnails next to the post. This only works if the `style` is `vertical-list`. Preview: + + + +> [!NOTE] +> +> Thumbnails don't work for some subreddits due to Reddit's API not returning the thumbnail URL. No workaround for this yet. + +##### `show-flairs` +Shows post flairs when set to `true`. + ##### `limit` The maximum number of posts to show. ##### `collapse-after` How many posts are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse. Not available when using the `vertical-cards` and `horizontal-cards` styles. +##### `comments-url-template` +Used to replace the default link for post comments. Useful if you want to use the old Reddit design or any other 3rd party front-end. Example: + +```yaml +comments-url-template: https://old.reddit.com/{POST-PATH} +``` + +Placeholders: + +`{POST-PATH}` - the full path to the post, such as: + +``` +r/selfhosted/comments/bsp01i/welcome_to_rselfhosted_please_read_this_first/ +``` + +`{POST-ID}` - the ID that comes after `/comments/` + +`{SUBREDDIT}` - the subreddit name + +##### `request-url-template` +A custom request URL that will be used to fetch the data. This is useful when you're hosting Glance on a VPS where Reddit is blocking the requests and you want to route them through a proxy that accepts the URL as either a part of the path or a query parameter. + +Placeholders: + +`{REQUEST-URL}` - will be templated and replaced with the expanded request URL (i.e. https://www.reddit.com/r/selfhosted/hot.json). Example: + +``` +https://proxy/{REQUEST-URL} +https://your.proxy/?url={REQUEST-URL} +``` + +##### `proxy` +A custom HTTP/HTTPS proxy URL that will be used to fetch the data. This is useful when you're hosting Glance on a VPS where Reddit is blocking the requests and you want to bypass the restriction by routing the requests through a proxy. Example: + +```yaml +proxy: http://user:pass@proxy.com:8080 +proxy: https://user:pass@proxy.com:443 +``` + +Alternatively, you can specify the proxy URL as well as additional options by using multiple parameters: + +```yaml +proxy: + url: http://proxy.com:8080 + allow-insecure: true + timeout: 10s +``` + +###### `allow-insecure` +When set to `true`, allows the use of insecure connections such as when the proxy has a self-signed certificate. + +###### `timeout` +The maximum time to wait for a response from the proxy. The value is a string and must be a number followed by one of s, m, h, d. Example: `10s` for 10 seconds, `1m` for 1 minute, etc + +##### `sort-by` +Can be used to specify the order in which the posts should get returned. Possible values are `hot`, `new`, `top` and `rising`. + +##### `top-period` +Available only when `sort-by` is set to `top`. Possible values are `hour`, `day`, `week`, `month`, `year` and `all`. + +##### `search` +Keywords to search for. Searching within specific fields is also possible, **though keep in mind that Reddit may remove the ability to use any of these at any time**: + + + +##### `extra-sort-by` +Can be used to specify an additional sort which will be applied on top of the already sorted posts. By default does not apply any extra sorting and the only available option is `engagement`. + +The `engagement` sort tries to place the posts with the most points and comments on top, also prioritizing recent over old posts. + +### Search Widget +Display a search bar that can be used to search for specific terms on various search engines. + +Example: + +```yaml +- type: search + search-engine: duckduckgo + bangs: + - title: YouTube + shortcut: "!yt" + url: https://www.youtube.com/results?search_query={QUERY} +``` + +Preview: + + + +#### Keyboard shortcuts +| Keys | Action | Condition | +| ---- | ------ | --------- | +| S | Focus the search bar | Not already focused on another input field | +| Enter | Perform search in the same tab | Search input is focused and not empty | +| Ctrl + Enter | Perform search in a new tab | Search input is focused and not empty | +| Escape | Leave focus | Search input is focused | +| Up | Insert the last search query since the page was opened into the input field | Search input is focused | + +> [!TIP] +> +> You can use the property `new-tab` with a value of `true` if you want to show search results in a new tab by default. Ctrl + Enter will then show results in the same tab. + +#### Properties +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| search-engine | string | no | duckduckgo | +| new-tab | boolean | no | false | +| autofocus | boolean | no | false | +| placeholder | string | no | Type here to search… | +| bangs | array | no | | + +##### `search-engine` +Either a value from the table below or a URL to a custom search engine. Use `{QUERY}` to indicate where the query value gets placed. + +| Name | URL | +| ---- | --- | +| duckduckgo | `https://duckduckgo.com/?q={QUERY}` | +| google | `https://www.google.com/search?q={QUERY}` | + +##### `new-tab` +When set to `true`, swaps the shortcuts for showing results in the same or new tab, defaulting to showing results in a new tab. + +##### `autofocus` +When set to `true`, automatically focuses the search input on page load. + +##### `placeholder` +When set, modifies the text displayed in the input field before typing. + +##### `bangs` +What now? [Bangs](https://duckduckgo.com/bangs). They're shortcuts that allow you to use the same search box for many different sites. Assuming you have it configured, if for example you start your search input with `!yt` you'd be able to perform a search on YouTube: + + + +##### Properties for each bang +| Name | Type | Required | +| ---- | ---- | -------- | +| title | string | no | +| shortcut | string | yes | +| url | string | yes | + +###### `title` +Optional title that will appear on the right side of the search bar when the query starts with the associated shortcut. + +###### `shortcut` +Any value you wish to use as the shortcut for the search engine. It does not have to start with `!`. + +> [!IMPORTANT] +> +> In YAML some characters have special meaning when placed in the beginning of a value. If your shortcut starts with `!` (and potentially some other special characters) you'll have to wrap the value in quotes: +> ```yaml +> shortcut: "!yt" +>``` + +###### `url` +The URL of the search engine. Use `{QUERY}` to indicate where the query value gets placed. Examples: + +```yaml +url: https://www.reddit.com/search?q={QUERY} +url: https://store.steampowered.com/search/?term={QUERY} +url: https://www.amazon.com/s?k={QUERY} +``` + +### Group +Group multiple widgets into one using tabs. Widgets are defined using a `widgets` property exactly as you would on a page column. The only limitation is that you cannot place a group widget or a split column widget within a group widget. + +Example: + +```yaml +- type: group + widgets: + - type: reddit + subreddit: gamingnews + show-thumbnails: true + collapse-after: 6 + - type: reddit + subreddit: games + - type: reddit + subreddit: pcgaming + show-thumbnails: true +``` + +Preview: + + + +#### Sharing properties + +To avoid repetition you can use [YAML anchors](https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/) and share properties between widgets. + +Example: + +```yaml +- type: group + define: &shared-properties + type: reddit + show-thumbnails: true + collapse-after: 6 + widgets: + - subreddit: gamingnews + <<: *shared-properties + - subreddit: games + <<: *shared-properties + - subreddit: pcgaming + <<: *shared-properties +``` + +### Split Column +Splits a full sized column in half, allowing you to place widgets side by side horizontally. This is converted to a single column on mobile devices or if not enough width is available. Widgets are defined using a `widgets` property exactly as you would on a page column. + +Two widgets side by side in a `full` column: + + + +glance.ymlglance.ymlglance.ymlglance.ymlglance.yml{{ .JSON.String "text" }}
+``` +glance.ymlglance.yml{{ .JSON.String "text" }}
+{{ (.Subrequest "another-one").JSON.String "text" }}
+``` + +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" }} +{{ $anotherOne.JSON.String "text" }}
+``` + +You can also access the `.Response` property of a subrequest as you would with the main request: + +```yaml + template: | + {{ $anotherOne := .Subrequest "another-one" }} +{{ $anotherOne.Response.StatusCode }}
+``` + +> [!NOTE] +> +> Setting this property will override any query parameters that are already in the URL. + +```yaml +parameters: + param1: value1 + param2: + - item1 + - item2 +``` + +### 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). + +```yaml +- type: extension + url: https://domain.com/widget/display-a-message + allow-potentially-dangerous-html: true + parameters: + message: Hello, world! +``` + +#### Properties +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| url | string | yes | | +| fallback-content-type | string | no | | +| allow-potentially-dangerous-html | boolean | no | false | +| headers | key & value | no | | +| parameters | key & value | no | | + +##### `url` +The URL of the extension. **Note that the query gets stripped from this URL and the one defined by `parameters` gets used instead.** + +##### `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`. + +##### `headers` +Optionally specify the headers that will be sent with the request. Example: + +```yaml +headers: + x-api-key: ${SECRET_KEY} +``` + +##### `allow-potentially-dangerous-html` +Whether to allow the extension to display HTML. + +> [!WARNING] +> +> There's a reason this property is scary-sounding. It's intended to be used by developers who are comfortable with developing and using their own extensions. Do not enable it if you have no idea what it means or if you're not **absolutely sure** that the extension URL you're using is safe. + +##### `parameters` +A list of keys and values that will be sent to the extension as query paramters. + ### Weather Display weather information for a specific location. The data is provided by https://open-meteo.com/. @@ -521,9 +1452,19 @@ Example: ```yaml - type: weather units: metric + hour-format: 12h location: London, United Kingdom ``` +> [!NOTE] +> +> US cities which have common names can have their state specified as the second parameter as such: +> +> * Greenville, North Carolina, United States +> * Greenville, South Carolina, United States +> * Greenville, Mississippi, United States + + Preview:  @@ -536,7 +1477,9 @@ Each bar represents a 2 hour interval. The yellow background represents sunrise | ---- | ---- | -------- | ------- | | location | string | yes | | | units | string | no | metric | +| hour-format | string | no | 12h | | hide-location | boolean | no | false | +| show-area-name | boolean | no | false | ##### `location` The name of the city and country to fetch weather information for. Attempting to launch the applcation with an invalid location will result in an error. You can use the [gecoding API page](https://open-meteo.com/en/docs/geocoding-api) to search for your specific location. Glance will use the first result from the list if there are multiple. @@ -544,11 +1487,27 @@ The name of the city and country to fetch weather information for. Attempting to ##### `units` Whether to show the temperature in celsius or fahrenheit, possible values are `metric` or `imperial`. +#### `hour-format` +Whether to show the hours of the day in 12-hour format or 24-hour format. Possible values are `12h` and `24h`. + ##### `hide-location` Optionally don't display the location name on the widget. +##### `show-area-name` +Whether to display the state/administrative area in the location name. If set to `true` the location will be displayed as: + +``` +Greenville, North Carolina, United States +``` + +Otherwise, if set to `false` (which is the default) it'll be displayed as: + +``` +Greenville, United States +``` + ### Monitor -Display a list of sites and whether they are reachable (online) or not. This is determined by sending a HEAD request to the specified URL, if the response is 200 then the site is OK. The time it took to receive a response is also shown in milliseconds. +Display a list of sites and whether they are reachable (online) or not. This is determined by sending a GET request to the specified URL, if the response is 200 then the site is OK. The time it took to receive a response is also shown in milliseconds. Example: @@ -583,19 +1542,36 @@ You can hover over the "ERROR" text to view more information. #### Properties -| Name | Type | Required | -| ---- | ---- | -------- | -| sites | array | yes | | +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| sites | array | yes | | +| style | string | no | | +| show-failing-only | boolean | no | false | + +##### `show-failing-only` +Shows only a list of failing sites when set to `true`. + +##### `style` +Used to change the appearance of the widget. Possible values are `compact`. + +Preview of `compact`: + + ##### `sites` Properties for each site: -| Name | Type | Required | -| ---- | ---- | -------- | -| title | string | yes | -| url | string | yes | -| icon | string | no | +| Name | Type | Required | Default | +| ---- | ---- | -------- | ------- | +| title | string | yes | | +| url | string | yes | | +| check-url | string | no | | +| error-url | string | no | | +| icon | string | no | | +| allow-insecure | boolean | no | false | +| same-tab | boolean | no | false | +| alt-status-codes | array | no | | `title` @@ -603,24 +1579,62 @@ The title used to indicate the site. `url` -The URL which will be requested and its response will determine the status of the site. +The public facing URL of a monitored service, the user will be redirected here. If `check-url` is not specified, this is used as the status check. + +`check-url` + +The URL which will be requested and its response will determine the status of the site. If not specified, the `url` property is used. + +`error-url` + +If the monitored service returns an error, the user will be redirected here. If not specified, the `url` property is used. `icon` -Optional URL to an image which will be used as the icon for the site. Can be an external URL or internal via [server configured assets](#assets-path). +Optional URL to an image which will be used as the icon for the site. Can be an external URL or internal via [server configured assets](#assets-path). You can also directly use [Simple Icons](https://simpleicons.org/) via a `si:` prefix or [Dashboard Icons](https://github.com/walkxcode/dashboard-icons) via a `di:` prefix: + +```yaml +icon: si:jellyfin +icon: si:gitea +icon: si:adguard +``` + +> [!WARNING] +> +> Simple Icons are loaded externally and are hosted on `cdn.jsdelivr.net`, if you do not wish to depend on a 3rd party you are free to download the icons individually and host them locally. + +`allow-insecure` + +Whether to ignore invalid/self-signed certificates. + +`same-tab` + +Whether to open the link in the same or a new tab. + +`alt-status-codes` + +Status codes other than 200 that you want to return "OK". + +```yaml +alt-status-codes: + - 403 +``` ### Releases -Display a list of releases for specific repositories on Github. Draft releases and prereleases will not be shown. +Display a list of latest releases for specific repositories on Github, GitLab, Codeberg or Docker Hub. Example: ```yaml - type: releases + show-source-icon: true repositories: - - immich-app/immich - go-gitea/gitea - - dani-garcia/vaultwarden - jellyfin/jellyfin + - glanceapp/glance + - codeberg:redict/redict + - gitlab:fdroid/fdroidclient + - dockerhub:gotify/server ``` Preview: @@ -632,12 +1646,53 @@ Preview: | Name | Type | Required | Default | | ---- | ---- | -------- | ------- | | repositories | array | yes | | +| show-source-icon | boolean | no | false | | | token | string | no | | +| gitlab-token | string | no | | | limit | integer | no | 10 | | collapse-after | integer | no | 5 | ##### `repositories` -A list of repositores for which to fetch the latest release for. Only the name/repo is required, not the full URL. +A list of repositores to fetch the latest release for. Only the name/repo is required, not the full URL. A prefix can be specified for repositories hosted elsewhere such as GitLab, Codeberg and Docker Hub. Example: + +```yaml +repositories: + - gitlab:inkscape/inkscape + - dockerhub:glanceapp/glance + - codeberg:redict/redict +``` + +Official images on Docker Hub can be specified by omitting the owner: + +```yaml +repositories: + - dockerhub:nginx + - dockerhub:node + - dockerhub:alpine +``` + +You can also specify exact tags for Docker Hub images: + +```yaml +repositories: + - dockerhub:nginx:latest + - dockerhub:nginx:stable-alpine +``` + +To include prereleases you can specify the repository as an object and use the `include-prereleases` property: + +**Note: This feature is currently only available for GitHub repositories.** + +```yaml +repositories: + - gitlab:inkscape/inkscape + - repository: glanceapp/glance + include-prereleases: true + - codeberg:redict/redict +``` + +##### `show-source-icon` +Shows an icon of the source (GitHub/GitLab/Codeberg/Docker Hub) next to the repository name when set to `true`. ##### `token` Without authentication Github allows for up to 60 requests per hour. You can easily exceed this limit and start seeing errors if you're tracking lots of repositories or your cache time is low. To circumvent this you can [create a read only token from your Github account](https://github.com/settings/personal-access-tokens/new) and provide it here. @@ -649,7 +1704,7 @@ services: glance: image: glanceapp/glance environment: - - GITHUB_TOKEN:docker-compose.ymlHello, World!
+``` + +Note the use of `|` after `source:`, this allows you to insert a multi-line string. diff --git a/docs/custom-api.md b/docs/custom-api.md new file mode 100644 index 0000000..d42d1fc --- /dev/null +++ b/docs/custom-api.md @@ -0,0 +1,415 @@ +[Jump to function definitions](#functions) + +## Examples + +The best way to get an idea of how the templates work would be with a bunch examples. Here are the most common use cases: + +JSON response: + +```json +{ + "title": "My Title", + "content": "My Content", +} +``` + +To access the two fields in the JSON response, you would use the following: + +```html +Success!
+{{ else }} +Failed to fetch data
+{{ end }} +``` + +You can also access the response headers: + +```html +{{ .String "name" }} is {{ .Int "age" }} years old
+{{ end }} +``` + +Output: + +```html +Steve is 30 years old
+Alex is 25 years old
+John is 35 years old
+``` + +For other ways of selecting data from a JSON Lines response, have a look at the docs for [tidwall/gjson](https://github.com/tidwall/gjson/tree/master?tab=readme-ov-file#json-lines). For example, to get an array of all names, you can use the following: + +```html +{{ range .JSON.Array "..#.name" }} +{{ .String "" }}
+{{ end }} +``` + +Output: + +```html +Steve
+Alex
+John
+``` + +## Functions + +The following functions are available on the `JSON` object: + +- `String(key string) string`: Returns the value of the key as a string. +- `Int(key string) int`: Returns the value of the key as an integer. +- `Float(key string) float`: Returns the value of the key as a float. +- `Bool(key string) bool`: Returns the value of the key as a boolean. +- `Array(key string) []JSON`: Returns the value of the key as an array of `JSON` objects. +- `Exists(key string) bool`: Returns true if the key exists in the JSON object. + +The following helper functions provided by Glance are available: + +- `toFloat(i int) float`: Converts an integer to a float. +- `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. ``. +- `now() time.Time`: Returns the current time. +- `offsetNow(offset string) time.Time`: Returns the current time with an offset. The offset can be positive or negative and must be in the format "3h" "-1h" or "2h30m10s". +- `duration(str string) time.Duration`: Parses a string such as `1h`, `24h`, `5h30m`, etc into a `time.Duration`. +- `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. +- `sub(a, b float) float`: Subtracts two numbers. +- `mul(a, b float) float`: Multiplies 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. +- `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. +- `replaceMatches(pattern string, replacement string, str string) string`: Replaces all occurrences of a regular expression 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). +- `concat(strings ...string) string`: Concatenates multiple strings together. +- `unique(key string, arr []JSON) []JSON`: Returns a unique array of JSON objects based on the given key. + +The following helper functions provided by Go's `text/template` are available: + +- `eq(a, b any) bool`: Compares two values for equality. +- `ne(a, b any) bool`: Compares two values for inequality. +- `lt(a, b any) bool`: Compares two values for less than. +- `lte(a, b any) bool`: Compares two values for less than or equal to. +- `gt(a, b any) bool`: Compares two values for greater than. +- `gte(a, b any) bool`: Compares two values for greater than or equal to. +- `and(a, b bool) bool`: Returns true if both values are true. +- `or(a, b bool) bool`: Returns true if either value is true. +- `not(a bool) bool`: Returns the opposite of the value. +- `index(a any, b int) any`: Returns the value at the specified index of an array. +- `len(a any) int`: Returns the length of an array. +- `printf(format string, a ...any) string`: Returns a formatted string. diff --git a/docs/extensions.md b/docs/extensions.md new file mode 100644 index 0000000..b6719c1 --- /dev/null +++ b/docs/extensions.md @@ -0,0 +1,164 @@ +# Extensions + +> [!IMPORTANT] +> +> **This document as well as the extensions feature are a work in progress. The API may change in the future. You are responsible for maintaining your own extensions.** + +## Overview + +With the intention of requiring minimal knowledge in order to develop extensions, rather than being a convoluted protocol they are nothing more than an HTTP request to a server that returns a few special headers. The exchange between Glance and extensions can be seen in the following diagram: + + + +If you know how to setup an HTTP server and a bit of HTML and CSS you're ready to start building your own extensions. + +> [!TIP] +> +> By default, the extension widget has a cache time of 30 minutes. To avoid having to restart Glance after every extension change you can set the cache time of the widget to 1 second: +> ```yaml +> - type: extension +> url: http://localhost:8081 +> cache: 1s +> ``` + +## Headers + +### `Widget-Title` +Used to specify the title of the widget. If not provided, the widget's title will be "Extension". + +### `Widget-Title-URL` +Used to specify the URL that will be opened when the widget's title is clicked. If the user has specified a `title-url` in their config, it will take precedence over this header. + +### `Widget-Content-Type` +Used to specify the content type that will be returned by the extension. If not provided, the content will be shown as plain text. + +### `Widget-Content-Frameless` +When set to `true`, the widget's content will be displayed without the default background or "frame". + +## Content Types + +> [!NOTE] +> +> Currently, `html` is the only supported content type. The long-term goal is to have generic content types such as `videos`, `forum-posts`, `markets`, `streams`, etc. which will be returned in JSON format and displayed by Glance using existing styles and functionality, allowing extension developers to achieve a native look while only focusing on providing data from their preferred source. + +### `html` +Displays the content as HTML. This requires the user to have the `allow-potentially-dangerous-html` property set to `true`, otherwise the content will be shown as plain text. + + +#### Using existing classes and functionality +Most of the features seen throughout Glance can easily be used in your custom HTML extensions. Below is an example of some of these features: + +```html +Text with subdued color
+Text with base color
+Text with highlighted color
+Text with primary color
+Text with positive color
+Text with negative color
+ +Font size 1
+Font size 2
+Font size 3
+Font size 4
+Font size base
+Font size 5
+Font size 6
+ +Event happened ago
+ +Lazily loaded image:
+ +List of posts:
+ +v0.6.0 or higher)v0.6.0 or higher)v0.6.0 or higher)All sites are online
+ +All sites are online
+ +{{ .Description }}
+ {{ end }} + {{ if gt (len .Categories) 0 }} + + {{ end }} +
+ {{ .StreamTitle }}
+" + html.EscapeString(string(content)) + "") + } +} + +func fetchExtension(options extensionRequestOptions) (extension, error) { + request, _ := http.NewRequest("GET", options.URL, nil) + if len(options.Parameters) > 0 { + request.URL.RawQuery = options.Parameters.toQueryString() + } + + for key, value := range options.Headers { + request.Header.Add(key, value) + } + + response, err := http.DefaultClient.Do(request) + if err != nil { + slog.Error("Failed fetching extension", "url", options.URL, "error", err) + return extension{}, fmt.Errorf("%w: request failed: %w", errNoContent, err) + } + + defer response.Body.Close() + + body, err := io.ReadAll(response.Body) + if err != nil { + slog.Error("Failed reading response body of extension", "url", options.URL, "error", err) + return extension{}, fmt.Errorf("%w: could not read body: %w", errNoContent, err) + } + + extension := extension{} + + if response.Header.Get(extensionHeaderTitle) == "" { + extension.Title = "Extension" + } else { + extension.Title = response.Header.Get(extensionHeaderTitle) + } + + if response.Header.Get(extensionHeaderTitleURL) != "" { + extension.TitleURL = response.Header.Get(extensionHeaderTitleURL) + } + + contentType, ok := extensionStringToType[response.Header.Get(extensionHeaderContentType)] + + if !ok { + contentType, ok = extensionStringToType[options.FallbackContentType] + + if !ok { + contentType = extensionContentUnknown + } + } + + if stringToBool(response.Header.Get(extensionHeaderContentFrameless)) { + extension.Frameless = true + } + + extension.Content = convertExtensionContent(options, body, contentType) + + return extension, nil +} diff --git a/internal/glance/widget-group.go b/internal/glance/widget-group.go new file mode 100644 index 0000000..2ea3813 --- /dev/null +++ b/internal/glance/widget-group.go @@ -0,0 +1,52 @@ +package glance + +import ( + "context" + "errors" + "html/template" + "time" +) + +var groupWidgetTemplate = mustParseTemplate("group.html", "widget-base.html") + +type groupWidget struct { + widgetBase `yaml:",inline"` + containerWidgetBase `yaml:",inline"` +} + +func (widget *groupWidget) initialize() error { + widget.withError(nil) + widget.HideHeader = true + + for i := range widget.Widgets { + widget.Widgets[i].setHideHeader(true) + + if widget.Widgets[i].GetType() == "group" { + return errors.New("nested groups are not supported") + } else if widget.Widgets[i].GetType() == "split-column" { + return errors.New("split columns inside of groups are not supported") + } + } + + if err := widget.containerWidgetBase._initializeWidgets(); err != nil { + return err + } + + return nil +} + +func (widget *groupWidget) update(ctx context.Context) { + widget.containerWidgetBase._update(ctx) +} + +func (widget *groupWidget) setProviders(providers *widgetProviders) { + widget.containerWidgetBase._setProviders(providers) +} + +func (widget *groupWidget) requiresUpdate(now *time.Time) bool { + return widget.containerWidgetBase._requiresUpdate(now) +} + +func (widget *groupWidget) Render() template.HTML { + return widget.renderTemplate(widget, groupWidgetTemplate) +} diff --git a/internal/glance/widget-hacker-news.go b/internal/glance/widget-hacker-news.go new file mode 100644 index 0000000..ad00df0 --- /dev/null +++ b/internal/glance/widget-hacker-news.go @@ -0,0 +1,152 @@ +package glance + +import ( + "context" + "fmt" + "html/template" + "log/slog" + "net/http" + "strconv" + "strings" + "time" +) + +type hackerNewsWidget struct { + widgetBase `yaml:",inline"` + Posts forumPostList `yaml:"-"` + Limit int `yaml:"limit"` + SortBy string `yaml:"sort-by"` + ExtraSortBy string `yaml:"extra-sort-by"` + CollapseAfter int `yaml:"collapse-after"` + CommentsUrlTemplate string `yaml:"comments-url-template"` + ShowThumbnails bool `yaml:"-"` +} + +func (widget *hackerNewsWidget) initialize() error { + widget. + withTitle("Hacker News"). + withTitleURL("https://news.ycombinator.com/"). + withCacheDuration(30 * time.Minute) + + if widget.Limit <= 0 { + widget.Limit = 15 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + if widget.SortBy != "top" && widget.SortBy != "new" && widget.SortBy != "best" { + widget.SortBy = "top" + } + + return nil +} + +func (widget *hackerNewsWidget) update(ctx context.Context) { + posts, err := fetchHackerNewsPosts(widget.SortBy, 40, widget.CommentsUrlTemplate) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if widget.ExtraSortBy == "engagement" { + posts.calculateEngagement() + posts.sortByEngagement() + } + + if widget.Limit < len(posts) { + posts = posts[:widget.Limit] + } + + widget.Posts = posts +} + +func (widget *hackerNewsWidget) Render() template.HTML { + return widget.renderTemplate(widget, forumPostsTemplate) +} + +type hackerNewsPostResponseJson struct { + Id int `json:"id"` + Score int `json:"score"` + Title string `json:"title"` + TargetUrl string `json:"url,omitempty"` + CommentCount int `json:"descendants"` + TimePosted int64 `json:"time"` +} + +func fetchHackerNewsPostIds(sort string) ([]int, error) { + request, _ := http.NewRequest("GET", fmt.Sprintf("https://hacker-news.firebaseio.com/v0/%sstories.json", sort), nil) + response, err := decodeJsonFromRequest[[]int](defaultHTTPClient, request) + if err != nil { + return nil, fmt.Errorf("%w: could not fetch list of post IDs", errNoContent) + } + + return response, nil +} + +func fetchHackerNewsPostsFromIds(postIds []int, commentsUrlTemplate string) (forumPostList, error) { + requests := make([]*http.Request, len(postIds)) + + for i, id := range postIds { + request, _ := http.NewRequest("GET", fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", id), nil) + requests[i] = request + } + + task := decodeJsonFromRequestTask[hackerNewsPostResponseJson](defaultHTTPClient) + job := newJob(task, requests).withWorkers(30) + results, errs, err := workerPoolDo(job) + if err != nil { + return nil, err + } + + posts := make(forumPostList, 0, len(postIds)) + + for i := range results { + if errs[i] != nil { + slog.Error("Failed to fetch or parse hacker news post", "error", errs[i], "url", requests[i].URL) + continue + } + + var commentsUrl string + + if commentsUrlTemplate == "" { + commentsUrl = "https://news.ycombinator.com/item?id=" + strconv.Itoa(results[i].Id) + } else { + commentsUrl = strings.ReplaceAll(commentsUrlTemplate, "{POST-ID}", strconv.Itoa(results[i].Id)) + } + + posts = append(posts, forumPost{ + Title: results[i].Title, + DiscussionUrl: commentsUrl, + TargetUrl: results[i].TargetUrl, + TargetUrlDomain: extractDomainFromUrl(results[i].TargetUrl), + CommentCount: results[i].CommentCount, + Score: results[i].Score, + TimePosted: time.Unix(results[i].TimePosted, 0), + }) + } + + if len(posts) == 0 { + return nil, errNoContent + } + + if len(posts) != len(postIds) { + return posts, fmt.Errorf("%w could not fetch some hacker news posts", errPartialContent) + } + + return posts, nil +} + +func fetchHackerNewsPosts(sort string, limit int, commentsUrlTemplate string) (forumPostList, error) { + postIds, err := fetchHackerNewsPostIds(sort) + if err != nil { + return nil, err + } + + if len(postIds) > limit { + postIds = postIds[:limit] + } + + return fetchHackerNewsPostsFromIds(postIds, commentsUrlTemplate) +} diff --git a/internal/glance/widget-html.go b/internal/glance/widget-html.go new file mode 100644 index 0000000..0e32a46 --- /dev/null +++ b/internal/glance/widget-html.go @@ -0,0 +1,20 @@ +package glance + +import ( + "html/template" +) + +type htmlWidget struct { + widgetBase `yaml:",inline"` + Source template.HTML `yaml:"source"` +} + +func (widget *htmlWidget) initialize() error { + widget.withTitle("").withError(nil) + + return nil +} + +func (widget *htmlWidget) Render() template.HTML { + return widget.Source +} diff --git a/internal/widget/iframe.go b/internal/glance/widget-iframe.go similarity index 50% rename from internal/widget/iframe.go rename to internal/glance/widget-iframe.go index 44d0822..830b383 100644 --- a/internal/widget/iframe.go +++ b/internal/glance/widget-iframe.go @@ -1,32 +1,30 @@ -package widget +package glance import ( "errors" "fmt" "html/template" "net/url" - - "github.com/glanceapp/glance/internal/assets" ) -type IFrame struct { +var iframeWidgetTemplate = mustParseTemplate("iframe.html", "widget-base.html") + +type iframeWidget struct { widgetBase `yaml:",inline"` cachedHTML template.HTML `yaml:"-"` Source string `yaml:"source"` Height int `yaml:"height"` } -func (widget *IFrame) Initialize() error { +func (widget *iframeWidget) initialize() error { widget.withTitle("IFrame").withError(nil) if widget.Source == "" { - return errors.New("missing source for iframe") + return errors.New("source is required") } - _, err := url.Parse(widget.Source) - - if err != nil { - return fmt.Errorf("invalid source for iframe: %v", err) + if _, err := url.Parse(widget.Source); err != nil { + return fmt.Errorf("parsing URL: %v", err) } if widget.Height == 50 { @@ -35,11 +33,11 @@ func (widget *IFrame) Initialize() error { widget.Height = 50 } - widget.cachedHTML = widget.render(widget, assets.IFrameTemplate) + widget.cachedHTML = widget.renderTemplate(widget, iframeWidgetTemplate) return nil } -func (widget *IFrame) Render() template.HTML { +func (widget *iframeWidget) Render() template.HTML { return widget.cachedHTML } diff --git a/internal/glance/widget-lobsters.go b/internal/glance/widget-lobsters.go new file mode 100644 index 0000000..786d1df --- /dev/null +++ b/internal/glance/widget-lobsters.go @@ -0,0 +1,144 @@ +package glance + +import ( + "context" + "html/template" + "net/http" + "strings" + "time" +) + +type lobstersWidget struct { + widgetBase `yaml:",inline"` + Posts forumPostList `yaml:"-"` + InstanceURL string `yaml:"instance-url"` + CustomURL string `yaml:"custom-url"` + Limit int `yaml:"limit"` + CollapseAfter int `yaml:"collapse-after"` + SortBy string `yaml:"sort-by"` + Tags []string `yaml:"tags"` + ShowThumbnails bool `yaml:"-"` +} + +func (widget *lobstersWidget) initialize() error { + widget.withTitle("Lobsters").withCacheDuration(time.Hour) + + if widget.InstanceURL == "" { + widget.withTitleURL("https://lobste.rs") + } else { + widget.withTitleURL(widget.InstanceURL) + } + + if widget.SortBy == "" || (widget.SortBy != "hot" && widget.SortBy != "new") { + widget.SortBy = "hot" + } + + if widget.Limit <= 0 { + widget.Limit = 15 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + return nil +} + +func (widget *lobstersWidget) update(ctx context.Context) { + posts, err := fetchLobstersPosts(widget.CustomURL, widget.InstanceURL, widget.SortBy, widget.Tags) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if widget.Limit < len(posts) { + posts = posts[:widget.Limit] + } + + widget.Posts = posts +} + +func (widget *lobstersWidget) Render() template.HTML { + return widget.renderTemplate(widget, forumPostsTemplate) +} + +type lobstersPostResponseJson struct { + CreatedAt string `json:"created_at"` + Title string `json:"title"` + URL string `json:"url"` + Score int `json:"score"` + CommentCount int `json:"comment_count"` + CommentsURL string `json:"comments_url"` + Tags []string `json:"tags"` +} + +type lobstersFeedResponseJson []lobstersPostResponseJson + +func fetchLobstersPostsFromFeed(feedUrl string) (forumPostList, error) { + request, err := http.NewRequest("GET", feedUrl, nil) + if err != nil { + return nil, err + } + + feed, err := decodeJsonFromRequest[lobstersFeedResponseJson](defaultHTTPClient, request) + if err != nil { + return nil, err + } + + posts := make(forumPostList, 0, len(feed)) + + for i := range feed { + createdAt, _ := time.Parse(time.RFC3339, feed[i].CreatedAt) + + posts = append(posts, forumPost{ + Title: feed[i].Title, + DiscussionUrl: feed[i].CommentsURL, + TargetUrl: feed[i].URL, + TargetUrlDomain: extractDomainFromUrl(feed[i].URL), + CommentCount: feed[i].CommentCount, + Score: feed[i].Score, + TimePosted: createdAt, + Tags: feed[i].Tags, + }) + } + + if len(posts) == 0 { + return nil, errNoContent + } + + return posts, nil +} + +func fetchLobstersPosts(customURL string, instanceURL string, sortBy string, tags []string) (forumPostList, error) { + var feedUrl string + + if customURL != "" { + feedUrl = customURL + } else { + if instanceURL != "" { + instanceURL = strings.TrimRight(instanceURL, "/") + "/" + } else { + instanceURL = "https://lobste.rs/" + } + + if sortBy == "hot" { + sortBy = "hottest" + } else if sortBy == "new" { + sortBy = "newest" + } + + if len(tags) == 0 { + feedUrl = instanceURL + sortBy + ".json" + } else { + tags := strings.Join(tags, ",") + feedUrl = instanceURL + "t/" + tags + ".json" + } + } + + posts, err := fetchLobstersPostsFromFeed(feedUrl) + if err != nil { + return nil, err + } + + return posts, nil +} diff --git a/internal/glance/widget-markets.go b/internal/glance/widget-markets.go new file mode 100644 index 0000000..b53b10a --- /dev/null +++ b/internal/glance/widget-markets.go @@ -0,0 +1,228 @@ +package glance + +import ( + "context" + "fmt" + "html/template" + "log/slog" + "math" + "net/http" + "sort" + "strings" + "time" +) + +var marketsWidgetTemplate = mustParseTemplate("markets.html", "widget-base.html") + +type marketsWidget struct { + widgetBase `yaml:",inline"` + StocksRequests []marketRequest `yaml:"stocks"` + MarketRequests []marketRequest `yaml:"markets"` + ChartLinkTemplate string `yaml:"chart-link-template"` + SymbolLinkTemplate string `yaml:"symbol-link-template"` + Sort string `yaml:"sort-by"` + Markets marketList `yaml:"-"` +} + +func (widget *marketsWidget) initialize() error { + widget.withTitle("Markets").withCacheDuration(time.Hour) + + // legacy support, remove in v0.10.0 + if len(widget.MarketRequests) == 0 { + widget.MarketRequests = widget.StocksRequests + } + + for i := range widget.MarketRequests { + m := &widget.MarketRequests[i] + + if widget.ChartLinkTemplate != "" && m.ChartLink == "" { + m.ChartLink = strings.ReplaceAll(widget.ChartLinkTemplate, "{SYMBOL}", m.Symbol) + } + + if widget.SymbolLinkTemplate != "" && m.SymbolLink == "" { + m.SymbolLink = strings.ReplaceAll(widget.SymbolLinkTemplate, "{SYMBOL}", m.Symbol) + } + } + + return nil +} + +func (widget *marketsWidget) update(ctx context.Context) { + markets, err := fetchMarketsDataFromYahoo(widget.MarketRequests) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if widget.Sort == "absolute-change" { + markets.sortByAbsChange() + } else if widget.Sort == "change" { + markets.sortByChange() + } + + widget.Markets = markets +} + +func (widget *marketsWidget) Render() template.HTML { + return widget.renderTemplate(widget, marketsWidgetTemplate) +} + +type marketRequest struct { + CustomName string `yaml:"name"` + Symbol string `yaml:"symbol"` + ChartLink string `yaml:"chart-link"` + SymbolLink string `yaml:"symbol-link"` +} + +type market struct { + marketRequest + Name string + Currency string + Price float64 + PriceHint int + PercentChange float64 + SvgChartPoints string +} + +type marketList []market + +func (t marketList) sortByAbsChange() { + sort.Slice(t, func(i, j int) bool { + return math.Abs(t[i].PercentChange) > math.Abs(t[j].PercentChange) + }) +} + +func (t marketList) sortByChange() { + sort.Slice(t, func(i, j int) bool { + return t[i].PercentChange > t[j].PercentChange + }) +} + +type marketResponseJson struct { + Chart struct { + Result []struct { + Meta struct { + Currency string `json:"currency"` + Symbol string `json:"symbol"` + RegularMarketPrice float64 `json:"regularMarketPrice"` + ChartPreviousClose float64 `json:"chartPreviousClose"` + ShortName string `json:"shortName"` + PriceHint int `json:"priceHint"` + } `json:"meta"` + Indicators struct { + Quote []struct { + Close []float64 `json:"close,omitempty"` + } `json:"quote"` + } `json:"indicators"` + } `json:"result"` + } `json:"chart"` +} + +// TODO: allow changing chart time frame +const marketChartDays = 21 + +func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, error) { + requests := make([]*http.Request, 0, len(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) + setBrowserUserAgentHeader(request) + requests = append(requests, request) + } + + job := newJob(decodeJsonFromRequestTask[marketResponseJson](defaultHTTPClient), requests) + responses, errs, err := workerPoolDo(job) + if err != nil { + return nil, fmt.Errorf("%w: %v", errNoContent, err) + } + + markets := make(marketList, 0, len(responses)) + var failed int + + for i := range responses { + if errs[i] != nil { + failed++ + slog.Error("Failed to fetch market data", "symbol", marketRequests[i].Symbol, "error", errs[i]) + continue + } + + response := responses[i] + + if len(response.Chart.Result) == 0 { + failed++ + slog.Error("Market response contains no data", "symbol", marketRequests[i].Symbol) + continue + } + + result := &response.Chart.Result[0] + prices := result.Indicators.Quote[0].Close + + if len(prices) > marketChartDays { + prices = prices[len(prices)-marketChartDays:] + } + + previous := result.Meta.RegularMarketPrice + + if len(prices) >= 2 && prices[len(prices)-2] != 0 { + previous = prices[len(prices)-2] + } + + points := svgPolylineCoordsFromYValues(100, 50, maybeCopySliceWithoutZeroValues(prices)) + + currency, exists := currencyToSymbol[strings.ToUpper(result.Meta.Currency)] + if !exists { + currency = result.Meta.Currency + } + + markets = append(markets, market{ + marketRequest: marketRequests[i], + Price: result.Meta.RegularMarketPrice, + Currency: currency, + PriceHint: result.Meta.PriceHint, + Name: ternary(marketRequests[i].CustomName == "", + result.Meta.ShortName, + marketRequests[i].CustomName, + ), + PercentChange: percentChange( + result.Meta.RegularMarketPrice, + previous, + ), + SvgChartPoints: points, + }) + } + + if len(markets) == 0 { + return nil, errNoContent + } + + if failed > 0 { + return markets, fmt.Errorf("%w: could not fetch data for %d market(s)", errPartialContent, failed) + } + + return markets, nil +} + +var currencyToSymbol = map[string]string{ + "USD": "$", + "EUR": "€", + "JPY": "¥", + "CAD": "C$", + "AUD": "A$", + "GBP": "£", + "CHF": "Fr", + "NZD": "N$", + "INR": "₹", + "BRL": "R$", + "RUB": "₽", + "TRY": "₺", + "ZAR": "R", + "CNY": "¥", + "KRW": "₩", + "HKD": "HK$", + "SGD": "S$", + "SEK": "kr", + "NOK": "kr", + "DKK": "kr", + "PLN": "zł", + "PHP": "₱", +} diff --git a/internal/glance/widget-monitor.go b/internal/glance/widget-monitor.go new file mode 100644 index 0000000..76f0d45 --- /dev/null +++ b/internal/glance/widget-monitor.go @@ -0,0 +1,182 @@ +package glance + +import ( + "context" + "errors" + "html/template" + "net/http" + "slices" + "strconv" + "time" +) + +var ( + monitorWidgetTemplate = mustParseTemplate("monitor.html", "widget-base.html") + monitorWidgetCompactTemplate = mustParseTemplate("monitor-compact.html", "widget-base.html") +) + +type monitorWidget struct { + widgetBase `yaml:",inline"` + Sites []struct { + *SiteStatusRequest `yaml:",inline"` + Status *siteStatus `yaml:"-"` + URL string `yaml:"-"` + ErrorURL string `yaml:"error-url"` + Title string `yaml:"title"` + Icon customIconField `yaml:"icon"` + SameTab bool `yaml:"same-tab"` + StatusText string `yaml:"-"` + StatusStyle string `yaml:"-"` + AltStatusCodes []int `yaml:"alt-status-codes"` + } `yaml:"sites"` + Style string `yaml:"style"` + ShowFailingOnly bool `yaml:"show-failing-only"` + HasFailing bool `yaml:"-"` +} + +func (widget *monitorWidget) initialize() error { + widget.withTitle("Monitor").withCacheDuration(5 * time.Minute) + + return nil +} + +func (widget *monitorWidget) update(ctx context.Context) { + requests := make([]*SiteStatusRequest, len(widget.Sites)) + + for i := range widget.Sites { + requests[i] = widget.Sites[i].SiteStatusRequest + } + + statuses, err := fetchStatusForSites(requests) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + widget.HasFailing = false + + for i := range widget.Sites { + site := &widget.Sites[i] + status := &statuses[i] + site.Status = status + + if !slices.Contains(site.AltStatusCodes, status.Code) && (status.Code >= 400 || status.Error != nil) { + widget.HasFailing = true + } + + if status.Error != nil && site.ErrorURL != "" { + site.URL = site.ErrorURL + } else { + site.URL = site.DefaultURL + } + + site.StatusText = statusCodeToText(status.Code, site.AltStatusCodes) + site.StatusStyle = statusCodeToStyle(status.Code, site.AltStatusCodes) + } +} + +func (widget *monitorWidget) Render() template.HTML { + if widget.Style == "compact" { + return widget.renderTemplate(widget, monitorWidgetCompactTemplate) + } + + return widget.renderTemplate(widget, monitorWidgetTemplate) +} + +func statusCodeToText(status int, altStatusCodes []int) string { + if status == 200 || slices.Contains(altStatusCodes, status) { + return "OK" + } + if status == 404 { + return "Not Found" + } + if status == 403 { + return "Forbidden" + } + if status == 401 { + return "Unauthorized" + } + if status >= 500 { + return "Server Error" + } + if status >= 400 { + return "Client Error" + } + + return strconv.Itoa(status) +} + +func statusCodeToStyle(status int, altStatusCodes []int) string { + if status == 200 || slices.Contains(altStatusCodes, status) { + return "ok" + } + + return "error" +} + +type SiteStatusRequest struct { + DefaultURL string `yaml:"url"` + CheckURL string `yaml:"check-url"` + AllowInsecure bool `yaml:"allow-insecure"` +} + +type siteStatus struct { + Code int + TimedOut bool + ResponseTime time.Duration + Error error +} + +func fetchSiteStatusTask(statusRequest *SiteStatusRequest) (siteStatus, error) { + var url string + if statusRequest.CheckURL != "" { + url = statusRequest.CheckURL + } else { + url = statusRequest.DefaultURL + } + request, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return siteStatus{ + Error: err, + }, nil + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) + defer cancel() + request = request.WithContext(ctx) + requestSentAt := time.Now() + var response *http.Response + + if !statusRequest.AllowInsecure { + response, err = defaultHTTPClient.Do(request) + } else { + response, err = defaultInsecureHTTPClient.Do(request) + } + + status := siteStatus{ResponseTime: time.Since(requestSentAt)} + + if err != nil { + if errors.Is(err, context.DeadlineExceeded) { + status.TimedOut = true + } + + status.Error = err + return status, nil + } + + defer response.Body.Close() + + status.Code = response.StatusCode + + return status, nil +} + +func fetchStatusForSites(requests []*SiteStatusRequest) ([]siteStatus, error) { + job := newJob(fetchSiteStatusTask, requests).withWorkers(20) + results, _, err := workerPoolDo(job) + if err != nil { + return nil, err + } + + return results, nil +} diff --git a/internal/glance/widget-old-calendar.go b/internal/glance/widget-old-calendar.go new file mode 100644 index 0000000..e4fbe74 --- /dev/null +++ b/internal/glance/widget-old-calendar.go @@ -0,0 +1,86 @@ +package glance + +import ( + "context" + "html/template" + "time" +) + +var oldCalendarWidgetTemplate = mustParseTemplate("old-calendar.html", "widget-base.html") + +type oldCalendarWidget struct { + widgetBase `yaml:",inline"` + Calendar *calendar + StartSunday bool `yaml:"start-sunday"` +} + +func (widget *oldCalendarWidget) initialize() error { + widget.withTitle("Calendar").withCacheOnTheHour() + + return nil +} + +func (widget *oldCalendarWidget) update(ctx context.Context) { + widget.Calendar = newCalendar(time.Now(), widget.StartSunday) + widget.withError(nil).scheduleNextUpdate() +} + +func (widget *oldCalendarWidget) Render() template.HTML { + return widget.renderTemplate(widget, oldCalendarWidgetTemplate) +} + +type calendar struct { + CurrentDay int + CurrentWeekNumber int + CurrentMonthName string + CurrentYear int + Days []int +} + +// TODO: very inflexible, refactor to allow more customizability +// TODO: allow changing between showing the previous and next week and the entire month +func newCalendar(now time.Time, startSunday bool) *calendar { + year, week := now.ISOWeek() + weekday := now.Weekday() + if !startSunday { + weekday = (weekday + 6) % 7 // Shift Monday to 0 + } + + currentMonthDays := daysInMonth(now.Month(), year) + + var previousMonthDays int + + if previousMonthNumber := now.Month() - 1; previousMonthNumber < 1 { + previousMonthDays = daysInMonth(12, year-1) + } else { + previousMonthDays = daysInMonth(previousMonthNumber, year) + } + + startDaysFrom := now.Day() - int(weekday) - 7 + + days := make([]int, 21) + + for i := 0; i < 21; i++ { + day := startDaysFrom + i + + if day < 1 { + day = previousMonthDays + day + } else if day > currentMonthDays { + day = day - currentMonthDays + } + + days[i] = day + } + + return &calendar{ + CurrentDay: now.Day(), + CurrentWeekNumber: week, + CurrentMonthName: now.Month().String(), + CurrentYear: year, + Days: days, + } +} + +func daysInMonth(m time.Month, year int) int { + return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day() +} diff --git a/internal/glance/widget-reddit.go b/internal/glance/widget-reddit.go new file mode 100644 index 0000000..e7109fa --- /dev/null +++ b/internal/glance/widget-reddit.go @@ -0,0 +1,276 @@ +package glance + +import ( + "context" + "errors" + "fmt" + "html" + "html/template" + "net/http" + "net/url" + "strings" + "time" +) + +var ( + redditWidgetHorizontalCardsTemplate = mustParseTemplate("reddit-horizontal-cards.html", "widget-base.html") + redditWidgetVerticalCardsTemplate = mustParseTemplate("reddit-vertical-cards.html", "widget-base.html") +) + +type redditWidget struct { + widgetBase `yaml:",inline"` + Posts forumPostList `yaml:"-"` + Subreddit string `yaml:"subreddit"` + Proxy proxyOptionsField `yaml:"proxy"` + Style string `yaml:"style"` + ShowThumbnails bool `yaml:"show-thumbnails"` + ShowFlairs bool `yaml:"show-flairs"` + SortBy string `yaml:"sort-by"` + TopPeriod string `yaml:"top-period"` + Search string `yaml:"search"` + ExtraSortBy string `yaml:"extra-sort-by"` + CommentsUrlTemplate string `yaml:"comments-url-template"` + Limit int `yaml:"limit"` + CollapseAfter int `yaml:"collapse-after"` + RequestUrlTemplate string `yaml:"request-url-template"` +} + +func (widget *redditWidget) initialize() error { + if widget.Subreddit == "" { + return errors.New("subreddit is required") + } + + if widget.Limit <= 0 { + widget.Limit = 15 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + if !isValidRedditSortType(widget.SortBy) { + widget.SortBy = "hot" + } + + if !isValidRedditTopPeriod(widget.TopPeriod) { + widget.TopPeriod = "day" + } + + if widget.RequestUrlTemplate != "" { + if !strings.Contains(widget.RequestUrlTemplate, "{REQUEST-URL}") { + return errors.New("no `{REQUEST-URL}` placeholder specified") + } + } + + widget. + withTitle("r/" + widget.Subreddit). + withTitleURL("https://www.reddit.com/r/" + widget.Subreddit + "/"). + withCacheDuration(30 * time.Minute) + + return nil +} + +func isValidRedditSortType(sortBy string) bool { + return sortBy == "hot" || + sortBy == "new" || + sortBy == "top" || + sortBy == "rising" +} + +func isValidRedditTopPeriod(period string) bool { + return period == "hour" || + period == "day" || + period == "week" || + period == "month" || + period == "year" || + period == "all" +} + +func (widget *redditWidget) update(ctx context.Context) { + // TODO: refactor, use a struct to pass all of these + posts, err := fetchSubredditPosts( + widget.Subreddit, + widget.SortBy, + widget.TopPeriod, + widget.Search, + widget.CommentsUrlTemplate, + widget.RequestUrlTemplate, + widget.Proxy.client, + widget.ShowFlairs, + ) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if len(posts) > widget.Limit { + posts = posts[:widget.Limit] + } + + if widget.ExtraSortBy == "engagement" { + posts.calculateEngagement() + posts.sortByEngagement() + } + + widget.Posts = posts +} + +func (widget *redditWidget) Render() template.HTML { + if widget.Style == "horizontal-cards" { + return widget.renderTemplate(widget, redditWidgetHorizontalCardsTemplate) + } + + if widget.Style == "vertical-cards" { + return widget.renderTemplate(widget, redditWidgetVerticalCardsTemplate) + } + + return widget.renderTemplate(widget, forumPostsTemplate) + +} + +type subredditResponseJson struct { + Data struct { + Children []struct { + Data struct { + Id string `json:"id"` + Title string `json:"title"` + Upvotes int `json:"ups"` + Url string `json:"url"` + Time float64 `json:"created"` + CommentsCount int `json:"num_comments"` + Domain string `json:"domain"` + Permalink string `json:"permalink"` + Stickied bool `json:"stickied"` + Pinned bool `json:"pinned"` + IsSelf bool `json:"is_self"` + Thumbnail string `json:"thumbnail"` + Flair string `json:"link_flair_text"` + ParentList []struct { + Id string `json:"id"` + Subreddit string `json:"subreddit"` + Permalink string `json:"permalink"` + } `json:"crosspost_parent_list"` + } `json:"data"` + } `json:"children"` + } `json:"data"` +} + +func templateRedditCommentsURL(template, subreddit, postId, postPath string) string { + template = strings.ReplaceAll(template, "{SUBREDDIT}", subreddit) + template = strings.ReplaceAll(template, "{POST-ID}", postId) + template = strings.ReplaceAll(template, "{POST-PATH}", strings.TrimLeft(postPath, "/")) + + return template +} + +func fetchSubredditPosts( + subreddit, + sort, + topPeriod, + search, + commentsUrlTemplate, + requestUrlTemplate string, + proxyClient *http.Client, + showFlairs bool, +) (forumPostList, error) { + query := url.Values{} + var requestUrl string + + if search != "" { + query.Set("q", search+" subreddit:"+subreddit) + query.Set("sort", sort) + } + + if sort == "top" { + query.Set("t", topPeriod) + } + + if search != "" { + requestUrl = fmt.Sprintf("https://www.reddit.com/search.json?%s", query.Encode()) + } else { + requestUrl = fmt.Sprintf("https://www.reddit.com/r/%s/%s.json?%s", subreddit, sort, query.Encode()) + } + + var client requestDoer = defaultHTTPClient + + if requestUrlTemplate != "" { + requestUrl = strings.ReplaceAll(requestUrlTemplate, "{REQUEST-URL}", requestUrl) + } else if proxyClient != nil { + client = proxyClient + } + + request, err := http.NewRequest("GET", requestUrl, nil) + if err != nil { + return nil, err + } + + // Required to increase rate limit, otherwise Reddit randomly returns 429 even after just 2 requests + setBrowserUserAgentHeader(request) + responseJson, err := decodeJsonFromRequest[subredditResponseJson](client, request) + if err != nil { + return nil, err + } + + if len(responseJson.Data.Children) == 0 { + return nil, fmt.Errorf("no posts found") + } + + posts := make(forumPostList, 0, len(responseJson.Data.Children)) + + for i := range responseJson.Data.Children { + post := &responseJson.Data.Children[i].Data + + if post.Stickied || post.Pinned { + continue + } + + var commentsUrl string + + if commentsUrlTemplate == "" { + commentsUrl = "https://www.reddit.com" + post.Permalink + } else { + commentsUrl = templateRedditCommentsURL(commentsUrlTemplate, subreddit, post.Id, post.Permalink) + } + + forumPost := forumPost{ + Title: html.UnescapeString(post.Title), + DiscussionUrl: commentsUrl, + TargetUrlDomain: post.Domain, + CommentCount: post.CommentsCount, + Score: post.Upvotes, + TimePosted: time.Unix(int64(post.Time), 0), + } + + if post.Thumbnail != "" && post.Thumbnail != "self" && post.Thumbnail != "default" && post.Thumbnail != "nsfw" { + forumPost.ThumbnailUrl = html.UnescapeString(post.Thumbnail) + } + + if !post.IsSelf { + forumPost.TargetUrl = post.Url + } + + if showFlairs && post.Flair != "" { + forumPost.Tags = append(forumPost.Tags, post.Flair) + } + + if len(post.ParentList) > 0 { + forumPost.IsCrosspost = true + forumPost.TargetUrlDomain = "r/" + post.ParentList[0].Subreddit + + if commentsUrlTemplate == "" { + forumPost.TargetUrl = "https://www.reddit.com" + post.ParentList[0].Permalink + } else { + forumPost.TargetUrl = templateRedditCommentsURL( + commentsUrlTemplate, + post.ParentList[0].Subreddit, + post.ParentList[0].Id, + post.ParentList[0].Permalink, + ) + } + } + + posts = append(posts, forumPost) + } + + return posts, nil +} diff --git a/internal/glance/widget-releases.go b/internal/glance/widget-releases.go new file mode 100644 index 0000000..de56bc5 --- /dev/null +++ b/internal/glance/widget-releases.go @@ -0,0 +1,421 @@ +package glance + +import ( + "context" + "errors" + "fmt" + "html/template" + "log/slog" + "net/http" + "net/url" + "sort" + "strings" + "time" + + "gopkg.in/yaml.v3" +) + +var releasesWidgetTemplate = mustParseTemplate("releases.html", "widget-base.html") + +type releasesWidget struct { + widgetBase `yaml:",inline"` + Releases appReleaseList `yaml:"-"` + Repositories []*releaseRequest `yaml:"repositories"` + Token string `yaml:"token"` + GitLabToken string `yaml:"gitlab-token"` + Limit int `yaml:"limit"` + CollapseAfter int `yaml:"collapse-after"` + ShowSourceIcon bool `yaml:"show-source-icon"` +} + +func (widget *releasesWidget) initialize() error { + widget.withTitle("Releases").withCacheDuration(2 * time.Hour) + + if widget.Limit <= 0 { + widget.Limit = 10 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + for i := range widget.Repositories { + r := widget.Repositories[i] + + if r.source == releaseSourceGithub && widget.Token != "" { + r.token = &widget.Token + } else if r.source == releaseSourceGitlab && widget.GitLabToken != "" { + r.token = &widget.GitLabToken + } + } + + return nil +} + +func (widget *releasesWidget) update(ctx context.Context) { + releases, err := fetchLatestReleases(widget.Repositories) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if len(releases) > widget.Limit { + releases = releases[:widget.Limit] + } + + for i := range releases { + releases[i].SourceIconURL = widget.Providers.assetResolver("icons/" + string(releases[i].Source) + ".svg") + } + + widget.Releases = releases +} + +func (widget *releasesWidget) Render() template.HTML { + return widget.renderTemplate(widget, releasesWidgetTemplate) +} + +type releaseSource string + +const ( + releaseSourceCodeberg releaseSource = "codeberg" + releaseSourceGithub releaseSource = "github" + releaseSourceGitlab releaseSource = "gitlab" + releaseSourceDockerHub releaseSource = "dockerhub" +) + +type appRelease struct { + Source releaseSource + SourceIconURL string + Name string + Version string + NotesUrl string + TimeReleased time.Time + Downvotes int +} + +type appReleaseList []appRelease + +func (r appReleaseList) sortByNewest() appReleaseList { + sort.Slice(r, func(i, j int) bool { + return r[i].TimeReleased.After(r[j].TimeReleased) + }) + + return r +} + +type releaseRequest struct { + IncludePreleases bool `yaml:"include-prereleases"` + Repository string `yaml:"repository"` + + source releaseSource + token *string +} + +func (r *releaseRequest) UnmarshalYAML(node *yaml.Node) error { + type releaseRequestAlias releaseRequest + alias := (*releaseRequestAlias)(r) + var repository string + + if err := node.Decode(&repository); err != nil { + if err := node.Decode(alias); err != nil { + return fmt.Errorf("could not umarshal repository into string or struct: %v", err) + } + } + + if r.Repository == "" { + if repository == "" { + return errors.New("repository is required") + } else { + r.Repository = repository + } + } + + parts := strings.SplitN(repository, ":", 2) + if len(parts) == 1 { + r.source = releaseSourceGithub + } else if len(parts) == 2 { + r.Repository = parts[1] + + switch parts[0] { + case string(releaseSourceGithub): + r.source = releaseSourceGithub + case string(releaseSourceGitlab): + r.source = releaseSourceGitlab + case string(releaseSourceDockerHub): + r.source = releaseSourceDockerHub + case string(releaseSourceCodeberg): + r.source = releaseSourceCodeberg + default: + return errors.New("invalid source") + } + } + + return nil +} + +func fetchLatestReleases(requests []*releaseRequest) (appReleaseList, error) { + job := newJob(fetchLatestReleaseTask, requests).withWorkers(20) + results, errs, err := workerPoolDo(job) + if err != nil { + return nil, err + } + + var failed int + + releases := make(appReleaseList, 0, len(requests)) + + for i := range results { + if errs[i] != nil { + failed++ + slog.Error("Failed to fetch release", "source", requests[i].source, "repository", requests[i].Repository, "error", errs[i]) + continue + } + + releases = append(releases, *results[i]) + } + + if failed == len(requests) { + return nil, errNoContent + } + + releases.sortByNewest() + + if failed > 0 { + return releases, fmt.Errorf("%w: could not get %d releases", errPartialContent, failed) + } + + return releases, nil +} + +func fetchLatestReleaseTask(request *releaseRequest) (*appRelease, error) { + switch request.source { + case releaseSourceCodeberg: + return fetchLatestCodebergRelease(request) + case releaseSourceGithub: + return fetchLatestGithubRelease(request) + case releaseSourceGitlab: + return fetchLatestGitLabRelease(request) + case releaseSourceDockerHub: + return fetchLatestDockerHubRelease(request) + } + + return nil, errors.New("unsupported source") +} + +type githubReleaseResponseJson struct { + TagName string `json:"tag_name"` + PublishedAt string `json:"published_at"` + HtmlUrl string `json:"html_url"` + Reactions struct { + Downvotes int `json:"-1"` + } `json:"reactions"` +} + +func fetchLatestGithubRelease(request *releaseRequest) (*appRelease, error) { + var requestURL string + if !request.IncludePreleases { + requestURL = fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", request.Repository) + } else { + requestURL = fmt.Sprintf("https://api.github.com/repos/%s/releases", request.Repository) + } + + httpRequest, err := http.NewRequest("GET", requestURL, nil) + if err != nil { + return nil, err + } + + if request.token != nil { + httpRequest.Header.Add("Authorization", "Bearer "+(*request.token)) + } + + var response githubReleaseResponseJson + + if !request.IncludePreleases { + response, err = decodeJsonFromRequest[githubReleaseResponseJson](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + } else { + responses, err := decodeJsonFromRequest[[]githubReleaseResponseJson](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + + if len(responses) == 0 { + return nil, fmt.Errorf("no releases found for repository %s", request.Repository) + } + + response = responses[0] + } + + return &appRelease{ + Source: releaseSourceGithub, + Name: request.Repository, + Version: normalizeVersionFormat(response.TagName), + NotesUrl: response.HtmlUrl, + TimeReleased: parseRFC3339Time(response.PublishedAt), + Downvotes: response.Reactions.Downvotes, + }, nil +} + +type dockerHubRepositoryTagsResponse struct { + Results []dockerHubRepositoryTagResponse `json:"results"` +} + +type dockerHubRepositoryTagResponse struct { + Name string `json:"name"` + LastPushed string `json:"tag_last_pushed"` +} + +const dockerHubOfficialRepoTagURLFormat = "https://hub.docker.com/_/%s/tags?name=%s" +const dockerHubRepoTagURLFormat = "https://hub.docker.com/r/%s/tags?name=%s" +const dockerHubTagsURLFormat = "https://hub.docker.com/v2/namespaces/%s/repositories/%s/tags" +const dockerHubSpecificTagURLFormat = "https://hub.docker.com/v2/namespaces/%s/repositories/%s/tags/%s" + +func fetchLatestDockerHubRelease(request *releaseRequest) (*appRelease, error) { + nameParts := strings.Split(request.Repository, "/") + + if len(nameParts) > 2 { + return nil, fmt.Errorf("invalid repository name: %s", request.Repository) + } else if len(nameParts) == 1 { + nameParts = []string{"library", nameParts[0]} + } + + tagParts := strings.SplitN(nameParts[1], ":", 2) + var requestURL string + + if len(tagParts) == 2 { + requestURL = fmt.Sprintf(dockerHubSpecificTagURLFormat, nameParts[0], tagParts[0], tagParts[1]) + } else { + requestURL = fmt.Sprintf(dockerHubTagsURLFormat, nameParts[0], nameParts[1]) + } + + httpRequest, err := http.NewRequest("GET", requestURL, nil) + if err != nil { + return nil, err + } + + if request.token != nil { + httpRequest.Header.Add("Authorization", "Bearer "+(*request.token)) + } + + var tag *dockerHubRepositoryTagResponse + + if len(tagParts) == 1 { + response, err := decodeJsonFromRequest[dockerHubRepositoryTagsResponse](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + + if len(response.Results) == 0 { + return nil, fmt.Errorf("no tags found for repository: %s", request.Repository) + } + + tag = &response.Results[0] + } else { + response, err := decodeJsonFromRequest[dockerHubRepositoryTagResponse](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + + tag = &response + } + + var repo string + var displayName string + var notesURL string + + if len(tagParts) == 1 { + repo = nameParts[1] + } else { + repo = tagParts[0] + } + + if nameParts[0] == "library" { + displayName = repo + notesURL = fmt.Sprintf(dockerHubOfficialRepoTagURLFormat, repo, tag.Name) + } else { + displayName = nameParts[0] + "/" + repo + notesURL = fmt.Sprintf(dockerHubRepoTagURLFormat, displayName, tag.Name) + } + + return &appRelease{ + Source: releaseSourceDockerHub, + NotesUrl: notesURL, + Name: displayName, + Version: tag.Name, + TimeReleased: parseRFC3339Time(tag.LastPushed), + }, nil +} + +type gitlabReleaseResponseJson struct { + TagName string `json:"tag_name"` + ReleasedAt string `json:"released_at"` + Links struct { + Self string `json:"self"` + } `json:"_links"` +} + +func fetchLatestGitLabRelease(request *releaseRequest) (*appRelease, error) { + httpRequest, err := http.NewRequest( + "GET", + fmt.Sprintf( + "https://gitlab.com/api/v4/projects/%s/releases/permalink/latest", + url.QueryEscape(request.Repository), + ), + nil, + ) + if err != nil { + return nil, err + } + + if request.token != nil { + httpRequest.Header.Add("PRIVATE-TOKEN", *request.token) + } + + response, err := decodeJsonFromRequest[gitlabReleaseResponseJson](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + + return &appRelease{ + Source: releaseSourceGitlab, + Name: request.Repository, + Version: normalizeVersionFormat(response.TagName), + NotesUrl: response.Links.Self, + TimeReleased: parseRFC3339Time(response.ReleasedAt), + }, nil +} + +type codebergReleaseResponseJson struct { + TagName string `json:"tag_name"` + PublishedAt string `json:"published_at"` + HtmlUrl string `json:"html_url"` +} + +func fetchLatestCodebergRelease(request *releaseRequest) (*appRelease, error) { + httpRequest, err := http.NewRequest( + "GET", + fmt.Sprintf( + "https://codeberg.org/api/v1/repos/%s/releases/latest", + request.Repository, + ), + nil, + ) + if err != nil { + return nil, err + } + + response, err := decodeJsonFromRequest[codebergReleaseResponseJson](defaultHTTPClient, httpRequest) + if err != nil { + return nil, err + } + + return &appRelease{ + Source: releaseSourceCodeberg, + Name: request.Repository, + Version: normalizeVersionFormat(response.TagName), + NotesUrl: response.HtmlUrl, + TimeReleased: parseRFC3339Time(response.PublishedAt), + }, nil +} diff --git a/internal/glance/widget-repository.go b/internal/glance/widget-repository.go new file mode 100644 index 0000000..1eeb8b4 --- /dev/null +++ b/internal/glance/widget-repository.go @@ -0,0 +1,238 @@ +package glance + +import ( + "context" + "fmt" + "html/template" + "net/http" + "strings" + "sync" + "time" +) + +var repositoryWidgetTemplate = mustParseTemplate("repository.html", "widget-base.html") + +type repositoryWidget struct { + widgetBase `yaml:",inline"` + RequestedRepository string `yaml:"repository"` + Token string `yaml:"token"` + PullRequestsLimit int `yaml:"pull-requests-limit"` + IssuesLimit int `yaml:"issues-limit"` + CommitsLimit int `yaml:"commits-limit"` + Repository repository `yaml:"-"` +} + +func (widget *repositoryWidget) initialize() error { + widget.withTitle("Repository").withCacheDuration(1 * time.Hour) + + if widget.PullRequestsLimit == 0 || widget.PullRequestsLimit < -1 { + widget.PullRequestsLimit = 3 + } + + if widget.IssuesLimit == 0 || widget.IssuesLimit < -1 { + widget.IssuesLimit = 3 + } + + if widget.CommitsLimit == 0 || widget.CommitsLimit < -1 { + widget.CommitsLimit = -1 + } + + return nil +} + +func (widget *repositoryWidget) update(ctx context.Context) { + details, err := fetchRepositoryDetailsFromGithub( + widget.RequestedRepository, + string(widget.Token), + widget.PullRequestsLimit, + widget.IssuesLimit, + widget.CommitsLimit, + ) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + widget.Repository = details +} + +func (widget *repositoryWidget) Render() template.HTML { + return widget.renderTemplate(widget, repositoryWidgetTemplate) +} + +type repository struct { + Name string + Stars int + Forks int + OpenPullRequests int + PullRequests []githubTicket + OpenIssues int + Issues []githubTicket + LastCommits int + Commits []githubCommitDetails +} + +type githubTicket struct { + Number int + CreatedAt time.Time + Title string +} + +type githubCommitDetails struct { + Sha string + Author string + CreatedAt time.Time + Message string +} + +type githubRepositoryResponseJson struct { + Name string `json:"full_name"` + Stars int `json:"stargazers_count"` + Forks int `json:"forks_count"` +} + +type githubTicketResponseJson struct { + Count int `json:"total_count"` + Tickets []struct { + Number int `json:"number"` + CreatedAt string `json:"created_at"` + Title string `json:"title"` + } `json:"items"` +} + +type gitHubCommitResponseJson struct { + Sha string `json:"sha"` + Commit struct { + Author struct { + Name string `json:"name"` + Date string `json:"date"` + } `json:"author"` + Message string `json:"message"` + } `json:"commit"` +} + +func fetchRepositoryDetailsFromGithub(repo string, token string, maxPRs int, maxIssues int, maxCommits int) (repository, error) { + repositoryRequest, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s", repo), nil) + if err != nil { + return repository{}, fmt.Errorf("%w: could not create request with repository: %v", errNoContent, err) + } + + PRsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:pr+is:open+repo:%s&per_page=%d", repo, maxPRs), nil) + issuesRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/search/issues?q=is:issue+is:open+repo:%s&per_page=%d", repo, maxIssues), nil) + CommitsRequest, _ := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=%d", repo, maxCommits), nil) + + if token != "" { + token = fmt.Sprintf("Bearer %s", token) + repositoryRequest.Header.Add("Authorization", token) + PRsRequest.Header.Add("Authorization", token) + issuesRequest.Header.Add("Authorization", token) + CommitsRequest.Header.Add("Authorization", token) + } + + var repositoryResponse githubRepositoryResponseJson + var detailsErr error + var PRsResponse githubTicketResponseJson + var PRsErr error + var issuesResponse githubTicketResponseJson + var issuesErr error + var commitsResponse []gitHubCommitResponseJson + var CommitsErr error + var wg sync.WaitGroup + + wg.Add(1) + go (func() { + defer wg.Done() + repositoryResponse, detailsErr = decodeJsonFromRequest[githubRepositoryResponseJson](defaultHTTPClient, repositoryRequest) + })() + + if maxPRs > 0 { + wg.Add(1) + go (func() { + defer wg.Done() + PRsResponse, PRsErr = decodeJsonFromRequest[githubTicketResponseJson](defaultHTTPClient, PRsRequest) + })() + } + + if maxIssues > 0 { + wg.Add(1) + go (func() { + defer wg.Done() + issuesResponse, issuesErr = decodeJsonFromRequest[githubTicketResponseJson](defaultHTTPClient, issuesRequest) + })() + } + + if maxCommits > 0 { + wg.Add(1) + go (func() { + defer wg.Done() + commitsResponse, CommitsErr = decodeJsonFromRequest[[]gitHubCommitResponseJson](defaultHTTPClient, CommitsRequest) + })() + } + + wg.Wait() + + if detailsErr != nil { + return repository{}, fmt.Errorf("%w: could not get repository details: %s", errNoContent, detailsErr) + } + + details := repository{ + Name: repositoryResponse.Name, + Stars: repositoryResponse.Stars, + Forks: repositoryResponse.Forks, + PullRequests: make([]githubTicket, 0, len(PRsResponse.Tickets)), + Issues: make([]githubTicket, 0, len(issuesResponse.Tickets)), + Commits: make([]githubCommitDetails, 0, len(commitsResponse)), + } + + err = nil + + if maxPRs > 0 { + if PRsErr != nil { + err = fmt.Errorf("%w: could not get PRs: %s", errPartialContent, PRsErr) + } else { + details.OpenPullRequests = PRsResponse.Count + + for i := range PRsResponse.Tickets { + details.PullRequests = append(details.PullRequests, githubTicket{ + Number: PRsResponse.Tickets[i].Number, + CreatedAt: parseRFC3339Time(PRsResponse.Tickets[i].CreatedAt), + Title: PRsResponse.Tickets[i].Title, + }) + } + } + } + + if maxIssues > 0 { + if issuesErr != nil { + // TODO: fix, overwriting the previous error + err = fmt.Errorf("%w: could not get issues: %s", errPartialContent, issuesErr) + } else { + details.OpenIssues = issuesResponse.Count + + for i := range issuesResponse.Tickets { + details.Issues = append(details.Issues, githubTicket{ + Number: issuesResponse.Tickets[i].Number, + CreatedAt: parseRFC3339Time(issuesResponse.Tickets[i].CreatedAt), + Title: issuesResponse.Tickets[i].Title, + }) + } + } + } + + if maxCommits > 0 { + if CommitsErr != nil { + err = fmt.Errorf("%w: could not get commits: %s", errPartialContent, CommitsErr) + } else { + for i := range commitsResponse { + details.Commits = append(details.Commits, githubCommitDetails{ + Sha: commitsResponse[i].Sha, + Author: commitsResponse[i].Commit.Author.Name, + CreatedAt: parseRFC3339Time(commitsResponse[i].Commit.Author.Date), + Message: strings.SplitN(commitsResponse[i].Commit.Message, "\n\n", 2)[0], + }) + } + } + } + + return details, err +} diff --git a/internal/glance/widget-rss.go b/internal/glance/widget-rss.go new file mode 100644 index 0000000..1598371 --- /dev/null +++ b/internal/glance/widget-rss.go @@ -0,0 +1,361 @@ +package glance + +import ( + "context" + "fmt" + "html" + "html/template" + "io" + "log/slog" + "net/http" + "net/url" + "regexp" + "sort" + "strings" + "time" + + "github.com/mmcdole/gofeed" + gofeedext "github.com/mmcdole/gofeed/extensions" +) + +var ( + rssWidgetTemplate = mustParseTemplate("rss-list.html", "widget-base.html") + rssWidgetDetailedListTemplate = mustParseTemplate("rss-detailed-list.html", "widget-base.html") + rssWidgetHorizontalCardsTemplate = mustParseTemplate("rss-horizontal-cards.html", "widget-base.html") + rssWidgetHorizontalCards2Template = mustParseTemplate("rss-horizontal-cards-2.html", "widget-base.html") +) + +type rssWidget struct { + widgetBase `yaml:",inline"` + FeedRequests []rssFeedRequest `yaml:"feeds"` + Style string `yaml:"style"` + ThumbnailHeight float64 `yaml:"thumbnail-height"` + CardHeight float64 `yaml:"card-height"` + Items rssFeedItemList `yaml:"-"` + Limit int `yaml:"limit"` + CollapseAfter int `yaml:"collapse-after"` + SingleLineTitles bool `yaml:"single-line-titles"` + PreserveOrder bool `yaml:"preserve-order"` + NoItemsMessage string `yaml:"-"` +} + +func (widget *rssWidget) initialize() error { + widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour) + + if widget.Limit <= 0 { + widget.Limit = 25 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + if widget.ThumbnailHeight < 0 { + widget.ThumbnailHeight = 0 + } + + if widget.CardHeight < 0 { + widget.CardHeight = 0 + } + + if widget.Style == "detailed-list" { + for i := range widget.FeedRequests { + widget.FeedRequests[i].IsDetailed = true + } + } + + widget.NoItemsMessage = "No items were returned from the feeds." + + return nil +} + +func (widget *rssWidget) update(ctx context.Context) { + items, err := fetchItemsFromRSSFeeds(widget.FeedRequests) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if !widget.PreserveOrder { + items.sortByNewest() + } + + if len(items) > widget.Limit { + items = items[:widget.Limit] + } + + widget.Items = items +} + +func (widget *rssWidget) Render() template.HTML { + if widget.Style == "horizontal-cards" { + return widget.renderTemplate(widget, rssWidgetHorizontalCardsTemplate) + } + + if widget.Style == "horizontal-cards-2" { + return widget.renderTemplate(widget, rssWidgetHorizontalCards2Template) + } + + if widget.Style == "detailed-list" { + return widget.renderTemplate(widget, rssWidgetDetailedListTemplate) + } + + return widget.renderTemplate(widget, rssWidgetTemplate) +} + +type rssFeedItem struct { + ChannelName string + ChannelURL string + Title string + Link string + ImageURL string + Categories []string + Description string + PublishedAt time.Time +} + +// doesn't cover all cases but works the vast majority of the time +var htmlTagsWithAttributesPattern = regexp.MustCompile(`<\/?[a-zA-Z0-9-]+ *(?:[a-zA-Z-]+=(?:"|').*?(?:"|') ?)* *\/?>`) + +func sanitizeFeedDescription(description string) string { + if description == "" { + return "" + } + + description = strings.ReplaceAll(description, "\n", " ") + description = htmlTagsWithAttributesPattern.ReplaceAllString(description, "") + description = sequentialWhitespacePattern.ReplaceAllString(description, " ") + description = strings.TrimSpace(description) + description = html.UnescapeString(description) + + return description +} + +func shortenFeedDescriptionLen(description string, maxLen int) string { + description, _ = limitStringLength(description, 1000) + description = sanitizeFeedDescription(description) + description, limited := limitStringLength(description, maxLen) + + if limited { + description += "…" + } + + return description +} + +type rssFeedRequest struct { + URL string `yaml:"url"` + Title string `yaml:"title"` + HideCategories bool `yaml:"hide-categories"` + HideDescription bool `yaml:"hide-description"` + Limit int `yaml:"limit"` + ItemLinkPrefix string `yaml:"item-link-prefix"` + Headers map[string]string `yaml:"headers"` + IsDetailed bool `yaml:"-"` +} + +type rssFeedItemList []rssFeedItem + +func (f rssFeedItemList) sortByNewest() rssFeedItemList { + sort.Slice(f, func(i, j int) bool { + return f[i].PublishedAt.After(f[j].PublishedAt) + }) + + return f +} + +var feedParser = gofeed.NewParser() + +func fetchItemsFromRSSFeedTask(request rssFeedRequest) ([]rssFeedItem, error) { + req, err := http.NewRequest("GET", request.URL, nil) + if err != nil { + return nil, err + } + + for key, value := range request.Headers { + req.Header.Add(key, value) + } + + resp, err := defaultHTTPClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code %d from %s", resp.StatusCode, request.URL) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + feed, err := feedParser.ParseString(string(body)) + if err != nil { + return nil, err + } + + if request.Limit > 0 && len(feed.Items) > request.Limit { + feed.Items = feed.Items[:request.Limit] + } + + items := make(rssFeedItemList, 0, len(feed.Items)) + + for i := range feed.Items { + item := feed.Items[i] + + rssItem := rssFeedItem{ + ChannelURL: feed.Link, + } + + if request.ItemLinkPrefix != "" { + rssItem.Link = request.ItemLinkPrefix + item.Link + } else if strings.HasPrefix(item.Link, "http://") || strings.HasPrefix(item.Link, "https://") { + rssItem.Link = item.Link + } else { + parsedUrl, err := url.Parse(feed.Link) + if err != nil { + parsedUrl, err = url.Parse(request.URL) + } + + if err == nil { + var link string + + if len(item.Link) > 0 && item.Link[0] == '/' { + link = item.Link + } else { + link = "/" + item.Link + } + + rssItem.Link = parsedUrl.Scheme + "://" + parsedUrl.Host + link + } + } + + if item.Title != "" { + rssItem.Title = html.UnescapeString(item.Title) + } else { + rssItem.Title = shortenFeedDescriptionLen(item.Description, 100) + } + + if request.IsDetailed { + if !request.HideDescription && item.Description != "" && item.Title != "" { + rssItem.Description = shortenFeedDescriptionLen(item.Description, 200) + } + + if !request.HideCategories { + var categories = make([]string, 0, 6) + + for _, category := range item.Categories { + if len(categories) == 6 { + break + } + + if len(category) == 0 || len(category) > 30 { + continue + } + + categories = append(categories, category) + } + + rssItem.Categories = categories + } + } + + if request.Title != "" { + rssItem.ChannelName = request.Title + } else { + rssItem.ChannelName = feed.Title + } + + if item.Image != nil { + rssItem.ImageURL = item.Image.URL + } else if url := findThumbnailInItemExtensions(item); url != "" { + rssItem.ImageURL = url + } else if feed.Image != nil { + if len(feed.Image.URL) > 0 && feed.Image.URL[0] == '/' { + rssItem.ImageURL = strings.TrimRight(feed.Link, "/") + feed.Image.URL + } else { + rssItem.ImageURL = feed.Image.URL + } + } + + if item.PublishedParsed != nil { + rssItem.PublishedAt = *item.PublishedParsed + } else { + rssItem.PublishedAt = time.Now() + } + + items = append(items, rssItem) + } + + return items, nil +} + +func recursiveFindThumbnailInExtensions(extensions map[string][]gofeedext.Extension) string { + for _, exts := range extensions { + for _, ext := range exts { + if ext.Name == "thumbnail" || ext.Name == "image" { + if url, ok := ext.Attrs["url"]; ok { + return url + } + } + + if ext.Children != nil { + if url := recursiveFindThumbnailInExtensions(ext.Children); url != "" { + return url + } + } + } + } + + return "" +} + +func findThumbnailInItemExtensions(item *gofeed.Item) string { + media, ok := item.Extensions["media"] + + if !ok { + return "" + } + + return recursiveFindThumbnailInExtensions(media) +} + +func fetchItemsFromRSSFeeds(requests []rssFeedRequest) (rssFeedItemList, error) { + job := newJob(fetchItemsFromRSSFeedTask, requests).withWorkers(30) + feeds, errs, err := workerPoolDo(job) + if err != nil { + return nil, fmt.Errorf("%w: %v", errNoContent, err) + } + + failed := 0 + entries := make(rssFeedItemList, 0, len(feeds)*10) + seen := make(map[string]struct{}) + + for i := range feeds { + if errs[i] != nil { + failed++ + slog.Error("Failed to get RSS feed", "url", requests[i].URL, "error", errs[i]) + continue + } + + for _, item := range feeds[i] { + if _, exists := seen[item.Link]; exists { + continue + } + entries = append(entries, item) + seen[item.Link] = struct{}{} + } + } + + if failed == len(requests) { + return nil, errNoContent + } + + if failed > 0 { + return entries, fmt.Errorf("%w: missing %d RSS feeds", errPartialContent, failed) + } + + return entries, nil +} diff --git a/internal/glance/widget-search.go b/internal/glance/widget-search.go new file mode 100644 index 0000000..9d2b600 --- /dev/null +++ b/internal/glance/widget-search.go @@ -0,0 +1,73 @@ +package glance + +import ( + "fmt" + "html/template" + "strings" +) + +var searchWidgetTemplate = mustParseTemplate("search.html", "widget-base.html") + +type SearchBang struct { + Title string + Shortcut string + URL string +} + +type searchWidget struct { + widgetBase `yaml:",inline"` + cachedHTML template.HTML `yaml:"-"` + SearchEngine string `yaml:"search-engine"` + Bangs []SearchBang `yaml:"bangs"` + NewTab bool `yaml:"new-tab"` + Autofocus bool `yaml:"autofocus"` + Placeholder string `yaml:"placeholder"` +} + +func convertSearchUrl(url string) string { + // Go's template is being stubborn and continues to escape the curlies in the + // URL regardless of what the type of the variable is so this is my way around it + return strings.ReplaceAll(url, "{QUERY}", "!QUERY!") +} + +var searchEngines = map[string]string{ + "duckduckgo": "https://duckduckgo.com/?q={QUERY}", + "google": "https://www.google.com/search?q={QUERY}", +} + +func (widget *searchWidget) initialize() error { + widget.withTitle("Search").withError(nil) + + if widget.SearchEngine == "" { + widget.SearchEngine = "duckduckgo" + } + + if widget.Placeholder == "" { + widget.Placeholder = "Type here to search…" + } + + if url, ok := searchEngines[widget.SearchEngine]; ok { + widget.SearchEngine = url + } + + widget.SearchEngine = convertSearchUrl(widget.SearchEngine) + + for i := range widget.Bangs { + if widget.Bangs[i].Shortcut == "" { + return fmt.Errorf("search bang #%d has no shortcut", i+1) + } + + if widget.Bangs[i].URL == "" { + return fmt.Errorf("search bang #%d has no URL", i+1) + } + + widget.Bangs[i].URL = convertSearchUrl(widget.Bangs[i].URL) + } + + widget.cachedHTML = widget.renderTemplate(widget, searchWidgetTemplate) + return nil +} + +func (widget *searchWidget) Render() template.HTML { + return widget.cachedHTML +} diff --git a/internal/glance/widget-server-stats.go b/internal/glance/widget-server-stats.go new file mode 100644 index 0000000..90bf8db --- /dev/null +++ b/internal/glance/widget-server-stats.go @@ -0,0 +1,117 @@ +package glance + +import ( + "context" + "html/template" + "log/slog" + "net/http" + "strconv" + "strings" + "sync" + "time" + + "github.com/glanceapp/glance/pkg/sysinfo" +) + +var serverStatsWidgetTemplate = mustParseTemplate("server-stats.html", "widget-base.html") + +type serverStatsWidget struct { + widgetBase `yaml:",inline"` + Servers []serverStatsRequest `yaml:"servers"` +} + +func (widget *serverStatsWidget) initialize() error { + widget.withTitle("Server Stats").withCacheDuration(15 * time.Second) + widget.widgetBase.WIP = true + + if len(widget.Servers) == 0 { + widget.Servers = []serverStatsRequest{{Type: "local"}} + } + + for i := range widget.Servers { + widget.Servers[i].URL = strings.TrimRight(widget.Servers[i].URL, "/") + + if widget.Servers[i].Timeout == 0 { + widget.Servers[i].Timeout = durationField(3 * time.Second) + } + } + + return nil +} + +func (widget *serverStatsWidget) update(context.Context) { + // Refactor later, most of it may change depending on feedback + var wg sync.WaitGroup + + for i := range widget.Servers { + serv := &widget.Servers[i] + + if serv.Type == "local" { + info, errs := sysinfo.Collect(serv.SystemInfoRequest) + + if len(errs) > 0 { + for i := range errs { + slog.Warn("Getting system info: " + errs[i].Error()) + } + } + + serv.IsReachable = true + serv.Info = info + } else { + wg.Add(1) + go func() { + defer wg.Done() + info, err := fetchRemoteServerInfo(serv) + if err != nil { + slog.Warn("Getting remote system info: " + err.Error()) + serv.IsReachable = false + serv.Info = &sysinfo.SystemInfo{ + Hostname: "Unnamed server #" + strconv.Itoa(i+1), + } + } else { + serv.IsReachable = true + serv.Info = info + } + }() + } + } + + wg.Wait() + widget.withError(nil).scheduleNextUpdate() +} + +func (widget *serverStatsWidget) Render() template.HTML { + return widget.renderTemplate(widget, serverStatsWidgetTemplate) +} + +type serverStatsRequest struct { + *sysinfo.SystemInfoRequest `yaml:",inline"` + Info *sysinfo.SystemInfo `yaml:"-"` + IsReachable bool `yaml:"-"` + StatusText string `yaml:"-"` + Name string `yaml:"name"` + HideSwap bool `yaml:"hide-swap"` + Type string `yaml:"type"` + URL string `yaml:"url"` + Token string `yaml:"token"` + Timeout durationField `yaml:"timeout"` + // Support for other agents + // Provider string `yaml:"provider"` +} + +func fetchRemoteServerInfo(infoReq *serverStatsRequest) (*sysinfo.SystemInfo, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(infoReq.Timeout)) + defer cancel() + + request, _ := http.NewRequestWithContext(ctx, "GET", infoReq.URL+"/api/sysinfo/all", nil) + if infoReq.Token != "" { + request.Header.Set("Authorization", "Bearer "+infoReq.Token) + } + + info, err := decodeJsonFromRequest[*sysinfo.SystemInfo](defaultHTTPClient, request) + if err != nil { + return nil, err + } + + return info, nil +} diff --git a/internal/glance/widget-shared.go b/internal/glance/widget-shared.go new file mode 100644 index 0000000..45144ac --- /dev/null +++ b/internal/glance/widget-shared.go @@ -0,0 +1,64 @@ +package glance + +import ( + "math" + "sort" + "time" +) + +const twitchGqlEndpoint = "https://gql.twitch.tv/gql" +const twitchGqlClientId = "kimne78kx3ncx6brgo4mv6wki5h1ko" + +var forumPostsTemplate = mustParseTemplate("forum-posts.html", "widget-base.html") + +type forumPost struct { + Title string + DiscussionUrl string + TargetUrl string + TargetUrlDomain string + ThumbnailUrl string + CommentCount int + Score int + Engagement float64 + TimePosted time.Time + Tags []string + IsCrosspost bool +} + +type forumPostList []forumPost + +const depreciatePostsOlderThanHours = 7 +const maxDepreciation = 0.9 +const maxDepreciationAfterHours = 24 + +func (p forumPostList) calculateEngagement() { + var totalComments int + var totalScore int + + for i := range p { + totalComments += p[i].CommentCount + totalScore += p[i].Score + } + + numberOfPosts := float64(len(p)) + averageComments := float64(totalComments) / numberOfPosts + averageScore := float64(totalScore) / numberOfPosts + + for i := range p { + p[i].Engagement = (float64(p[i].CommentCount)/averageComments + float64(p[i].Score)/averageScore) / 2 + + elapsed := time.Since(p[i].TimePosted) + + if elapsed < time.Hour*depreciatePostsOlderThanHours { + continue + } + + p[i].Engagement *= 1.0 - (math.Max(elapsed.Hours()-depreciatePostsOlderThanHours, maxDepreciationAfterHours)/maxDepreciationAfterHours)*maxDepreciation + } +} + +func (p forumPostList) sortByEngagement() { + sort.Slice(p, func(i, j int) bool { + return p[i].Engagement > p[j].Engagement + }) +} diff --git a/internal/glance/widget-split-column.go b/internal/glance/widget-split-column.go new file mode 100644 index 0000000..71747c9 --- /dev/null +++ b/internal/glance/widget-split-column.go @@ -0,0 +1,45 @@ +package glance + +import ( + "context" + "html/template" + "time" +) + +var splitColumnWidgetTemplate = mustParseTemplate("split-column.html", "widget-base.html") + +type splitColumnWidget struct { + widgetBase `yaml:",inline"` + containerWidgetBase `yaml:",inline"` + MaxColumns int `yaml:"max-columns"` +} + +func (widget *splitColumnWidget) initialize() error { + widget.withError(nil).withTitle("Split Column").setHideHeader(true) + + if err := widget.containerWidgetBase._initializeWidgets(); err != nil { + return err + } + + if widget.MaxColumns < 2 { + widget.MaxColumns = 2 + } + + return nil +} + +func (widget *splitColumnWidget) update(ctx context.Context) { + widget.containerWidgetBase._update(ctx) +} + +func (widget *splitColumnWidget) setProviders(providers *widgetProviders) { + widget.containerWidgetBase._setProviders(providers) +} + +func (widget *splitColumnWidget) requiresUpdate(now *time.Time) bool { + return widget.containerWidgetBase._requiresUpdate(now) +} + +func (widget *splitColumnWidget) Render() template.HTML { + return widget.renderTemplate(widget, splitColumnWidgetTemplate) +} diff --git a/internal/glance/widget-twitch-channels.go b/internal/glance/widget-twitch-channels.go new file mode 100644 index 0000000..1290a26 --- /dev/null +++ b/internal/glance/widget-twitch-channels.go @@ -0,0 +1,238 @@ +package glance + +import ( + "context" + "encoding/json" + "fmt" + "html/template" + "log/slog" + "net/http" + "sort" + "strings" + "time" +) + +var twitchChannelsWidgetTemplate = mustParseTemplate("twitch-channels.html", "widget-base.html") + +type twitchChannelsWidget struct { + widgetBase `yaml:",inline"` + ChannelsRequest []string `yaml:"channels"` + Channels []twitchChannel `yaml:"-"` + CollapseAfter int `yaml:"collapse-after"` + SortBy string `yaml:"sort-by"` +} + +func (widget *twitchChannelsWidget) initialize() error { + widget. + withTitle("Twitch Channels"). + withTitleURL("https://www.twitch.tv/directory/following"). + withCacheDuration(time.Minute * 10) + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + if widget.SortBy != "viewers" && widget.SortBy != "live" { + widget.SortBy = "viewers" + } + + return nil +} + +func (widget *twitchChannelsWidget) update(ctx context.Context) { + channels, err := fetchChannelsFromTwitch(widget.ChannelsRequest) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if widget.SortBy == "viewers" { + channels.sortByViewers() + } else if widget.SortBy == "live" { + channels.sortByLive() + } + + widget.Channels = channels +} + +func (widget *twitchChannelsWidget) Render() template.HTML { + return widget.renderTemplate(widget, twitchChannelsWidgetTemplate) +} + +type twitchChannel struct { + Login string + Exists bool + Name string + StreamTitle string + AvatarUrl string + IsLive bool + LiveSince time.Time + Category string + CategorySlug string + ViewersCount int +} + +type twitchChannelList []twitchChannel + +func (channels twitchChannelList) sortByViewers() { + sort.Slice(channels, func(i, j int) bool { + return channels[i].ViewersCount > channels[j].ViewersCount + }) +} + +func (channels twitchChannelList) sortByLive() { + sort.SliceStable(channels, func(i, j int) bool { + return channels[i].IsLive && !channels[j].IsLive + }) +} + +type twitchOperationResponse struct { + Data json.RawMessage + Extensions struct { + OperationName string `json:"operationName"` + } +} + +type twitchChannelShellOperationResponse struct { + UserOrError struct { + Type string `json:"__typename"` + DisplayName string `json:"displayName"` + ProfileImageUrl string `json:"profileImageURL"` + Stream *struct { + ViewersCount int `json:"viewersCount"` + } + } `json:"userOrError"` +} + +type twitchStreamMetadataOperationResponse struct { + UserOrNull *struct { + Stream *struct { + StartedAt string `json:"createdAt"` + Game *struct { + Slug string `json:"slug"` + Name string `json:"name"` + } `json:"game"` + } `json:"stream"` + LastBroadcast *struct { + Title string `json:"title"` + } + } `json:"user"` +} + +const twitchChannelStatusOperationRequestBody = `[ +{"operationName":"ChannelShell","variables":{"login":"%s"},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"580ab410bcd0c1ad194224957ae2241e5d252b2c5173d8e0cce9d32d5bb14efe"}}}, +{"operationName":"StreamMetadata","variables":{"channelLogin":"%s"},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"676ee2f834ede42eb4514cdb432b3134fefc12590080c9a2c9bb44a2a4a63266"}}} +]` + +// TODO: rework +// The operations for multiple channels can all be sent in a single request +// rather than sending a separate request for each channel. Need to figure out +// what the limit is for max operations per request and batch operations in +// multiple requests if number of channels exceeds allowed limit. + +func fetchChannelFromTwitchTask(channel string) (twitchChannel, error) { + result := twitchChannel{ + Login: strings.ToLower(channel), + } + + reader := strings.NewReader(fmt.Sprintf(twitchChannelStatusOperationRequestBody, channel, channel)) + request, _ := http.NewRequest("POST", twitchGqlEndpoint, reader) + request.Header.Add("Client-ID", twitchGqlClientId) + + response, err := decodeJsonFromRequest[[]twitchOperationResponse](defaultHTTPClient, request) + if err != nil { + return result, err + } + + if len(response) != 2 { + return result, fmt.Errorf("expected 2 operation responses, got %d", len(response)) + } + + var channelShell twitchChannelShellOperationResponse + var streamMetadata twitchStreamMetadataOperationResponse + + for i := range response { + switch response[i].Extensions.OperationName { + case "ChannelShell": + if err = json.Unmarshal(response[i].Data, &channelShell); err != nil { + return result, fmt.Errorf("unmarshalling channel shell: %w", err) + } + case "StreamMetadata": + if err = json.Unmarshal(response[i].Data, &streamMetadata); err != nil { + return result, fmt.Errorf("unmarshalling stream metadata: %w", err) + } + default: + return result, fmt.Errorf("unknown operation name: %s", response[i].Extensions.OperationName) + } + } + + if channelShell.UserOrError.Type != "User" { + result.Name = result.Login + return result, nil + } + + result.Exists = true + result.Name = channelShell.UserOrError.DisplayName + result.AvatarUrl = channelShell.UserOrError.ProfileImageUrl + + if channelShell.UserOrError.Stream != nil { + result.IsLive = true + result.ViewersCount = channelShell.UserOrError.Stream.ViewersCount + + if streamMetadata.UserOrNull != nil && streamMetadata.UserOrNull.Stream != nil { + if streamMetadata.UserOrNull.LastBroadcast != nil { + result.StreamTitle = streamMetadata.UserOrNull.LastBroadcast.Title + } + + if streamMetadata.UserOrNull.Stream.Game != nil { + result.Category = streamMetadata.UserOrNull.Stream.Game.Name + result.CategorySlug = streamMetadata.UserOrNull.Stream.Game.Slug + } + startedAt, err := time.Parse("2006-01-02T15:04:05Z", streamMetadata.UserOrNull.Stream.StartedAt) + + if err == nil { + result.LiveSince = startedAt + } else { + slog.Warn("Failed to parse Twitch stream started at", "error", err, "started_at", streamMetadata.UserOrNull.Stream.StartedAt) + } + } + } else { + // This prevents live channels with 0 viewers from being + // incorrectly sorted lower than offline channels + result.ViewersCount = -1 + } + + return result, nil +} + +func fetchChannelsFromTwitch(channelLogins []string) (twitchChannelList, error) { + result := make(twitchChannelList, 0, len(channelLogins)) + + job := newJob(fetchChannelFromTwitchTask, channelLogins).withWorkers(10) + channels, errs, err := workerPoolDo(job) + if err != nil { + return result, err + } + + var failed int + + for i := range channels { + if errs[i] != nil { + failed++ + slog.Error("Failed to fetch Twitch channel", "channel", channelLogins[i], "error", errs[i]) + continue + } + + result = append(result, channels[i]) + } + + if failed == len(channelLogins) { + return result, errNoContent + } + + if failed > 0 { + return result, fmt.Errorf("%w: failed to fetch %d channels", errPartialContent, failed) + } + + return result, nil +} diff --git a/internal/glance/widget-twitch-top-games.go b/internal/glance/widget-twitch-top-games.go new file mode 100644 index 0000000..4235bc9 --- /dev/null +++ b/internal/glance/widget-twitch-top-games.go @@ -0,0 +1,125 @@ +package glance + +import ( + "context" + "errors" + "fmt" + "html/template" + "net/http" + "slices" + "strings" + "time" +) + +var twitchGamesWidgetTemplate = mustParseTemplate("twitch-games-list.html", "widget-base.html") + +type twitchGamesWidget struct { + widgetBase `yaml:",inline"` + Categories []twitchCategory `yaml:"-"` + Exclude []string `yaml:"exclude"` + Limit int `yaml:"limit"` + CollapseAfter int `yaml:"collapse-after"` +} + +func (widget *twitchGamesWidget) initialize() error { + widget. + withTitle("Top games on Twitch"). + withTitleURL("https://www.twitch.tv/directory?sort=VIEWER_COUNT"). + withCacheDuration(time.Minute * 10) + + if widget.Limit <= 0 { + widget.Limit = 10 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 5 + } + + return nil +} + +func (widget *twitchGamesWidget) update(ctx context.Context) { + categories, err := fetchTopGamesFromTwitch(widget.Exclude, widget.Limit) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + widget.Categories = categories +} + +func (widget *twitchGamesWidget) Render() template.HTML { + return widget.renderTemplate(widget, twitchGamesWidgetTemplate) +} + +type twitchCategory struct { + Slug string `json:"slug"` + Name string `json:"name"` + AvatarUrl string `json:"avatarURL"` + ViewersCount int `json:"viewersCount"` + Tags []struct { + Name string `json:"tagName"` + } `json:"tags"` + GameReleaseDate string `json:"originalReleaseDate"` + IsNew bool `json:"-"` +} + +type twitchDirectoriesOperationResponse struct { + Data struct { + DirectoriesWithTags struct { + Edges []struct { + Node twitchCategory `json:"node"` + } `json:"edges"` + } `json:"directoriesWithTags"` + } `json:"data"` +} + +const twitchDirectoriesOperationRequestBody = `[ +{"operationName": "BrowsePage_AllDirectories","variables": {"limit": %d,"options": {"sort": "VIEWER_COUNT","tags": []}},"extensions": {"persistedQuery": {"version": 1,"sha256Hash": "2f67f71ba89f3c0ed26a141ec00da1defecb2303595f5cda4298169549783d9e"}}} +]` + +func fetchTopGamesFromTwitch(exclude []string, limit int) ([]twitchCategory, error) { + reader := strings.NewReader(fmt.Sprintf(twitchDirectoriesOperationRequestBody, len(exclude)+limit)) + request, _ := http.NewRequest("POST", twitchGqlEndpoint, reader) + request.Header.Add("Client-ID", twitchGqlClientId) + response, err := decodeJsonFromRequest[[]twitchDirectoriesOperationResponse](defaultHTTPClient, request) + if err != nil { + return nil, err + } + + if len(response) == 0 { + return nil, errors.New("no categories could be retrieved") + } + + edges := (response)[0].Data.DirectoriesWithTags.Edges + categories := make([]twitchCategory, 0, len(edges)) + + for i := range edges { + if slices.Contains(exclude, edges[i].Node.Slug) { + continue + } + + category := &edges[i].Node + category.AvatarUrl = strings.Replace(category.AvatarUrl, "285x380", "144x192", 1) + + if len(category.Tags) > 2 { + category.Tags = category.Tags[:2] + } + + gameReleasedDate, err := time.Parse("2006-01-02T15:04:05Z", category.GameReleaseDate) + + if err == nil { + if time.Since(gameReleasedDate) < 14*24*time.Hour { + category.IsNew = true + } + } + + categories = append(categories, *category) + } + + if len(categories) > limit { + categories = categories[:limit] + } + + return categories, nil +} diff --git a/internal/feed/requests.go b/internal/glance/widget-utils.go similarity index 71% rename from internal/feed/requests.go rename to internal/glance/widget-utils.go index 0f49ff0..9489cbf 100644 --- a/internal/feed/requests.go +++ b/internal/glance/widget-utils.go @@ -1,65 +1,80 @@ -package feed +package glance import ( "context" + "crypto/tls" "encoding/json" "encoding/xml" + "errors" "fmt" "io" + "math/rand/v2" "net/http" + "strconv" "sync" + "sync/atomic" "time" ) -var defaultClient = &http.Client{ - Timeout: 5 * time.Second, +var ( + errNoContent = errors.New("failed to retrieve any content") + errPartialContent = errors.New("failed to retrieve some of the content") +) + +const defaultClientTimeout = 5 * time.Second + +var defaultHTTPClient = &http.Client{ + Timeout: defaultClientTimeout, } -type RequestDoer interface { +var defaultInsecureHTTPClient = &http.Client{ + Timeout: defaultClientTimeout, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, +} + +type requestDoer interface { Do(*http.Request) (*http.Response, error) } -func addBrowserUserAgentHeader(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") -} +var userAgentPersistentVersion atomic.Int32 -func truncateString(s string, maxLen int) string { - asRunes := []rune(s) - - if len(asRunes) > maxLen { - return string(asRunes[:maxLen]) +func setBrowserUserAgentHeader(request *http.Request) { + if rand.IntN(2000) == 0 { + userAgentPersistentVersion.Store(rand.Int32N(5)) } - return s + 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) { - response, err := client.Do(request) +func decodeJsonFromRequest[T any](client requestDoer, request *http.Request) (T, error) { var result T + response, err := client.Do(request) if err != nil { return result, err } - defer response.Body.Close() body, err := io.ReadAll(response.Body) - if err != nil { return result, err } if response.StatusCode != http.StatusOK { + truncatedBody, _ := limitStringLength(string(body), 256) + return result, fmt.Errorf( "unexpected status code %d for %s, response: %s", response.StatusCode, request.URL, - truncateString(string(body), 256), + truncatedBody, ) } err = json.Unmarshal(body, &result) - if err != nil { return result, err } @@ -67,40 +82,39 @@ func decodeJsonFromRequest[T any](client RequestDoer, request *http.Request) (T, return result, nil } -func decodeJsonFromRequestTask[T any](client RequestDoer) func(*http.Request) (T, error) { +func decodeJsonFromRequestTask[T any](client requestDoer) func(*http.Request) (T, error) { return func(request *http.Request) (T, error) { return decodeJsonFromRequest[T](client, request) } } // TODO: tidy up, these are a copy of the above but with a line changed -func decodeXmlFromRequest[T any](client RequestDoer, request *http.Request) (T, error) { - response, err := client.Do(request) +func decodeXmlFromRequest[T any](client requestDoer, request *http.Request) (T, error) { var result T + response, err := client.Do(request) if err != nil { return result, err } - defer response.Body.Close() body, err := io.ReadAll(response.Body) - if err != nil { return result, err } if response.StatusCode != http.StatusOK { + truncatedBody, _ := limitStringLength(string(body), 256) + return result, fmt.Errorf( "unexpected status code %d for %s, response: %s", response.StatusCode, request.URL, - truncateString(string(body), 256), + truncatedBody, ) } err = xml.Unmarshal(body, &result) - if err != nil { return result, err } @@ -108,7 +122,7 @@ func decodeXmlFromRequest[T any](client RequestDoer, request *http.Request) (T, return result, nil } -func decodeXmlFromRequestTask[T any](client RequestDoer) func(*http.Request) (T, error) { +func decodeXmlFromRequestTask[T any](client requestDoer) func(*http.Request) (T, error) { return func(request *http.Request) (T, error) { return decodeXmlFromRequest[T](client, request) } @@ -167,6 +181,11 @@ func workerPoolDo[I any, O any](job *workerPoolJob[I, O]) ([]O, []error, error) return results, errs, nil } + if len(job.data) == 1 { + results[0], errs[0] = job.task(job.data[0]) + return results, errs, nil + } + tasksQueue := make(chan *workerPoolTask[I, O]) resultsQueue := make(chan *workerPoolTask[I, O]) diff --git a/internal/glance/widget-videos.go b/internal/glance/widget-videos.go new file mode 100644 index 0000000..ff79864 --- /dev/null +++ b/internal/glance/widget-videos.go @@ -0,0 +1,216 @@ +package glance + +import ( + "context" + "fmt" + "html/template" + "log/slog" + "net/http" + "net/url" + "sort" + "strings" + "time" +) + +const videosWidgetPlaylistPrefix = "playlist:" + +var ( + videosWidgetTemplate = mustParseTemplate("videos.html", "widget-base.html", "video-card-contents.html") + videosWidgetGridTemplate = mustParseTemplate("videos-grid.html", "widget-base.html", "video-card-contents.html") + videosWidgetVerticalListTemplate = mustParseTemplate("videos-vertical-list.html", "widget-base.html") +) + +type videosWidget struct { + widgetBase `yaml:",inline"` + Videos videoList `yaml:"-"` + VideoUrlTemplate string `yaml:"video-url-template"` + Style string `yaml:"style"` + CollapseAfter int `yaml:"collapse-after"` + CollapseAfterRows int `yaml:"collapse-after-rows"` + Channels []string `yaml:"channels"` + Playlists []string `yaml:"playlists"` + Limit int `yaml:"limit"` + IncludeShorts bool `yaml:"include-shorts"` +} + +func (widget *videosWidget) initialize() error { + widget.withTitle("Videos").withCacheDuration(time.Hour) + + if widget.Limit <= 0 { + widget.Limit = 25 + } + + if widget.CollapseAfterRows == 0 || widget.CollapseAfterRows < -1 { + widget.CollapseAfterRows = 4 + } + + if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { + widget.CollapseAfter = 7 + } + + // A bit cheeky, but from a user's perspective it makes more sense when channels and + // playlists are separate things rather than specifying a list of channels and some of + // them awkwardly have a "playlist:" prefix + if len(widget.Playlists) > 0 { + initialLen := len(widget.Channels) + widget.Channels = append(widget.Channels, make([]string, len(widget.Playlists))...) + + for i := range widget.Playlists { + widget.Channels[initialLen+i] = videosWidgetPlaylistPrefix + widget.Playlists[i] + } + } + + return nil +} + +func (widget *videosWidget) update(ctx context.Context) { + videos, err := fetchYoutubeChannelUploads(widget.Channels, widget.VideoUrlTemplate, widget.IncludeShorts) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + if len(videos) > widget.Limit { + videos = videos[:widget.Limit] + } + + widget.Videos = videos +} + +func (widget *videosWidget) Render() template.HTML { + var template *template.Template + + switch widget.Style { + case "grid-cards": + template = videosWidgetGridTemplate + case "vertical-list": + template = videosWidgetVerticalListTemplate + default: + template = videosWidgetTemplate + } + + return widget.renderTemplate(widget, template) +} + +type youtubeFeedResponseXml struct { + Channel string `xml:"author>name"` + ChannelLink string `xml:"author>uri"` + Videos []struct { + Title string `xml:"title"` + Published string `xml:"published"` + Link struct { + Href string `xml:"href,attr"` + } `xml:"link"` + + Group struct { + Thumbnail struct { + Url string `xml:"url,attr"` + } `xml:"http://search.yahoo.com/mrss/ thumbnail"` + } `xml:"http://search.yahoo.com/mrss/ group"` + } `xml:"entry"` +} + +func parseYoutubeFeedTime(t string) time.Time { + parsedTime, err := time.Parse("2006-01-02T15:04:05-07:00", t) + if err != nil { + return time.Now() + } + + return parsedTime +} + +type video struct { + ThumbnailUrl string + Title string + Url string + Author string + AuthorUrl string + TimePosted time.Time +} + +type videoList []video + +func (v videoList) sortByNewest() videoList { + sort.Slice(v, func(i, j int) bool { + return v[i].TimePosted.After(v[j].TimePosted) + }) + + return v +} + +func fetchYoutubeChannelUploads(channelOrPlaylistIDs []string, videoUrlTemplate string, includeShorts bool) (videoList, error) { + requests := make([]*http.Request, 0, len(channelOrPlaylistIDs)) + + for i := range channelOrPlaylistIDs { + var feedUrl string + if strings.HasPrefix(channelOrPlaylistIDs[i], videosWidgetPlaylistPrefix) { + feedUrl = "https://www.youtube.com/feeds/videos.xml?playlist_id=" + + strings.TrimPrefix(channelOrPlaylistIDs[i], videosWidgetPlaylistPrefix) + } else if !includeShorts && strings.HasPrefix(channelOrPlaylistIDs[i], "UC") { + playlistId := strings.Replace(channelOrPlaylistIDs[i], "UC", "UULF", 1) + feedUrl = "https://www.youtube.com/feeds/videos.xml?playlist_id=" + playlistId + } else { + feedUrl = "https://www.youtube.com/feeds/videos.xml?channel_id=" + channelOrPlaylistIDs[i] + } + + request, _ := http.NewRequest("GET", feedUrl, nil) + requests = append(requests, request) + } + + job := newJob(decodeXmlFromRequestTask[youtubeFeedResponseXml](defaultHTTPClient), requests).withWorkers(30) + responses, errs, err := workerPoolDo(job) + if err != nil { + return nil, fmt.Errorf("%w: %v", errNoContent, err) + } + + videos := make(videoList, 0, len(channelOrPlaylistIDs)*15) + var failed int + + for i := range responses { + if errs[i] != nil { + failed++ + slog.Error("Failed to fetch youtube feed", "channel", channelOrPlaylistIDs[i], "error", errs[i]) + continue + } + + response := responses[i] + + for j := range response.Videos { + v := &response.Videos[j] + var videoUrl string + + if videoUrlTemplate == "" { + videoUrl = v.Link.Href + } else { + parsedUrl, err := url.Parse(v.Link.Href) + + if err == nil { + videoUrl = strings.ReplaceAll(videoUrlTemplate, "{VIDEO-ID}", parsedUrl.Query().Get("v")) + } else { + videoUrl = "#" + } + } + + videos = append(videos, video{ + ThumbnailUrl: v.Group.Thumbnail.Url, + Title: v.Title, + Url: videoUrl, + Author: response.Channel, + AuthorUrl: response.ChannelLink + "/videos", + TimePosted: parseYoutubeFeedTime(v.Published), + }) + } + } + + if len(videos) == 0 { + return nil, errNoContent + } + + videos.sortByNewest() + + if failed > 0 { + return videos, fmt.Errorf("%w: missing videos from %d channels", errPartialContent, failed) + } + + return videos, nil +} diff --git a/internal/glance/widget-weather.go b/internal/glance/widget-weather.go new file mode 100644 index 0000000..9d53cd6 --- /dev/null +++ b/internal/glance/widget-weather.go @@ -0,0 +1,326 @@ +package glance + +import ( + "context" + "errors" + "fmt" + "html/template" + "math" + "net/http" + "net/url" + "slices" + "strings" + "time" + + _ "time/tzdata" +) + +var weatherWidgetTemplate = mustParseTemplate("weather.html", "widget-base.html") + +type weatherWidget struct { + widgetBase `yaml:",inline"` + Location string `yaml:"location"` + ShowAreaName bool `yaml:"show-area-name"` + HideLocation bool `yaml:"hide-location"` + HourFormat string `yaml:"hour-format"` + Units string `yaml:"units"` + Place *openMeteoPlaceResponseJson `yaml:"-"` + Weather *weather `yaml:"-"` + TimeLabels [12]string `yaml:"-"` +} + +var timeLabels12h = [12]string{"2am", "4am", "6am", "8am", "10am", "12pm", "2pm", "4pm", "6pm", "8pm", "10pm", "12am"} +var timeLabels24h = [12]string{"02:00", "04:00", "06:00", "08:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00", "22:00", "00:00"} + +func (widget *weatherWidget) initialize() error { + widget.withTitle("Weather").withCacheOnTheHour() + + if widget.Location == "" { + return fmt.Errorf("location is required") + } + + if widget.HourFormat == "" || widget.HourFormat == "12h" { + widget.TimeLabels = timeLabels12h + } else if widget.HourFormat == "24h" { + widget.TimeLabels = timeLabels24h + } else { + return errors.New("hour-format must be either 12h or 24h") + } + + if widget.Units == "" { + widget.Units = "metric" + } else if widget.Units != "metric" && widget.Units != "imperial" { + return errors.New("units must be either metric or imperial") + } + + return nil +} + +func (widget *weatherWidget) update(ctx context.Context) { + if widget.Place == nil { + place, err := fetchOpenMeteoPlaceFromName(widget.Location) + if err != nil { + widget.withError(err).scheduleEarlyUpdate() + return + } + + widget.Place = place + } + + weather, err := fetchWeatherForOpenMeteoPlace(widget.Place, widget.Units) + + if !widget.canContinueUpdateAfterHandlingErr(err) { + return + } + + widget.Weather = weather +} + +func (widget *weatherWidget) Render() template.HTML { + return widget.renderTemplate(widget, weatherWidgetTemplate) +} + +type weather struct { + Temperature int + ApparentTemperature int + WeatherCode int + CurrentColumn int + SunriseColumn int + SunsetColumn int + Columns []weatherColumn +} + +func (w *weather) WeatherCodeAsString() string { + if weatherCode, ok := weatherCodeTable[w.WeatherCode]; ok { + return weatherCode + } + + return "" +} + +type openMeteoPlacesResponseJson struct { + Results []openMeteoPlaceResponseJson +} + +type openMeteoPlaceResponseJson struct { + Name string + Area string `json:"admin1"` + Latitude float64 + Longitude float64 + Timezone string + Country string + location *time.Location +} + +type openMeteoWeatherResponseJson struct { + Daily struct { + Sunrise []int64 `json:"sunrise"` + Sunset []int64 `json:"sunset"` + } `json:"daily"` + + Hourly struct { + Temperature []float64 `json:"temperature_2m"` + PrecipitationProbability []int `json:"precipitation_probability"` + } `json:"hourly"` + + Current struct { + Temperature float64 `json:"temperature_2m"` + ApparentTemperature float64 `json:"apparent_temperature"` + WeatherCode int `json:"weather_code"` + } `json:"current"` +} + +type weatherColumn struct { + Temperature int + Scale float64 + HasPrecipitation bool +} + +var commonCountryAbbreviations = map[string]string{ + "US": "United States", + "USA": "United States", + "UK": "United Kingdom", +} + +func expandCountryAbbreviations(name string) string { + if expanded, ok := commonCountryAbbreviations[strings.TrimSpace(name)]; ok { + return expanded + } + + return name +} + +// Separates the location that Open Meteo accepts from the administrative area +// which can then be used to filter to the correct place after the list of places +// has been retrieved. Also expands abbreviations since Open Meteo does not accept +// country names like "US", "USA" and "UK" +func parsePlaceName(name string) (string, string) { + parts := strings.Split(name, ",") + + if len(parts) == 1 { + return name, "" + } + + if len(parts) == 2 { + return parts[0] + ", " + expandCountryAbbreviations(parts[1]), "" + } + + return parts[0] + ", " + expandCountryAbbreviations(parts[2]), strings.TrimSpace(parts[1]) +} + +func fetchOpenMeteoPlaceFromName(location string) (*openMeteoPlaceResponseJson, error) { + location, area := parsePlaceName(location) + requestUrl := fmt.Sprintf("https://geocoding-api.open-meteo.com/v1/search?name=%s&count=10&language=en&format=json", url.QueryEscape(location)) + request, _ := http.NewRequest("GET", requestUrl, nil) + responseJson, err := decodeJsonFromRequest[openMeteoPlacesResponseJson](defaultHTTPClient, request) + if err != nil { + return nil, fmt.Errorf("fetching places data: %v", err) + } + + if len(responseJson.Results) == 0 { + return nil, fmt.Errorf("no places found for %s", location) + } + + var place *openMeteoPlaceResponseJson + + if area != "" { + area = strings.ToLower(area) + + for i := range responseJson.Results { + if strings.ToLower(responseJson.Results[i].Area) == area { + place = &responseJson.Results[i] + break + } + } + + if place == nil { + return nil, fmt.Errorf("no place found for %s in %s", location, area) + } + } else { + place = &responseJson.Results[0] + } + + loc, err := time.LoadLocation(place.Timezone) + if err != nil { + return nil, fmt.Errorf("loading location: %v", err) + } + + place.location = loc + + return place, nil +} + +func fetchWeatherForOpenMeteoPlace(place *openMeteoPlaceResponseJson, units string) (*weather, error) { + query := url.Values{} + var temperatureUnit string + + if units == "imperial" { + temperatureUnit = "fahrenheit" + } else { + temperatureUnit = "celsius" + } + + query.Add("latitude", fmt.Sprintf("%f", place.Latitude)) + query.Add("longitude", fmt.Sprintf("%f", place.Longitude)) + query.Add("timeformat", "unixtime") + query.Add("timezone", place.Timezone) + query.Add("forecast_days", "1") + query.Add("current", "temperature_2m,apparent_temperature,weather_code") + query.Add("hourly", "temperature_2m,precipitation_probability") + query.Add("daily", "sunrise,sunset") + query.Add("temperature_unit", temperatureUnit) + + requestUrl := "https://api.open-meteo.com/v1/forecast?" + query.Encode() + request, _ := http.NewRequest("GET", requestUrl, nil) + responseJson, err := decodeJsonFromRequest[openMeteoWeatherResponseJson](defaultHTTPClient, request) + if err != nil { + return nil, fmt.Errorf("%w: %v", errNoContent, err) + } + + now := time.Now().In(place.location) + bars := make([]weatherColumn, 0, 24) + currentBar := now.Hour() / 2 + sunriseBar := (time.Unix(int64(responseJson.Daily.Sunrise[0]), 0).In(place.location).Hour()) / 2 + sunsetBar := (time.Unix(int64(responseJson.Daily.Sunset[0]), 0).In(place.location).Hour() - 1) / 2 + + if sunsetBar < 0 { + sunsetBar = 0 + } + + if len(responseJson.Hourly.Temperature) == 24 { + temperatures := make([]int, 12) + precipitations := make([]bool, 12) + + t := responseJson.Hourly.Temperature + p := responseJson.Hourly.PrecipitationProbability + + for i := 0; i < 24; i += 2 { + if i/2 == currentBar { + temperatures[i/2] = int(responseJson.Current.Temperature) + } else { + temperatures[i/2] = int(math.Round((t[i] + t[i+1]) / 2)) + } + + precipitations[i/2] = (p[i]+p[i+1])/2 > 75 + } + + minT := slices.Min(temperatures) + maxT := slices.Max(temperatures) + + temperaturesRange := float64(maxT - minT) + + for i := 0; i < 12; i++ { + bars = append(bars, weatherColumn{ + Temperature: temperatures[i], + HasPrecipitation: precipitations[i], + }) + + if temperaturesRange > 0 { + bars[i].Scale = float64(temperatures[i]-minT) / temperaturesRange + } else { + bars[i].Scale = 1 + } + } + } + + return &weather{ + Temperature: int(responseJson.Current.Temperature), + ApparentTemperature: int(responseJson.Current.ApparentTemperature), + WeatherCode: responseJson.Current.WeatherCode, + CurrentColumn: currentBar, + SunriseColumn: sunriseBar, + SunsetColumn: sunsetBar, + Columns: bars, + }, nil +} + +var weatherCodeTable = map[int]string{ + 0: "Clear Sky", + 1: "Mainly Clear", + 2: "Partly Cloudy", + 3: "Overcast", + 45: "Fog", + 48: "Rime Fog", + 51: "Drizzle", + 53: "Drizzle", + 55: "Drizzle", + 56: "Drizzle", + 57: "Drizzle", + 61: "Rain", + 63: "Moderate Rain", + 65: "Heavy Rain", + 66: "Freezing Rain", + 67: "Freezing Rain", + 71: "Snow", + 73: "Moderate Snow", + 75: "Heavy Snow", + 77: "Snow Grains", + 80: "Rain", + 81: "Moderate Rain", + 82: "Heavy Rain", + 85: "Snow", + 86: "Snow", + 95: "Thunderstorm", + 96: "Thunderstorm", + 99: "Thunderstorm", +} diff --git a/internal/widget/widget.go b/internal/glance/widget.go similarity index 57% rename from internal/widget/widget.go rename to internal/glance/widget.go index 367d822..ab41c79 100644 --- a/internal/widget/widget.go +++ b/internal/glance/widget.go @@ -1,4 +1,4 @@ -package widget +package glance import ( "bytes" @@ -8,49 +8,85 @@ import ( "html/template" "log/slog" "math" + "net/http" + "sync/atomic" "time" - "github.com/glanceapp/glance/internal/feed" - "gopkg.in/yaml.v3" ) -func New(widgetType string) (Widget, error) { +var widgetIDCounter atomic.Uint64 + +func newWidget(widgetType string) (widget, error) { + var w widget + switch widgetType { case "calendar": - return &Calendar{}, nil + w = &calendarWidget{} + case "calendar-legacy": + w = &oldCalendarWidget{} + case "clock": + w = &clockWidget{} case "weather": - return &Weather{}, nil + w = &weatherWidget{} case "bookmarks": - return &Bookmarks{}, nil + w = &bookmarksWidget{} case "iframe": - return &IFrame{}, nil + w = &iframeWidget{} + case "html": + w = &htmlWidget{} case "hacker-news": - return &HackerNews{}, nil + w = &hackerNewsWidget{} case "releases": - return &Releases{}, nil + w = &releasesWidget{} case "videos": - return &Videos{}, nil - case "stocks": - return &Stocks{}, nil + w = &videosWidget{} + case "markets", "stocks": + w = &marketsWidget{} case "reddit": - return &Reddit{}, nil + w = &redditWidget{} case "rss": - return &RSS{}, nil + w = &rssWidget{} case "monitor": - return &Monitor{}, nil + w = &monitorWidget{} case "twitch-top-games": - return &TwitchGames{}, nil + w = &twitchGamesWidget{} case "twitch-channels": - return &TwitchChannels{}, nil + w = &twitchChannelsWidget{} + case "lobsters": + w = &lobstersWidget{} + case "change-detection": + w = &changeDetectionWidget{} + case "repository": + w = &repositoryWidget{} + case "search": + w = &searchWidget{} + case "extension": + w = &extensionWidget{} + case "group": + w = &groupWidget{} + case "dns-stats": + w = &dnsStatsWidget{} + case "split-column": + w = &splitColumnWidget{} + case "custom-api": + w = &customAPIWidget{} + case "docker-containers": + w = &dockerContainersWidget{} + case "server-stats": + w = &serverStatsWidget{} default: return nil, fmt.Errorf("unknown widget type: %s", widgetType) } + + w.setID(widgetIDCounter.Add(1)) + + return w, nil } -type Widgets []Widget +type widgets []widget -func (w *Widgets) UnmarshalYAML(node *yaml.Node) error { +func (w *widgets) UnmarshalYAML(node *yaml.Node) error { var nodes []yaml.Node if err := node.Decode(&nodes); err != nil { @@ -66,8 +102,7 @@ func (w *Widgets) UnmarshalYAML(node *yaml.Node) error { return err } - widget, err := New(meta.Type) - + widget, err := newWidget(meta.Type) if err != nil { return err } @@ -76,22 +111,25 @@ func (w *Widgets) UnmarshalYAML(node *yaml.Node) error { return err } - if err = widget.Initialize(); err != nil { - return err - } - *w = append(*w, widget) } return nil } -type Widget interface { - Initialize() error - RequiresUpdate(*time.Time) bool - Update(context.Context) +type widget interface { + // These need to be exported because they get called in templates Render() template.HTML GetType() string + GetID() uint64 + + initialize() error + requiresUpdate(*time.Time) bool + setProviders(*widgetProviders) + update(context.Context) + setID(uint64) + handleRequest(w http.ResponseWriter, r *http.Request) + setHideHeader(bool) } type cacheType int @@ -103,20 +141,30 @@ const ( ) type widgetBase struct { - Type string `yaml:"type"` - Title string `yaml:"title"` - CustomCacheDuration DurationField `yaml:"cache"` - ContentAvailable bool `yaml:"-"` - Error error `yaml:"-"` - Notice error `yaml:"-"` - templateBuffer bytes.Buffer `yaml:"-"` - cacheDuration time.Duration `yaml:"-"` - cacheType cacheType `yaml:"-"` - nextUpdate time.Time `yaml:"-"` - updateRetriedTimes int `yaml:"-"` + ID uint64 `yaml:"-"` + Providers *widgetProviders `yaml:"-"` + Type string `yaml:"type"` + Title string `yaml:"title"` + TitleURL string `yaml:"title-url"` + CSSClass string `yaml:"css-class"` + CustomCacheDuration durationField `yaml:"cache"` + ContentAvailable bool `yaml:"-"` + WIP bool `yaml:"-"` + Error error `yaml:"-"` + Notice error `yaml:"-"` + templateBuffer bytes.Buffer `yaml:"-"` + cacheDuration time.Duration `yaml:"-"` + cacheType cacheType `yaml:"-"` + nextUpdate time.Time `yaml:"-"` + updateRetriedTimes int `yaml:"-"` + HideHeader bool `yaml:"-"` } -func (w *widgetBase) RequiresUpdate(now *time.Time) bool { +type widgetProviders struct { + assetResolver func(string) string +} + +func (w *widgetBase) requiresUpdate(now *time.Time) bool { if w.cacheType == cacheTypeInfinite { return false } @@ -128,23 +176,46 @@ func (w *widgetBase) RequiresUpdate(now *time.Time) bool { return now.After(w.nextUpdate) } -func (w *widgetBase) Update(ctx context.Context) { +func (w *widgetBase) IsWIP() bool { + return w.WIP +} +func (w *widgetBase) update(ctx context.Context) { + +} + +func (w *widgetBase) GetID() uint64 { + return w.ID +} + +func (w *widgetBase) setID(id uint64) { + w.ID = id +} + +func (w *widgetBase) setHideHeader(value bool) { + w.HideHeader = value +} + +func (widget *widgetBase) handleRequest(w http.ResponseWriter, r *http.Request) { + http.Error(w, "not implemented", http.StatusNotImplemented) } func (w *widgetBase) GetType() string { return w.Type } -func (w *widgetBase) render(data any, t *template.Template) template.HTML { +func (w *widgetBase) setProviders(providers *widgetProviders) { + w.Providers = providers +} + +func (w *widgetBase) renderTemplate(data any, t *template.Template) template.HTML { w.templateBuffer.Reset() err := t.Execute(&w.templateBuffer, data) - if err != nil { w.ContentAvailable = false w.Error = err - slog.Error("failed to render template", "error", err) + slog.Error("Failed to render template", "error", err) // need to immediately re-render with the error, // otherwise risk breaking the page since the widget @@ -153,7 +224,7 @@ func (w *widgetBase) render(data any, t *template.Template) template.HTML { err2 := t.Execute(&w.templateBuffer, data) if err2 != nil { - slog.Error("failed to render error within widget", "error", err2, "initial_error", err) + slog.Error("Failed to render error within widget", "error", err2, "initial_error", err) w.templateBuffer.Reset() // TODO: add some kind of a generic widget error template when the widget // failed to render, and we also failed to re-render the widget with the error @@ -171,6 +242,14 @@ func (w *widgetBase) withTitle(title string) *widgetBase { return w } +func (w *widgetBase) withTitleURL(titleURL string) *widgetBase { + if w.TitleURL == "" { + w.TitleURL = titleURL + } + + return w +} + func (w *widgetBase) withCacheDuration(duration time.Duration) *widgetBase { w.cacheType = cacheTypeDuration @@ -222,7 +301,7 @@ func (w *widgetBase) canContinueUpdateAfterHandlingErr(err error) bool { if err != nil { w.scheduleEarlyUpdate() - if !errors.Is(err, feed.ErrPartialContent) { + if !errors.Is(err, errPartialContent) { w.withError(err) w.withNotice(nil) return false diff --git a/internal/widget/bookmarks.go b/internal/widget/bookmarks.go deleted file mode 100644 index a3e3d28..0000000 --- a/internal/widget/bookmarks.go +++ /dev/null @@ -1,31 +0,0 @@ -package widget - -import ( - "html/template" - - "github.com/glanceapp/glance/internal/assets" -) - -type Bookmarks struct { - widgetBase `yaml:",inline"` - cachedHTML template.HTML `yaml:"-"` - Groups []struct { - Title string `yaml:"title"` - Color *HSLColorField `yaml:"color"` - Links []struct { - Title string `yaml:"title"` - URL string `yaml:"url"` - } `yaml:"links"` - } `yaml:"groups"` -} - -func (widget *Bookmarks) Initialize() error { - widget.withTitle("Bookmarks").withError(nil) - widget.cachedHTML = widget.render(widget, assets.BookmarksTemplate) - - return nil -} - -func (widget *Bookmarks) Render() template.HTML { - return widget.cachedHTML -} diff --git a/internal/widget/calendar.go b/internal/widget/calendar.go deleted file mode 100644 index a126353..0000000 --- a/internal/widget/calendar.go +++ /dev/null @@ -1,30 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Calendar struct { - widgetBase `yaml:",inline"` - Calendar *feed.Calendar -} - -func (widget *Calendar) Initialize() error { - widget.withTitle("Calendar").withCacheOnTheHour() - - return nil -} - -func (widget *Calendar) Update(ctx context.Context) { - widget.Calendar = feed.NewCalendar(time.Now()) - widget.withError(nil).scheduleNextUpdate() -} - -func (widget *Calendar) Render() template.HTML { - return widget.render(widget, assets.CalendarTemplate) -} diff --git a/internal/widget/fields.go b/internal/widget/fields.go deleted file mode 100644 index eecb916..0000000 --- a/internal/widget/fields.go +++ /dev/null @@ -1,152 +0,0 @@ -package widget - -import ( - "fmt" - "html/template" - "os" - "regexp" - "strconv" - "time" - - "gopkg.in/yaml.v3" -) - -var HSLColorPattern = regexp.MustCompile(`^(?:hsla?\()?(\d{1,3})(?: |,)+(\d{1,3})%?(?: |,)+(\d{1,3})%?\)?$`) -var EnvFieldPattern = regexp.MustCompile(`^\${([A-Z_]+)}$`) - -const ( - HSLHueMax = 360 - HSLSaturationMax = 100 - HSLLightnessMax = 100 -) - -type HSLColorField struct { - Hue uint16 - Saturation uint8 - Lightness uint8 -} - -func (c *HSLColorField) String() string { - return fmt.Sprintf("hsl(%d, %d%%, %d%%)", c.Hue, c.Saturation, c.Lightness) -} - -func (c *HSLColorField) AsCSSValue() template.CSS { - return template.CSS(c.String()) -} - -func (c *HSLColorField) UnmarshalYAML(node *yaml.Node) error { - var value string - - if err := node.Decode(&value); err != nil { - return err - } - - matches := HSLColorPattern.FindStringSubmatch(value) - - if len(matches) != 4 { - return fmt.Errorf("invalid HSL color format: %s", value) - } - - hue, err := strconv.ParseUint(matches[1], 10, 16) - - if err != nil { - return err - } - - if hue > HSLHueMax { - return fmt.Errorf("HSL hue must be between 0 and %d", HSLHueMax) - } - - saturation, err := strconv.ParseUint(matches[2], 10, 8) - - if err != nil { - return err - } - - if saturation > HSLSaturationMax { - return fmt.Errorf("HSL saturation must be between 0 and %d", HSLSaturationMax) - } - - lightness, err := strconv.ParseUint(matches[3], 10, 8) - - if err != nil { - return err - } - - if lightness > HSLLightnessMax { - return fmt.Errorf("HSL lightness must be between 0 and %d", HSLLightnessMax) - } - - c.Hue = uint16(hue) - c.Saturation = uint8(saturation) - c.Lightness = uint8(lightness) - - return nil -} - -var DurationPattern = regexp.MustCompile(`^(\d+)(s|m|h|d)$`) - -type DurationField time.Duration - -func (d *DurationField) UnmarshalYAML(node *yaml.Node) error { - var value string - - if err := node.Decode(&value); err != nil { - return err - } - - matches := DurationPattern.FindStringSubmatch(value) - - if len(matches) != 3 { - return fmt.Errorf("invalid duration format: %s", value) - } - - duration, err := strconv.Atoi(matches[1]) - - if err != nil { - return err - } - - switch matches[2] { - case "s": - *d = DurationField(time.Duration(duration) * time.Second) - case "m": - *d = DurationField(time.Duration(duration) * time.Minute) - case "h": - *d = DurationField(time.Duration(duration) * time.Hour) - case "d": - *d = DurationField(time.Duration(duration) * 24 * time.Hour) - } - - return nil -} - -type OptionalEnvString string - -func (f *OptionalEnvString) UnmarshalYAML(node *yaml.Node) error { - var value string - - err := node.Decode(&value) - - if err != nil { - return err - } - - matches := EnvFieldPattern.FindStringSubmatch(value) - - if len(matches) != 2 { - *f = OptionalEnvString(value) - - return nil - } - - value, found := os.LookupEnv(matches[1]) - - if !found { - return fmt.Errorf("environment variable %s not found", matches[1]) - } - - *f = OptionalEnvString(value) - - return nil -} diff --git a/internal/widget/hacker-news.go b/internal/widget/hacker-news.go deleted file mode 100644 index 9870e74..0000000 --- a/internal/widget/hacker-news.go +++ /dev/null @@ -1,52 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type HackerNews struct { - widgetBase `yaml:",inline"` - Posts feed.ForumPosts `yaml:"-"` - Limit int `yaml:"limit"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *HackerNews) Initialize() error { - widget.withTitle("Hacker News").withCacheDuration(30 * time.Minute) - - if widget.Limit <= 0 { - widget.Limit = 15 - } - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - return nil -} - -func (widget *HackerNews) Update(ctx context.Context) { - posts, err := feed.FetchHackerNewsTopPosts(40) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - posts.CalculateEngagement() - posts.SortByEngagement() - - if widget.Limit < len(posts) { - posts = posts[:widget.Limit] - } - - widget.Posts = posts -} - -func (widget *HackerNews) Render() template.HTML { - return widget.render(widget, assets.ForumPostsTemplate) -} diff --git a/internal/widget/monitor.go b/internal/widget/monitor.go deleted file mode 100644 index 7c964e5..0000000 --- a/internal/widget/monitor.go +++ /dev/null @@ -1,100 +0,0 @@ -package widget - -import ( - "context" - "fmt" - "html/template" - "net/http" - "strconv" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -func statusCodeToText(status int) string { - if status == 200 { - return "OK" - } - if status == 404 { - return "Not Found" - } - if status == 403 { - return "Forbidden" - } - if status == 401 { - return "Unauthorized" - } - if status >= 400 { - return "Client Error" - } - if status >= 500 { - return "Server Error" - } - - return strconv.Itoa(status) -} - -func statusCodeToStyle(status int) string { - if status == 200 { - return "good" - } - - return "bad" -} - -type Monitor struct { - widgetBase `yaml:",inline"` - Sites []struct { - Title string `yaml:"title"` - Url string `yaml:"url"` - IconUrl string `yaml:"icon"` - Status *feed.SiteStatus `yaml:"-"` - StatusText string `yaml:"-"` - StatusStyle string `yaml:"-"` - } `yaml:"sites"` -} - -func (widget *Monitor) Initialize() error { - widget.withTitle("Monitor").withCacheDuration(5 * time.Minute) - - return nil -} - -func (widget *Monitor) Update(ctx context.Context) { - requests := make([]*http.Request, len(widget.Sites)) - - for i := range widget.Sites { - request, err := http.NewRequest("GET", widget.Sites[i].Url, nil) - - if err != nil { - message := fmt.Errorf("failed to create http request for %s: %s", widget.Sites[i].Url, err) - widget.withNotice(message) - continue - } - - requests[i] = request - } - - statuses, err := feed.FetchStatusesForRequests(requests) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - for i := range widget.Sites { - site := &widget.Sites[i] - status := &statuses[i] - - site.Status = status - - if !status.TimedOut { - site.StatusText = statusCodeToText(status.Code) - site.StatusStyle = statusCodeToStyle(status.Code) - } - } -} - -func (widget *Monitor) Render() template.HTML { - return widget.render(widget, assets.MonitorTemplate) -} diff --git a/internal/widget/reddit.go b/internal/widget/reddit.go deleted file mode 100644 index 3287fcf..0000000 --- a/internal/widget/reddit.go +++ /dev/null @@ -1,66 +0,0 @@ -package widget - -import ( - "context" - "errors" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Reddit struct { - widgetBase `yaml:",inline"` - Posts feed.ForumPosts `yaml:"-"` - Subreddit string `yaml:"subreddit"` - Style string `yaml:"style"` - Limit int `yaml:"limit"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *Reddit) Initialize() error { - if widget.Subreddit == "" { - return errors.New("no subreddit specified") - } - - if widget.Limit <= 0 { - widget.Limit = 15 - } - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - widget.withTitle("/r/" + widget.Subreddit).withCacheDuration(30 * time.Minute) - - return nil -} - -func (widget *Reddit) Update(ctx context.Context) { - posts, err := feed.FetchSubredditPosts(widget.Subreddit) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - if len(posts) > widget.Limit { - posts = posts[:widget.Limit] - } - - posts.SortByEngagement() - widget.Posts = posts -} - -func (widget *Reddit) Render() template.HTML { - if widget.Style == "horizontal-cards" { - return widget.render(widget, assets.RedditCardsHorizontalTemplate) - } - - if widget.Style == "vertical-cards" { - return widget.render(widget, assets.RedditCardsVerticalTemplate) - } - - return widget.render(widget, assets.ForumPostsTemplate) - -} diff --git a/internal/widget/releases.go b/internal/widget/releases.go deleted file mode 100644 index 77fe103..0000000 --- a/internal/widget/releases.go +++ /dev/null @@ -1,51 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Releases struct { - widgetBase `yaml:",inline"` - Releases feed.AppReleases `yaml:"-"` - Repositories []string `yaml:"repositories"` - Token OptionalEnvString `yaml:"token"` - Limit int `yaml:"limit"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *Releases) Initialize() error { - widget.withTitle("Releases").withCacheDuration(2 * time.Hour) - - if widget.Limit <= 0 { - widget.Limit = 10 - } - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - return nil -} - -func (widget *Releases) Update(ctx context.Context) { - releases, err := feed.FetchLatestReleasesFromGithub(widget.Repositories, string(widget.Token)) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - if len(releases) > widget.Limit { - releases = releases[:widget.Limit] - } - - widget.Releases = releases -} - -func (widget *Releases) Render() template.HTML { - return widget.render(widget, assets.ReleasesTemplate) -} diff --git a/internal/widget/rss.go b/internal/widget/rss.go deleted file mode 100644 index 81bd3ae..0000000 --- a/internal/widget/rss.go +++ /dev/null @@ -1,55 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type RSS struct { - widgetBase `yaml:",inline"` - FeedRequests []feed.RSSFeedRequest `yaml:"feeds"` - Style string `yaml:"style"` - Items feed.RSSFeedItems `yaml:"-"` - Limit int `yaml:"limit"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *RSS) Initialize() error { - widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour) - - if widget.Limit <= 0 { - widget.Limit = 25 - } - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - return nil -} - -func (widget *RSS) Update(ctx context.Context) { - items, err := feed.GetItemsFromRSSFeeds(widget.FeedRequests) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - if len(items) > widget.Limit { - items = items[:widget.Limit] - } - - widget.Items = items -} - -func (widget *RSS) Render() template.HTML { - if widget.Style == "horizontal-cards" { - return widget.render(widget, assets.RSSCardsTemplate) - } - - return widget.render(widget, assets.RSSListTemplate) -} diff --git a/internal/widget/stocks.go b/internal/widget/stocks.go deleted file mode 100644 index afba0e4..0000000 --- a/internal/widget/stocks.go +++ /dev/null @@ -1,37 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Stocks struct { - widgetBase `yaml:",inline"` - Stocks feed.Stocks `yaml:"-"` - Tickers []feed.StockRequest `yaml:"stocks"` -} - -func (widget *Stocks) Initialize() error { - widget.withTitle("Stocks").withCacheDuration(time.Hour) - - return nil -} - -func (widget *Stocks) Update(ctx context.Context) { - stocks, err := feed.FetchStocksDataFromYahoo(widget.Tickers) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - stocks.SortByAbsChange() - widget.Stocks = stocks -} - -func (widget *Stocks) Render() template.HTML { - return widget.render(widget, assets.StocksTemplate) -} diff --git a/internal/widget/twitch-channels.go b/internal/widget/twitch-channels.go deleted file mode 100644 index 3f36669..0000000 --- a/internal/widget/twitch-channels.go +++ /dev/null @@ -1,42 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type TwitchChannels struct { - widgetBase `yaml:",inline"` - ChannelsRequest []string `yaml:"channels"` - Channels []feed.TwitchChannel `yaml:"-"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *TwitchChannels) Initialize() error { - widget.withTitle("Twitch Channels").withCacheDuration(time.Minute * 10) - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - return nil -} - -func (widget *TwitchChannels) Update(ctx context.Context) { - channels, err := feed.FetchChannelsFromTwitch(widget.ChannelsRequest) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - channels.SortByViewers() - widget.Channels = channels -} - -func (widget *TwitchChannels) Render() template.HTML { - return widget.render(widget, assets.TwitchChannelsTemplate) -} diff --git a/internal/widget/twitch-top-games.go b/internal/widget/twitch-top-games.go deleted file mode 100644 index 812c3c6..0000000 --- a/internal/widget/twitch-top-games.go +++ /dev/null @@ -1,46 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type TwitchGames struct { - widgetBase `yaml:",inline"` - Categories []feed.TwitchCategory `yaml:"-"` - Exclude []string `yaml:"exclude"` - Limit int `yaml:"limit"` - CollapseAfter int `yaml:"collapse-after"` -} - -func (widget *TwitchGames) Initialize() error { - widget.withTitle("Top games on Twitch").withCacheDuration(time.Minute * 10) - - if widget.Limit <= 0 { - widget.Limit = 10 - } - - if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 { - widget.CollapseAfter = 5 - } - - return nil -} - -func (widget *TwitchGames) Update(ctx context.Context) { - categories, err := feed.FetchTopGamesFromTwitch(widget.Exclude, widget.Limit) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - widget.Categories = categories -} - -func (widget *TwitchGames) Render() template.HTML { - return widget.render(widget, assets.TwitchGamesListTemplate) -} diff --git a/internal/widget/videos.go b/internal/widget/videos.go deleted file mode 100644 index 2652768..0000000 --- a/internal/widget/videos.go +++ /dev/null @@ -1,45 +0,0 @@ -package widget - -import ( - "context" - "html/template" - "time" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Videos struct { - widgetBase `yaml:",inline"` - Videos feed.Videos `yaml:"-"` - Channels []string `yaml:"channels"` - Limit int `yaml:"limit"` -} - -func (widget *Videos) Initialize() error { - widget.withTitle("Videos").withCacheDuration(time.Hour) - - if widget.Limit <= 0 { - widget.Limit = 25 - } - - return nil -} - -func (widget *Videos) Update(ctx context.Context) { - videos, err := feed.FetchYoutubeChannelUploads(widget.Channels) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - if len(videos) > widget.Limit { - videos = videos[:widget.Limit] - } - - widget.Videos = videos -} - -func (widget *Videos) Render() template.HTML { - return widget.render(widget, assets.VideosTemplate) -} diff --git a/internal/widget/weather.go b/internal/widget/weather.go deleted file mode 100644 index a59b9b9..0000000 --- a/internal/widget/weather.go +++ /dev/null @@ -1,57 +0,0 @@ -package widget - -import ( - "context" - "fmt" - "html/template" - - "github.com/glanceapp/glance/internal/assets" - "github.com/glanceapp/glance/internal/feed" -) - -type Weather struct { - widgetBase `yaml:",inline"` - Location string `yaml:"location"` - HideLocation bool `yaml:"hide-location"` - Units string `yaml:"units"` - Place *feed.PlaceJson `yaml:"-"` - Weather *feed.Weather `yaml:"-"` - TimeLabels [12]string `yaml:"-"` -} - -var timeLabels = [12]string{"2am", "4am", "6am", "8am", "10am", "12pm", "2pm", "4pm", "6pm", "8pm", "10pm", "12am"} - -func (widget *Weather) Initialize() error { - widget.withTitle("Weather").withCacheOnTheHour() - widget.TimeLabels = timeLabels - - if widget.Units == "" { - widget.Units = "metric" - } else if widget.Units != "metric" && widget.Units != "imperial" { - return fmt.Errorf("invalid units '%s' for weather, must be either metric or imperial", widget.Units) - } - - place, err := feed.FetchPlaceFromName(widget.Location) - - if err != nil { - return fmt.Errorf("failed fetching data for %s: %v", widget.Location, err) - } - - widget.Place = place - - return nil -} - -func (widget *Weather) Update(ctx context.Context) { - weather, err := feed.FetchWeatherForPlace(widget.Place, widget.Units) - - if !widget.canContinueUpdateAfterHandlingErr(err) { - return - } - - widget.Weather = weather -} - -func (widget *Weather) Render() template.HTML { - return widget.render(widget, assets.WeatherTemplate) -} diff --git a/pkg/sysinfo/sysinfo.go b/pkg/sysinfo/sysinfo.go new file mode 100644 index 0000000..1ed8c75 --- /dev/null +++ b/pkg/sysinfo/sysinfo.go @@ -0,0 +1,297 @@ +package sysinfo + +import ( + "fmt" + "math" + "os" + "runtime" + "sort" + "strconv" + "time" + + "github.com/shirou/gopsutil/v4/cpu" + "github.com/shirou/gopsutil/v4/disk" + "github.com/shirou/gopsutil/v4/host" + "github.com/shirou/gopsutil/v4/load" + "github.com/shirou/gopsutil/v4/mem" + "github.com/shirou/gopsutil/v4/sensors" +) + +type timestampJSON struct { + time.Time +} + +func (t timestampJSON) MarshalJSON() ([]byte, error) { + return []byte(strconv.FormatInt(t.Unix(), 10)), nil +} + +func (t *timestampJSON) UnmarshalJSON(data []byte) error { + i, err := strconv.ParseInt(string(data), 10, 64) + if err != nil { + return err + } + + t.Time = time.Unix(i, 0) + return nil +} + +type SystemInfo struct { + HostInfoIsAvailable bool `json:"host_info_is_available"` + BootTime timestampJSON `json:"boot_time"` + Hostname string `json:"hostname"` + Platform string `json:"platform"` + + CPU struct { + LoadIsAvailable bool `json:"load_is_available"` + Load1Percent uint8 `json:"load1_percent"` + Load15Percent uint8 `json:"load15_percent"` + + TemperatureIsAvailable bool `json:"temperature_is_available"` + TemperatureC uint8 `json:"temperature_c"` + } `json:"cpu"` + + Memory struct { + IsAvailable bool `json:"memory_is_available"` + TotalMB uint64 `json:"total_mb"` + UsedMB uint64 `json:"used_mb"` + UsedPercent uint8 `json:"used_percent"` + + SwapIsAvailable bool `json:"swap_is_available"` + SwapTotalMB uint64 `json:"swap_total_mb"` + SwapUsedMB uint64 `json:"swap_used_mb"` + SwapUsedPercent uint8 `json:"swap_used_percent"` + } `json:"memory"` + + Mountpoints []MountpointInfo `json:"mountpoints"` +} + +type MountpointInfo struct { + Path string `json:"path"` + Name string `json:"name"` + TotalMB uint64 `json:"total_mb"` + UsedMB uint64 `json:"used_mb"` + UsedPercent uint8 `json:"used_percent"` +} + +type SystemInfoRequest struct { + CPUTempSensor string `yaml:"cpu-temp-sensor"` + HideMountpointsByDefault bool `yaml:"hide-mountpoints-by-default"` + Mountpoints map[string]MointpointRequest `yaml:"mountpoints"` +} + +type MointpointRequest struct { + Name string `yaml:"name"` + Hide *bool `yaml:"hide"` +} + +// Currently caches hostname indefinitely which isn't ideal +// Potential issue with caching boot time as it may not initially get reported correctly: +// https://github.com/shirou/gopsutil/issues/842#issuecomment-1908972344 +type cacheableHostInfo struct { + available bool + hostname string + platform string + bootTime timestampJSON +} + +var cachedHostInfo cacheableHostInfo + +func getHostInfo() (cacheableHostInfo, error) { + var err error + info := cacheableHostInfo{} + + info.hostname, err = os.Hostname() + if err != nil { + return info, err + } + + info.platform, _, _, err = host.PlatformInformation() + if err != nil { + return info, err + } + + bootTime, err := host.BootTime() + if err != nil { + return info, err + } + + info.bootTime = timestampJSON{time.Unix(int64(bootTime), 0)} + info.available = true + + return info, nil +} + +func Collect(req *SystemInfoRequest) (*SystemInfo, []error) { + if req == nil { + req = &SystemInfoRequest{} + } + + var errs []error + + addErr := func(err error) { + errs = append(errs, err) + } + + info := &SystemInfo{ + Mountpoints: []MountpointInfo{}, + } + + applyCachedHostInfo := func() { + info.HostInfoIsAvailable = true + info.BootTime = cachedHostInfo.bootTime + info.Hostname = cachedHostInfo.hostname + info.Platform = cachedHostInfo.platform + } + + if cachedHostInfo.available { + applyCachedHostInfo() + } else { + hostInfo, err := getHostInfo() + if err == nil { + cachedHostInfo = hostInfo + applyCachedHostInfo() + } else { + addErr(fmt.Errorf("getting host info: %v", err)) + } + } + + coreCount, err := cpu.Counts(true) + if err == nil { + loadAvg, err := load.Avg() + if err == nil { + info.CPU.LoadIsAvailable = true + if runtime.GOOS == "windows" { + // The numbers returned here seem unreliable on Windows. Even with the CPU pegged + // at close to 50% for multiple minutes, load1 is sometimes way under or way over + // with no clear pattern. Dividing by core count gives numbers that are way too + // low so that's likely not necessary as it is with unix. + info.CPU.Load1Percent = uint8(math.Min(loadAvg.Load1*100, 100)) + info.CPU.Load15Percent = uint8(math.Min(loadAvg.Load15*100, 100)) + } else { + info.CPU.Load1Percent = uint8(math.Min((loadAvg.Load1/float64(coreCount))*100, 100)) + info.CPU.Load15Percent = uint8(math.Min((loadAvg.Load15/float64(coreCount))*100, 100)) + } + } else { + addErr(fmt.Errorf("getting load avg: %v", err)) + } + } else { + addErr(fmt.Errorf("getting core count: %v", err)) + } + + memory, err := mem.VirtualMemory() + if err == nil { + info.Memory.IsAvailable = true + info.Memory.TotalMB = memory.Total / 1024 / 1024 + info.Memory.UsedMB = memory.Used / 1024 / 1024 + info.Memory.UsedPercent = uint8(math.Min(memory.UsedPercent, 100)) + } else { + addErr(fmt.Errorf("getting memory info: %v", err)) + } + + swapMemory, err := mem.SwapMemory() + if err == nil { + info.Memory.SwapIsAvailable = true + info.Memory.SwapTotalMB = swapMemory.Total / 1024 / 1024 + info.Memory.SwapUsedMB = swapMemory.Used / 1024 / 1024 + info.Memory.SwapUsedPercent = uint8(math.Min(swapMemory.UsedPercent, 100)) + } else { + addErr(fmt.Errorf("getting swap memory info: %v", err)) + } + + // currently disabled on Windows because it requires elevated privilidges, otherwise + // 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 + // compared against the temperatures Libre Hardware Monitor reports + // also disabled on openbsd because it's not implemented by go-psutil + if runtime.GOOS != "windows" && runtime.GOOS != "openbsd" { + sensorReadings, err := sensors.SensorsTemperatures() + if err == nil { + if req.CPUTempSensor != "" { + for i := range sensorReadings { + if sensorReadings[i].SensorKey == req.CPUTempSensor { + info.CPU.TemperatureIsAvailable = true + info.CPU.TemperatureC = uint8(sensorReadings[i].Temperature) + break + } + } + + if !info.CPU.TemperatureIsAvailable { + addErr(fmt.Errorf("CPU temperature sensor %s not found", req.CPUTempSensor)) + } + } else if cpuTempSensor := inferCPUTempSensor(sensorReadings); cpuTempSensor != nil { + info.CPU.TemperatureIsAvailable = true + info.CPU.TemperatureC = uint8(cpuTempSensor.Temperature) + } + } else { + addErr(fmt.Errorf("getting sensor readings: %v", err)) + } + } + + addedMountpoints := map[string]struct{}{} + addMountpointInfo := func(requestedPath string, mpReq MointpointRequest) { + if _, exists := addedMountpoints[requestedPath]; exists { + return + } + + isHidden := req.HideMountpointsByDefault + if mpReq.Hide != nil { + isHidden = *mpReq.Hide + } + if isHidden { + return + } + + usage, err := disk.Usage(requestedPath) + if err == nil { + mpInfo := MountpointInfo{ + Path: requestedPath, + Name: mpReq.Name, + TotalMB: usage.Total / 1024 / 1024, + UsedMB: usage.Used / 1024 / 1024, + UsedPercent: uint8(math.Min(usage.UsedPercent, 100)), + } + + info.Mountpoints = append(info.Mountpoints, mpInfo) + addedMountpoints[requestedPath] = struct{}{} + } else { + addErr(fmt.Errorf("getting filesystem usage for %s: %v", requestedPath, err)) + } + } + + if !req.HideMountpointsByDefault { + filesystems, err := disk.Partitions(false) + if err == nil { + for _, fs := range filesystems { + addMountpointInfo(fs.Mountpoint, req.Mountpoints[fs.Mountpoint]) + } + } else { + addErr(fmt.Errorf("getting filesystems: %v", err)) + } + } + + for mountpoint, mpReq := range req.Mountpoints { + addMountpointInfo(mountpoint, mpReq) + } + + sort.Slice(info.Mountpoints, func(a, b int) bool { + return info.Mountpoints[a].UsedPercent > info.Mountpoints[b].UsedPercent + }) + + return info, errs +} + +func inferCPUTempSensor(sensors []sensors.TemperatureStat) *sensors.TemperatureStat { + for i := range sensors { + switch sensors[i].SensorKey { + case + "coretemp_package_id_0", // intel / linux + "coretemp", // intel / linux + "k10temp", // amd / linux + "zenpower", // amd / linux + "cpu_thermal": // raspberry pi / linux + return &sensors[i] + } + } + + return nil +} diff --git a/scripts/build-and-ship/main.go b/scripts/build-and-ship/main.go deleted file mode 100644 index 2349947..0000000 --- a/scripts/build-and-ship/main.go +++ /dev/null @@ -1,237 +0,0 @@ -package main - -import ( - "fmt" - "os" - "os/exec" - "path" - "strings" -) - -// bunch of spaget but it does the job for now -// TODO: tidy up and add a proper build system with CI - -const buildPath = "./build" -const archivesPath = "./build/archives" -const executableName = "glance" -const ownerAndRepo = "glanceapp/glance" -const moduleName = "github.com/" + ownerAndRepo - -type archiveType int - -const ( - archiveTypeTarGz archiveType = iota - archiveTypeZip -) - -type buildInfo struct { - version string -} - -type buildTarget struct { - os string - arch string - armV int - extension string - archive archiveType -} - -var buildTargets = []buildTarget{ - { - os: "windows", - arch: "amd64", - extension: ".exe", - archive: archiveTypeZip, - }, - { - os: "windows", - arch: "arm64", - extension: ".exe", - archive: archiveTypeZip, - }, - { - os: "linux", - arch: "amd64", - }, - { - os: "linux", - arch: "arm64", - }, - { - os: "linux", - arch: "arm", - armV: 6, - }, - { - os: "linux", - arch: "arm", - armV: 7, - }, -} - -func main() { - cwd, err := os.Getwd() - - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - _, err = os.Stat(buildPath) - - if err == nil { - fmt.Println("Cleaning up build path") - os.RemoveAll(buildPath) - } - - os.Mkdir(buildPath, 0755) - os.Mkdir(archivesPath, 0755) - - version, err := getVersionFromGit() - - if err != nil { - fmt.Println(version, err) - os.Exit(1) - } - - info := buildInfo{ - version: version, - } - - for _, target := range buildTargets { - fmt.Printf("Building for %s/%s\n", target.os, target.arch) - if err := build(cwd, info, target); err != nil { - fmt.Println(err) - os.Exit(1) - } - } - - versionTag := fmt.Sprintf("%s:%s", ownerAndRepo, version) - latestTag := fmt.Sprintf("%s:latest", ownerAndRepo) - - fmt.Println("Building docker image") - - output, err := exec.Command( - "sudo", "docker", "build", - "--platform=linux/arm64,linux/amd64", - "-t", versionTag, - "-t", latestTag, - ".", - ).CombinedOutput() - - if err != nil { - fmt.Println(string(output)) - fmt.Println(err) - os.Exit(1) - } - - var input string - fmt.Print("Push docker image? [y/n]: ") - fmt.Scanln(&input) - - if input != "y" { - os.Exit(0) - } - - output, err = exec.Command( - "sudo", "docker", "push", versionTag, - ).CombinedOutput() - - if err != nil { - fmt.Printf("Failed pushing %s:\n", versionTag) - fmt.Println(string(output)) - fmt.Println(err) - os.Exit(1) - } - - output, err = exec.Command( - "sudo", "docker", "push", latestTag, - ).CombinedOutput() - - if err != nil { - fmt.Printf("Failed pushing %s:\n", latestTag) - fmt.Println(string(output)) - fmt.Println(err) - os.Exit(1) - } -} - -func getVersionFromGit() (string, error) { - output, err := exec.Command("git", "describe", "--tags", "--abbrev=0").CombinedOutput() - - if err == nil { - return strings.TrimSpace(string(output)), err - } - - return string(output), err -} - -func archiveFile(name string, target string, t archiveType) error { - var output []byte - var err error - - if t == archiveTypeZip { - output, err = exec.Command("zip", "-j", path.Join(archivesPath, name+".zip"), target).CombinedOutput() - } else if t == archiveTypeTarGz { - output, err = exec.Command("tar", "-C", buildPath, "-czf", path.Join(archivesPath, name+".tar.gz"), name).CombinedOutput() - } - - if err != nil { - fmt.Println(string(output)) - return err - } - - return nil -} - -func build(workingDir string, info buildInfo, target buildTarget) error { - var name string - - if target.arch != "arm" { - name = fmt.Sprintf("%s-%s-%s%s", executableName, target.os, target.arch, target.extension) - } else { - name = fmt.Sprintf("%s-%s-%sv%d", executableName, target.os, target.arch, target.armV) - } - - binaryPath := path.Join(buildPath, name) - - glancePackage := moduleName + "/internal/glance" - - flags := "-s -w" - flags += fmt.Sprintf(" -X %s.buildVersion=%s", glancePackage, info.version) - - cmd := exec.Command( - "go", - "build", - "--trimpath", - "--ldflags", - flags, - "-o", - binaryPath, - ) - - cmd.Dir = workingDir - env := append(os.Environ(), "GOOS="+target.os, "GOARCH="+target.arch, "CGO_ENABLED=0") - - if target.arch == "arm" { - env = append(env, fmt.Sprintf("GOARM=%d", target.armV)) - } - - cmd.Env = env - output, err := cmd.CombinedOutput() - - if err != nil { - fmt.Println(err) - fmt.Println(string(output)) - return err - } - - os.Chmod(binaryPath, 0755) - - fmt.Println("Creating archive") - if err := archiveFile(name, binaryPath, target.archive); err != nil { - return err - } - - return nil -}