Created
December 20, 2020 21:03
-
-
Save wolfeidau/9f43e3f44cdb7a5f77405d3aa60421fc to your computer and use it in GitHub Desktop.
Golang embed with echo and tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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