Skip to content

Commit

Permalink
Embed HTML templates in executable
Browse files Browse the repository at this point in the history
Refactoring around templates and main function.
  • Loading branch information
kjsanger committed Apr 4, 2024
1 parent b806a5b commit 95a22b5
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ all: build
build: build-linux build-darwin build-windows

build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${build_args} -o sqyrrl-linux-amd64 ./cmd/sqyrrl.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${build_args} -o sqyrrl-linux-amd64 ./main.go

build-darwin:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build ${build_args} -o sqyrrl-darwin-amd64 ./cmd/sqyrrl.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build ${build_args} -o sqyrrl-darwin-amd64 ./main.go

build-windows:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ${build_args} -o sqyrrl-windows-amd64.exe ./cmd/sqyrrl.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ${build_args} -o sqyrrl-windows-amd64.exe ./main.go

install:
go install -ldflags ${ldflags}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sqyrrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main
package cmd

import (
"fmt"
Expand Down Expand Up @@ -125,7 +125,7 @@ func startServer(cmd *cobra.Command, args []string) {
})
}

func main() {
func CLI() {
rootCmd := &cobra.Command{
Use: "sqyrrl",
Short: "Sqyrrl.",
Expand Down
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024. Genome Research Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
"sqyrrl/cmd"
)

func main() {
cmd.CLI()
}
7 changes: 4 additions & 3 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package server

import (
"github.com/cyverse/go-irodsclient/irods/types"
"github.com/rs/zerolog"
"net/http"
"path"

"github.com/cyverse/go-irodsclient/irods/types"
"github.com/rs/zerolog"
)

// HandleHomePage is a handler for the static home page.
Expand All @@ -48,7 +49,7 @@ func HandleHomePage(logger zerolog.Logger) http.Handler {
data := customData{Version: Version, URL: r.URL.RequestURI()}

tplName := "home.gohtml"
if err := GetTemplates().ExecuteTemplate(w, tplName, data); err != nil {
if err := templates.ExecuteTemplate(w, tplName, data); err != nil {
logger.Err(err).
Str("tplName", tplName).
Msg("Failed to execute HTML template")
Expand Down
22 changes: 8 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package server

import (
"context"
"embed"
"errors"
"html/template"
"net"
Expand All @@ -27,7 +28,6 @@ import (
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -75,21 +75,15 @@ const correlationIDKey = ContextKey("correlation_id")
var userInputPolicy = bluemonday.StrictPolicy()

var (
compileOnce sync.Once
templates *template.Template
//go:embed templates/*
embedded embed.FS
// HTML templates used by the server
templates *template.Template
)

// GetTemplates returns the HTML templates for the server.
//
// This exists to allow the tests to load the templates more easily from the context of
// the test subdirectory. This function must be called once in test suite setup,
// with the working directory set to the root of the project, to load the templates.
// After that, it may be called freely in any context to access the loaded templates.
func GetTemplates() *template.Template {
compileOnce.Do(func() {
templates = template.Must(template.ParseGlob("templates/*"))
})
return templates
// Embed the HTML templates at compile time
func init() {
templates = template.Must(template.ParseFS(embedded, "templates/*"))
}

// NewSqyrrlServer creates a new SqyrrlServer instance.
Expand Down
8 changes: 0 additions & 8 deletions server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ var _ = BeforeSuite(func() {
"setting up HTML templates")
}
}(dir)

// For the tests, make sure that the initial invocation of server. GetTemplates is
// done from a directory where the templates are located and not in the subdirectory
// where the tests are being run (because the templates are not there).
err = os.Chdir("..")
Expect(err).NotTo(HaveOccurred())

server.GetTemplates()
})

// Release the iRODS filesystem
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 95a22b5

Please sign in to comment.