Skip to content

Commit

Permalink
Embedded the static files
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Jul 17, 2022
1 parent 0f1cbd6 commit 92df056
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion application.develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ log:
request_log_format: ${remote_ip} ${account_name} ${uri} ${method} ${status}

staticcontents:
path: ./public/
enabled: true

security:
auth_path:
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 92df056

Please sign in to comment.