Skip to content

Instantly share code, notes, and snippets.

@wolfeidau
Created December 20, 2020 21:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfeidau/9f43e3f44cdb7a5f77405d3aa60421fc to your computer and use it in GitHub Desktop.
Save wolfeidau/9f43e3f44cdb7a5f77405d3aa60421fc to your computer and use it in GitHub Desktop.
Golang embed with echo and tests
package server
import (
"embed"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
// content holds our static web server content.
//go:embed static/*
var content embed.FS
var contentHandler = echo.WrapHandler(http.FileServer(http.FS(content)))
var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/static/$1"})
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/require"
)
func TestStaticEmbed(t *testing.T) {
assert := require.New(t)
f, err := content.Open("static/index.html")
assert.NoError(err)
assert.NotEmpty(f)
}
func TestEchoStatic(t *testing.T) {
assert := require.New(t)
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/favicon.ico", nil)
rec := httptest.NewRecorder()
e.GET("/*", contentHandler, contentRewrite)
e.ServeHTTP(rec, req)
assert.Equal(http.StatusOK, rec.Code)
}
package server
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/labstack/echo/v4"
"github.com/wolfeidau/analytics-beacon/internal/flags"
)
// Setup setup handlers for the server and configure any service APIs
func Setup(cfg *flags.API, awscfg *aws.Config, e *echo.Echo) error {
e.GET("/*", contentHandler, contentRewrite)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment