Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print version information in app #529

Merged
merged 11 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ jobs:
at: ./
- run:
name: Compile production backend
command: dev-scripts/build-backend
command: PS_VERSION="$(git describe --tags)" dev-scripts/build-backend
- run:
name: Compile dev backend
command: dev-scripts/build-backend dev
command: PS_VERSION="$(git describe --tags)" dev-scripts/build-backend dev
- persist_to_workspace:
root: ./
paths:
Expand Down Expand Up @@ -129,10 +129,10 @@ jobs:
docker buildx build \
--platform "${BUILD_TARGETS}" \
--target=artifact \
--output "type=local,dest=$(pwd)/build/" \
--output "type=local,dest=$(pwd)/bin/" \
.
- store_artifacts:
path: build
path: bin
- run:
name: Install compress utility for gzip compression
command: |
Expand Down
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.vscode/
data/
bin/
build/
dist/
.coverage*
*.env
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Compiled binaries
bin/
build/
dist/

# Persistent data directory
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM golang:1.21.1 AS builder

ARG TARGETPLATFORM
ARG PS_VERSION

COPY ./build /app/build
COPY ./cmd /app/cmd
COPY ./dev-scripts /app/dev-scripts
COPY ./garbagecollect /app/garbagecollect
Expand All @@ -14,7 +16,9 @@ COPY ./go.* /app/

WORKDIR /app

RUN TARGETPLATFORM="${TARGETPLATFORM}" ./dev-scripts/build-backend "prod"
RUN TARGETPLATFORM="${TARGETPLATFORM}" \
PS_VERSION="${PS_VERSION}" \
./dev-scripts/build-backend "prod"

FROM scratch as artifact
COPY --from=builder /app/bin/picoshare ./
Expand Down
21 changes: 21 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package build

import (
"log"
"strconv"
"time"
)

// These values are set by ldflags at build time.
// https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications
var unixTime string
var Version string

func Time() time.Time {
t, err := strconv.ParseInt(unixTime, 10, 64)
if err != nil {
log.Printf("no build time specified through ldflags")
return time.Time{}
}
return time.Unix(t, 0)
}
2 changes: 1 addition & 1 deletion dev-scripts/build-backend
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ export CGO_ENABLED=1

go build \
-tags "${BUILD_TAGS_JOINED}" \
-ldflags '-w -extldflags "-static"' \
-ldflags "-w -extldflags '-static' -X 'github.com/mtlynch/picoshare/v2/build.Version=${PS_VERSION}' -X 'github.com/mtlynch/picoshare/v2/build.unixTime=$(date +%s)'" \
-o "${BINARY}" \
cmd/picoshare/main.go
2 changes: 1 addition & 1 deletion dev-scripts/package-binaries
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
readonly SCRIPT_DIR
cd "${SCRIPT_DIR}/.."

cd build
cd bin

readonly OUTPUT_DIR="${PWD}/../dist"
mkdir -p "${OUTPUT_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion dev-scripts/run-e2e-tests
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ readonly SCRIPT_DIR
cd "${SCRIPT_DIR}/.."

if [[ "${REBUILD}" == "true" ]]; then
./dev-scripts/build-backend dev
PS_VERSION="$(git describe --tags)" ./dev-scripts/build-backend dev
fi

cd e2e
Expand Down
10 changes: 10 additions & 0 deletions e2e/system-info.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from "@playwright/test";
import { login } from "./helpers/login.js";

test("views system info", async ({ page }) => {
await login(page);

await page.getByRole("menuitem", { name: "System" }).hover();
await page.getByRole("menuitem", { name: "Information" }).click();
await expect(page).toHaveURL("/information");
});
2 changes: 1 addition & 1 deletion handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *Server) routes() {
authenticatedViews := s.router.PathPrefix("/").Subrouter()
authenticatedViews.Use(s.requireAuthentication)
authenticatedViews.Use(enforceContentSecurityPolicy)
authenticatedViews.HandleFunc("/disk-usage", s.diskUsageGet()).Methods(http.MethodGet)
authenticatedViews.HandleFunc("/information", s.systemInformationGet()).Methods(http.MethodGet)
authenticatedViews.HandleFunc("/files", s.fileIndexGet()).Methods(http.MethodGet)
authenticatedViews.HandleFunc("/files/{id}/downloads", s.fileDownloadsGet()).Methods(http.MethodGet)
authenticatedViews.HandleFunc("/files/{id}/edit", s.fileEditGet()).Methods(http.MethodGet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
{{ end }}

{{ define "content" }}
<h1 class="title">Disk Usage</h1>
<h1 class="title">System Information</h1>

<h2>Disk Usage</h2>
<progress
class="progress is-primary"
max="{{ .TotalBytes }}"
Expand All @@ -32,12 +33,14 @@ <h1 class="title">Disk Usage</h1>
{{ percentage .UsedBytes .TotalBytes }}
</progress>

<p>
<strong>Used space</strong>:
{{ formatFileSize .UsedBytes }}
({{ percentage .UsedBytes .TotalBytes }})
</p>
<p><strong>Total disk space</strong>: {{ formatFileSize .TotalBytes }}</p>
<ul>
<li>
<strong>Used space</strong>:
{{ formatFileSize .UsedBytes }}
({{ percentage .UsedBytes .TotalBytes }})
</li>
<li><strong>Total disk space</strong>: {{ formatFileSize .TotalBytes }}</li>
</ul>

<div class="notification is-info is-light">
<p>
Expand All @@ -55,6 +58,15 @@ <h1 class="title">Disk Usage</h1>
>.
</p>
</div>

<h2>PicoShare Version</h2>
<ul>
<li><strong>Version</strong>: {{ .Version }}</li>
<li>
<strong>Built at</strong>:
{{ .BuildTime.Format "2006-01-02 15:04:05 UTC" }}
</li>
</ul>
{{ end }}

{{ template "base.html" }}
6 changes: 3 additions & 3 deletions handlers/templates/partials/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" role="menuitem">System</a>
<div class="navbar-dropdown">
<a class="navbar-item" role="menuitem" href="/information"
>Information</a
>
<a class="navbar-item" role="menuitem" href="/settings"
>Settings</a
>
<a class="navbar-item" role="menuitem" href="/disk-usage"
>Disk Usage</a
>
<a id="navbar-log-out" class="navbar-item" role="menuitem"
>Log Out</a
>
Expand Down
12 changes: 9 additions & 3 deletions handlers/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gorilla/mux"
"github.com/mileusna/useragent"
"github.com/mtlynch/picoshare/v2/build"
"github.com/mtlynch/picoshare/v2/handlers/parse"
"github.com/mtlynch/picoshare/v2/picoshare"
"github.com/mtlynch/picoshare/v2/store"
Expand Down Expand Up @@ -550,22 +551,27 @@ func (s Server) settingsGet() http.HandlerFunc {
}
}

func (s Server) diskUsageGet() http.HandlerFunc {
func (s Server) systemInformationGet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
space, err := s.spaceChecker.Check()
if err != nil {
log.Printf("error checking available space: %v", err)
http.Error(w, fmt.Sprintf("failed to check available space: %v", err), http.StatusInternalServerError)
return
}
if err := renderTemplate(w, "disk-usage.html", struct {

if err := renderTemplate(w, "system-information.html", struct {
commonProps
UsedBytes uint64
TotalBytes uint64
BuildTime time.Time
Version string
}{
commonProps: makeCommonProps("PicoShare - Disk Usage", r.Context()),
commonProps: makeCommonProps("PicoShare - System Information", r.Context()),
UsedBytes: space.TotalBytes - space.AvailableBytes,
TotalBytes: space.TotalBytes,
BuildTime: build.Time(),
Version: build.Version,
}, template.FuncMap{
"formatFileSize": humanReadableFileSize,
"percentage": func(part, total uint64) string {
Expand Down
2 changes: 1 addition & 1 deletion modd.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
**/*.go !**/*_test.go **/*.html **/*.js **/*.css {
daemon: go build --tags 'dev' -o ./bin/picoshare-dev cmd/picoshare/main.go && PS_SHARED_SECRET=somepassword ./bin/picoshare-dev -db data/store.db
daemon: PS_VERSION="$(git describe --tags)" dev-scripts/build-backend dev && PS_SHARED_SECRET=somepassword ./bin/picoshare-dev -db data/store.db
}
Loading