Skip to content

Commit 2548e8a

Browse files
fifiugjkohen
fifiug
andauthored
Use statik to generate statusz template as code. (#206)
* Moving var statuszTmpl to func ServeHTTP When trying to run $GOPATH/bin/stackdriver-prometheus-sidecar --help ,I get error panic: open statusz-tmpl.html: no such file or directory. Moving the intialization of the statuszTmpl to func ServeHTTP which allows the use of --help * Moving statusz-tmpl.html to a const in statusz.go and updating related files. * Using statik as a code generator * Updating statusz.go * Updating statusz.go and main.go * Updating statusz.go * Adding dependcies from running go mod vendor * Updating statik Co-authored-by: Javier Kohen <[email protected]>
1 parent b0ee70e commit 2548e8a

File tree

9 files changed

+539
-2
lines changed

9 files changed

+539
-2
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ FROM gcr.io/distroless/static:latest
22
LABEL maintainer "Stackdriver Engineering <[email protected]>"
33

44
COPY stackdriver-prometheus-sidecar /bin/stackdriver-prometheus-sidecar
5-
COPY cmd/stackdriver-prometheus-sidecar/statusz-tmpl.html /statusz-tmpl.html
65

76
EXPOSE 9091
87
ENTRYPOINT [ "/bin/stackdriver-prometheus-sidecar" ]

cmd/stackdriver-prometheus-sidecar/statik/statik.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/stackdriver-prometheus-sidecar/statusz.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
15+
//go:generate statik -f -src=./ -include=*.html
1416
package main
1517

1618
import (
1719
"html/template"
20+
"io/ioutil"
1821
"net/http"
1922
"os"
2023
"path/filepath"
2124
"time"
2225

26+
_ "github.com/Stackdriver/stackdriver-prometheus-sidecar/cmd/stackdriver-prometheus-sidecar/statik"
2327
"github.com/go-kit/kit/log"
2428
"github.com/go-kit/kit/log/level"
2529
"github.com/prometheus/common/version"
30+
"github.com/rakyll/statik/fs"
2631
)
2732

2833
var (
2934
serverStart = time.Now()
30-
statuszTmpl = template.Must(template.ParseFiles("statusz-tmpl.html"))
35+
statuszTmpl *template.Template
3136
)
3237

3338
type statuszHandler struct {
@@ -36,6 +41,25 @@ type statuszHandler struct {
3641
cfg *mainConfig
3742
}
3843

44+
func init() {
45+
statikFS, err := fs.New()
46+
if err != nil {
47+
panic(err)
48+
}
49+
statuszFile, err := statikFS.Open("/statusz-tmpl.html")
50+
if err != nil {
51+
panic(err)
52+
}
53+
contents, err := ioutil.ReadAll(statuszFile)
54+
if err != nil {
55+
panic(err)
56+
}
57+
statuszTmpl, err = template.New("statusz-tmpl.html").Parse(string(contents))
58+
if err != nil {
59+
panic(err)
60+
}
61+
}
62+
3963
func (h *statuszHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
4064
var data struct {
4165
ServerName string

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ require (
6060
github.com/prometheus/procfs v0.0.3 // indirect
6161
github.com/prometheus/prometheus v0.0.0-20190710134608-e5b22494857d
6262
github.com/prometheus/tsdb v0.10.0
63+
github.com/rakyll/statik v0.1.6
6364
github.com/rogpeppe/go-internal v1.5.0 // indirect
6465
github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 // indirect
6566
github.com/shopspring/decimal v0.0.0-20191125035519-b054a8dfd10d // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ github.com/prometheus/prometheus v0.0.0-20190710134608-e5b22494857d/go.mod h1:11
474474
github.com/prometheus/tsdb v0.9.1/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
475475
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
476476
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
477+
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
478+
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
477479
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
478480
github.com/rlmcpherson/s3gof3r v0.5.0/go.mod h1:s7vv7SMDPInkitQMuZzH615G7yWHdrU2r/Go7Bo71Rs=
479481
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=

vendor/github.com/rakyll/statik/LICENSE

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)