Skip to content

Commit

Permalink
feat: add version to /status endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: guillaume <[email protected]>
  • Loading branch information
gruyaume committed Sep 24, 2024
1 parent 093c46d commit e0c5b8d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/server/handlers_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/crypto/bcrypt"
)

func expireAfter(hours int) int64 {
func expireAfter() int64 {
return time.Now().Add(time.Hour * 1).Unix()
}

Expand All @@ -39,7 +39,7 @@ func generateJWT(id int, username string, jwtSecret []byte, permissions int) (st
Username: username,
Permissions: permissions,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expireAfter(1),
ExpiresAt: expireAfter(),
},
})
tokenString, err := token.SignedString(jwtSecret)
Expand Down
6 changes: 5 additions & 1 deletion internal/server/handlers_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package server

import (
"net/http"

"github.com/canonical/notary/version"
)

type StatusResponse struct {
Initialized bool `json:"initialized"`
Initialized bool `json:"initialized"`
Version string `json:"version"`
}

// the GET status endpoint returns a http.StatusOK alongside info about the server
Expand All @@ -19,6 +22,7 @@ func GetStatus(env *HandlerConfig) http.HandlerFunc {
}
statusResponse := StatusResponse{
Initialized: numUsers > 0,
Version: version.GetVersion(),
}
w.WriteHeader(http.StatusOK)
err = writeJSON(w, statusResponse)
Expand Down
11 changes: 10 additions & 1 deletion internal/server/handlers_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type GetStatusResponseResult struct {
Initialized bool `json:"initialized"`
Initialized bool `json:"initialized"`
Version string `json:"version"`
}

type GetStatusResponse struct {
Expand Down Expand Up @@ -58,6 +59,10 @@ func TestStatus(t *testing.T) {
if statusResponse.Result.Initialized {
t.Fatalf("expected initialized to be false")
}

if statusResponse.Result.Version == "" {
t.Fatalf("expected version to be set")
}
})

var adminToken string
Expand All @@ -81,5 +86,9 @@ func TestStatus(t *testing.T) {
if !statusResponse.Result.Initialized {
t.Fatalf("expected initialized to be true")
}

if statusResponse.Result.Version == "" {
t.Fatalf("expected version to be set")
}
})
}
9 changes: 8 additions & 1 deletion rockcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: notary
base: bare
build-base: [email protected]
version: '0.0.3'
adopt-info: notary-release-data
summary: A certificate management tool
description: |
A certificate management tool.
Expand Down Expand Up @@ -32,3 +32,10 @@ parts:
- ca-certificates_data
- libc6_libs
- base-files_lib

notary-release-data:
plugin: nil
source: .
override-build: |
version="$(cat version/VERSION)"
craftctl set version="$version"
9 changes: 8 additions & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: notary
base: core24
version: '0.0.3'
adopt-info: notary-release-data
summary: Notary is a certificate management tool.
license: Apache-2.0
description: |
Expand Down Expand Up @@ -43,6 +43,13 @@ parts:
npm run build --prefix ui
craftctl default
notary-release-data:
plugin: nil
source: .
override-build: |
version="$(cat version/VERSION)"
craftctl set version="$version"
service-files:
plugin: dump
source: service
1 change: 1 addition & 0 deletions version/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.3
12 changes: 12 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package version

import (
_ "embed"
)

//go:embed VERSION
var version string

func GetVersion() string {
return version
}

0 comments on commit e0c5b8d

Please sign in to comment.