Skip to content

Commit

Permalink
Embed version in executable and expose version in API and frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
tfabritius committed May 22, 2023
1 parent 7ac7dfb commit d042fdb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion backend/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ builds:
- -s
- -w
# Embed version
- -X main.version={{.Version}}
- -X github.com/tfabritius/plainpage/build.version={{.Version}}

archives:
- name_template: >-
Expand Down
22 changes: 22 additions & 0 deletions backend/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package build

import "runtime/debug"

// version will be set via flag at build time
var version = "dev"

func GetVersion() string {
return version
}

func GetRevision() string {
info, _ := debug.ReadBuildInfo()

for _, kv := range info.Settings {
if kv.Key == "vcs.revision" {
return kv.Value
}
}

return ""
}
3 changes: 3 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/joho/godotenv"
"github.com/tfabritius/plainpage/build"
"github.com/tfabritius/plainpage/server"
"github.com/tfabritius/plainpage/service"
)
Expand Down Expand Up @@ -41,6 +42,8 @@ func getDataDir() string {
}

func main() {
log.Printf("📄 Plainpage %s\n", build.GetVersion())

if err := godotenv.Load(); err != nil && !errors.Is(err, fs.ErrNotExist) {
log.Fatalln("Error loading .env file:", err)
}
Expand Down
1 change: 1 addition & 0 deletions backend/model/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type GetAppResponse struct {
SetupMode bool `json:"setupMode"`
AllowRegister bool `json:"allowRegister"`
AllowAdmin bool `json:"allowAdmin"`
Version string `json:"version"`
GitSha string `json:"gitSha"`
}

Expand Down
19 changes: 3 additions & 16 deletions backend/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@ package server
import (
"encoding/json"
"net/http"
"runtime/debug"

"github.com/go-chi/render"
"github.com/tfabritius/plainpage/build"
"github.com/tfabritius/plainpage/model"
"github.com/tfabritius/plainpage/service/ctxutil"
)

func getRevision() string {
info, _ := debug.ReadBuildInfo()

for _, kv := range info.Settings {
if kv.Key == "vcs.revision" {
return kv.Value
}
}

return ""
}

func (app App) exposeConfig(w http.ResponseWriter, r *http.Request) {
userID := ctxutil.UserID(r.Context())

Expand All @@ -33,14 +21,13 @@ func (app App) exposeConfig(w http.ResponseWriter, r *http.Request) {
panic(err)
}

gitSha := getRevision()

response := model.GetAppResponse{
AppTitle: cfg.AppTitle,
SetupMode: cfg.SetupMode,
AllowRegister: allowRegister,
AllowAdmin: allowAdmin,
GitSha: gitSha,
GitSha: build.GetRevision(),
Version: build.GetVersion(),
}

render.JSON(w, r, response)
Expand Down
4 changes: 2 additions & 2 deletions frontend/pages/_admin/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { t } = useI18n()
useHead({ title: t('configuration') })
const app = useAppStore()
const { gitSha } = storeToRefs(app)
const { version } = storeToRefs(app)
const { data, error, refresh } = await useAsyncData('/config', () => apiFetch<Config>('/config'))
Expand Down Expand Up @@ -70,7 +70,7 @@ async function onSave() {
</ElFormItem>
<ElFormItem :label="$t('version')">
<ElInput
:value="gitSha" disabled
:value="version" disabled
/>
</ElFormItem>
</ElForm>
Expand Down
4 changes: 2 additions & 2 deletions frontend/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export const useAppStore = defineStore(
const appTitle = computed(() => data.value?.appTitle ?? 'PlainPage')
const allowAdmin = computed(() => data.value?.allowAdmin || false)
const allowRegister = computed(() => data.value?.allowRegister ?? false)
const gitSha = computed(() => data.value?.gitSha ?? '')
const version = computed(() => data.value?.version ?? '')

return {
appTitle,
allowAdmin,
allowRegister,
gitSha,
version,
refresh,
}
},
Expand Down
1 change: 1 addition & 0 deletions frontend/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface GetAppResponse {
setupMode: boolean
allowRegister: boolean
allowAdmin: boolean
version: string
gitSha: string
}

Expand Down

0 comments on commit d042fdb

Please sign in to comment.