Skip to content

Commit

Permalink
Embed templates into Go binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Lena Fuhrimann committed Apr 21, 2021
1 parent 285881e commit 43780bb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ linters:
disable:
- exhaustivestruct
- funlen
- gochecknoglobals
- ifshort
- lll
- nlreturn
Expand Down
8 changes: 3 additions & 5 deletions internal/app/s3manager/bucket_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package s3manager
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"path"
"path/filepath"

"github.com/matryer/way"
minio "github.com/minio/minio-go"
)

// HandleBucketView shows the details page of a bucket.
func HandleBucketView(s3 S3, tmplDir string) http.HandlerFunc {
func HandleBucketView(s3 S3, templates fs.FS) http.HandlerFunc {
type objectWithIcon struct {
Info minio.ObjectInfo
Icon string
Expand Down Expand Up @@ -43,9 +43,7 @@ func HandleBucketView(s3 S3, tmplDir string) http.HandlerFunc {
Objects: objs,
}

l := filepath.Join(tmplDir, "layout.html.tmpl")
p := filepath.Join(tmplDir, "bucket.html.tmpl")
t, err := template.ParseFiles(l, p)
t, err := template.ParseFS(templates, "layout.html.tmpl", "bucket.html.tmpl")
if err != nil {
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
return
Expand Down
5 changes: 3 additions & 2 deletions internal/app/s3manager/bucket_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -133,9 +134,9 @@ func TestHandleBucketView(t *testing.T) {
ListObjectsV2Func: tc.listObjectsV2Func,
}

tmplDir := filepath.Join("..", "..", "..", "web", "template")
templates := os.DirFS(filepath.Join("..", "..", "..", "web", "template"))
r := way.NewRouter()
r.Handle(http.MethodGet, "/buckets/:bucketName", s3manager.HandleBucketView(s3, tmplDir))
r.Handle(http.MethodGet, "/buckets/:bucketName", s3manager.HandleBucketView(s3, templates))

ts := httptest.NewServer(r)
defer ts.Close()
Expand Down
8 changes: 3 additions & 5 deletions internal/app/s3manager/buckets_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ package s3manager
import (
"fmt"
"html/template"
"io/fs"
"net/http"
"path/filepath"
)

// HandleBucketsView renders all buckets on an HTML page.
func HandleBucketsView(s3 S3, tmplDir string) http.HandlerFunc {
func HandleBucketsView(s3 S3, templates fs.FS) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
buckets, err := s3.ListBuckets()
if err != nil {
handleHTTPError(w, fmt.Errorf("error listing buckets: %w", err))
return
}

l := filepath.Join(tmplDir, "layout.html.tmpl")
p := filepath.Join(tmplDir, "buckets.html.tmpl")
t, err := template.ParseFiles(l, p)
t, err := template.ParseFS(templates, "layout.html.tmpl", "buckets.html.tmpl")
if err != nil {
handleHTTPError(w, fmt.Errorf("error parsing template files: %w", err))
return
Expand Down
5 changes: 3 additions & 2 deletions internal/app/s3manager/buckets_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -59,13 +60,13 @@ func TestHandleBucketsView(t *testing.T) {
ListBucketsFunc: tc.listBucketsFunc,
}

tmplDir := filepath.Join("..", "..", "..", "web", "template")
templates := os.DirFS(filepath.Join("..", "..", "..", "web", "template"))

req, err := http.NewRequest(http.MethodGet, "/buckets", nil)
is.NoErr(err)

rr := httptest.NewRecorder()
handler := s3manager.HandleBucketsView(s3, tmplDir)
handler := s3manager.HandleBucketsView(s3, templates)

handler.ServeHTTP(rr, req)
resp := rr.Result()
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"crypto/tls"
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/mastertinner/adapters/logging"
Expand All @@ -15,6 +16,9 @@ import (
minio "github.com/minio/minio-go"
)

//go:embed web/template
var templateFS embed.FS

func main() {
endpoint, ok := os.LookupEnv("ENDPOINT")
if !ok {
Expand All @@ -35,7 +39,11 @@ func main() {
port = "8080"
}

tmplDir := filepath.Join("web", "template")
// Set up templates
templates, err := fs.Sub(templateFS, "web/template")
if err != nil {
log.Fatal(err)
}

// Set up S3 client
s3, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
Expand All @@ -49,8 +57,8 @@ func main() {
// Set up router
r := way.NewRouter()
r.Handle(http.MethodGet, "/", http.RedirectHandler("/buckets", http.StatusPermanentRedirect))
r.Handle(http.MethodGet, "/buckets", s3manager.HandleBucketsView(s3, tmplDir))
r.Handle(http.MethodGet, "/buckets/:bucketName", s3manager.HandleBucketView(s3, tmplDir))
r.Handle(http.MethodGet, "/buckets", s3manager.HandleBucketsView(s3, templates))
r.Handle(http.MethodGet, "/buckets/:bucketName", s3manager.HandleBucketView(s3, templates))
r.Handle(http.MethodPost, "/api/buckets", s3manager.HandleCreateBucket(s3))
r.Handle(http.MethodDelete, "/api/buckets/:bucketName", s3manager.HandleDeleteBucket(s3))
r.Handle(http.MethodPost, "/api/buckets/:bucketName/objects", s3manager.HandleCreateObject(s3))
Expand Down

0 comments on commit 43780bb

Please sign in to comment.