Skip to content
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
1 change: 1 addition & 0 deletions internal/templates/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@
rel="noopener"
>{{COMMIT}}</a
>
({{BUILD_DATE}})
</div>
</footer>
<script
Expand Down
1 change: 1 addition & 0 deletions internal/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ <h2>Recent games (this browser)</h2>
rel="noopener"
>{{COMMIT}}</a
>
({{BUILD_DATE}})
</div>
</footer>
<script
Expand Down
8 changes: 7 additions & 1 deletion internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
)

var commit = "dev"
var buildDate = ""

func SetCommit(c string) { commit = c }
func SetVersion(c, d string) {
commit = c
buildDate = d
}

// WriteHomeHTML serves the home page template
func WriteHomeHTML(w http.ResponseWriter) {
Expand All @@ -23,6 +27,7 @@ func WriteHomeHTML(w http.ResponseWriter) {
return
}
html := strings.ReplaceAll(string(content), "{{COMMIT}}", commit)
html = strings.ReplaceAll(html, "{{BUILD_DATE}}", buildDate)
_, _ = w.Write([]byte(html))
}

Expand All @@ -40,6 +45,7 @@ func WriteGameHTML(w http.ResponseWriter, gameID string) {

html := strings.ReplaceAll(string(content), "{{GAME_ID}}", gameID)
html = strings.ReplaceAll(html, "{{COMMIT}}", commit)
html = strings.ReplaceAll(html, "{{BUILD_DATE}}", buildDate)
_, _ = w.Write([]byte(html))
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
flag.Parse()
logging.Debug = *debug

templates.SetCommit(commit)
templates.SetVersion(commit, buildDate)

if dsn := os.Getenv("DATABASE_URL"); dsn != "" {
if _, err := storage.New(dsn); err != nil {
Expand Down
32 changes: 22 additions & 10 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,38 @@ import (
"os/exec"
"runtime/debug"
"strings"
"time"
)

var commit = "dev"
var buildDate = ""

func init() {
if commit != "dev" {
return
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, s := range info.Settings {
if s.Key == "vcs.revision" && s.Value != "" {
commit = s.Value
if len(commit) > 7 {
commit = commit[:7]
switch s.Key {
case "vcs.revision":
if commit == "dev" && s.Value != "" {
commit = s.Value
if len(commit) > 7 {
commit = commit[:7]
}
}
case "vcs.time":
if buildDate == "" && s.Value != "" {
if t, err := time.Parse(time.RFC3339, s.Value); err == nil {
buildDate = t.Format("1-2-2006")
}
}
return
}
}
}
if c, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
commit = strings.TrimSpace(string(c))
if commit == "dev" {
if c, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output(); err == nil {
commit = strings.TrimSpace(string(c))
}
}
if buildDate == "" {
buildDate = time.Now().Format("1-2-2006")
}
}