From 92df056c395c06ef2118b6a973b60fa096c51cb4 Mon Sep 17 00:00:00 2001 From: ybkuroki <45133652+ybkuroki@users.noreply.github.com> Date: Sun, 17 Jul 2022 16:59:32 +0900 Subject: [PATCH] Embedded the static files --- application.develop.yml | 2 +- config/config.go | 2 +- main.go | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/application.develop.yml b/application.develop.yml index 4964a017..e5caaf4d 100644 --- a/application.develop.yml +++ b/application.develop.yml @@ -16,7 +16,7 @@ log: request_log_format: ${remote_ip} ${account_name} ${uri} ${method} ${status} staticcontents: - path: ./public/ + enabled: true security: auth_path: diff --git a/config/config.go b/config/config.go index 7c0f5ac5..bcf05b6d 100644 --- a/config/config.go +++ b/config/config.go @@ -35,7 +35,7 @@ type Config struct { RequestLogFormat string `yaml:"request_log_format" default:"${remote_ip} ${account_name} ${uri} ${method} ${status}"` } StaticContents struct { - Path string `yaml:"path"` + Enabled bool `default:"false"` } Security struct { AuthPath []string `yaml:"auth_path"` diff --git a/main.go b/main.go index 976ce699..48c537a6 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "embed" + "net/http" "github.com/labstack/echo/v4" + echomd "github.com/labstack/echo/v4/middleware" "github.com/ybkuroki/go-webapp-sample/config" "github.com/ybkuroki/go-webapp-sample/container" "github.com/ybkuroki/go-webapp-sample/logger" @@ -20,6 +22,9 @@ var yamlFile embed.FS //go:embed zaplogger.*.yml var zapYamlFile embed.FS +//go:embed public/* +var staticFile embed.FS + // @title go-webapp-sample API // @version 1.5.1 // @description This is API specification for go-webapp-sample project. @@ -47,9 +52,15 @@ func main() { middleware.InitLoggerMiddleware(e, container) middleware.InitSessionMiddleware(e, container) - if conf.StaticContents.Path != "" { - e.Static("/", conf.StaticContents.Path) - logger.GetZapLogger().Infof("Served the static contents. path: " + conf.StaticContents.Path) + if conf.StaticContents.Enabled { + e.Use(echomd.StaticWithConfig(echomd.StaticConfig{ + Root: "public", + Index: "index.html", + Browse: false, + HTML5: true, + Filesystem: http.FS(staticFile), + })) + logger.GetZapLogger().Infof("Served the static contents.") } if err := e.Start(":8080"); err != nil {