Skip to content

Commit

Permalink
Merge 331194a into eceb4d0
Browse files Browse the repository at this point in the history
  • Loading branch information
darklynx committed Jan 17, 2019
2 parents eceb4d0 + 331194a commit 1c5bf30
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
14 changes: 14 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# exit if any step fails
set -e

# change current dir to the script dir
cd "$(dirname "$0")"

GIT_VERSION="$(git describe)"
GIT_COMMIT="$(git rev-parse HEAD)"
GIT_COMMIT_SHORT="$(git rev-parse --short HEAD)"

# build service with version information
go get -ldflags "-X main.GitVersion=${GIT_VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.GitCommitShort=${GIT_COMMIT_SHORT}"
11 changes: 9 additions & 2 deletions docker/multistage/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Builds Request Baskets service using multi-stage builds
# Version 1.0
# Version 1.1

# Stage 1. Building service
FROM golang:latest as builder
WORKDIR /go/src/rbaskets
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/rbaskets
RUN set -e \
&& GIT_VERSION="$(git describe)" \
&& GIT_COMMIT="$(git rev-parse HEAD)" \
&& GIT_COMMIT_SHORT="$(git rev-parse --short HEAD)" \
&& set -x \
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo \
-ldflags="-w -s -X main.GitVersion=${GIT_VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.GitCommitShort=${GIT_COMMIT_SHORT}" \
-o /go/bin/rbaskets

# Stage 2. Packaging into alpine
FROM alpine:latest
Expand Down
6 changes: 4 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (

var validBasketName = regexp.MustCompile(basketNamePattern)
var defaultResponse = ResponseConfig{Status: http.StatusOK, Headers: http.Header{}, IsTemplate: false}
var indexPageTemplate = template.Must(template.New("index").Parse(indexPageContentTemplate))
var basketPageTemplate = template.Must(template.New("basket").Parse(basketPageContentTemplate))
var basketsPageTemplate = template.Must(template.New("baskets").Parse(basketsPageContentTemplate))

// writeJSON writes JSON content to HTTP response
func writeJSON(w http.ResponseWriter, status int, json []byte, err error) {
Expand Down Expand Up @@ -328,7 +330,7 @@ func ForwardToWeb(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
// WebIndexPage handles HTTP request to render index page
func WebIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(indexPageContent)
indexPageTemplate.Execute(w, version)
}

// WebBasketPage handles HTTP request to render basket details page
Expand All @@ -338,7 +340,7 @@ func WebBasketPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
case serviceAPIPath:
// admin page to access all baskets
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(basketsPageContent)
basketsPageTemplate.Execute(w, version)
default:
basketPageTemplate.Execute(w, name)
}
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import (
var basketsDb BasketsDatabase
var httpClient *http.Client
var httpInsecureClient *http.Client
var version *Version

// CreateServer creates an instance of Request Baskets server
func CreateServer(config *ServerConfig) *http.Server {
version = &Version{Version: GitVersion, Commit: GitCommit, CommitShort: GitCommitShort}

log.Printf("[info] service version: %s from commit: %s (%s)", version.Version, version.CommitShort, version.Commit)
// create database
db := createBasketsDatabase(config.DbType, config.DbFile, config.DbConnection)
if db == nil {
Expand Down
17 changes: 17 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

var (
// GitVersion is a placeholder to inject git version, result of `git describe`
GitVersion = "n/a"
// GitCommit is a placeholder to inject git commit, result of `git rev-parse HEAD`
GitCommit = "n/a"
// GitCommitShort is a placeholder to inject git commit, result of `git rev-parse --short HEAD`
GitCommitShort = "n/a"
)

// Version describes application version
type Version struct {
Version string
Commit string
CommitShort string
}
11 changes: 8 additions & 3 deletions web_baskets.html.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

var (
basketsPageContent = []byte(`<!DOCTYPE html>
basketsPageContentTemplate = `<!DOCTYPE html>
<html>
<head lang="en">
<title>Request Baskets - Administration</title>
Expand Down Expand Up @@ -253,9 +253,14 @@ var (
<footer class="footer">
<div class="container">
<p class="text-muted"><small>Powered by <a href="https://github.com/darklynx/request-baskets">request-baskets</a></small></p>
<p class="text-muted">
<small>
Powered by <a href="https://github.com/darklynx/request-baskets">request-baskets</a> |
Version: <abbr title="From commit: {{.CommitShort}} ({{.Commit}})">{{.Version}}</abbr>
</small>
</p>
</div>
</footer>
</body>
</html>`)
</html>`
)
11 changes: 8 additions & 3 deletions web_index.html.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

var (
indexPageContent = []byte(`<!DOCTYPE html>
indexPageContentTemplate = `<!DOCTYPE html>
<html>
<head lang="en">
<title>Request Baskets</title>
Expand Down Expand Up @@ -172,9 +172,14 @@ var (
<footer class="footer">
<div class="container">
<p class="text-muted"><small>Powered by <a href="https://github.com/darklynx/request-baskets">request-baskets</a></small></p>
<p class="text-muted">
<small>
Powered by <a href="https://github.com/darklynx/request-baskets">request-baskets</a> |
Version: <abbr title="From commit: {{.CommitShort}} ({{.Commit}})">{{.Version}}</abbr>
</small>
</p>
</div>
</footer>
</body>
</html>`)
</html>`
)

0 comments on commit 1c5bf30

Please sign in to comment.