36 lines
867 B
Go
36 lines
867 B
Go
package static
|
|
|
|
import (
|
|
"embed"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// GetStatic returns static file content
|
|
func GetStatic(staticfiles *embed.FS, c echo.Context) (err error) {
|
|
name := c.Param("*")
|
|
content, err := staticfiles.ReadFile(name)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
if strings.HasSuffix(name, ".js") {
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript)
|
|
} else if strings.HasSuffix(name, ".css") {
|
|
c.Response().Header().Set(echo.HeaderContentType, "text/css")
|
|
} else if strings.HasSuffix(name, ".ico") {
|
|
c.Response().Header().Set(echo.HeaderContentType, "image/x-icon")
|
|
}
|
|
c.Response().Header().Add(echo.HeaderCacheControl, "max-age=172800")
|
|
return c.String(http.StatusOK, string(content))
|
|
}
|
|
|
|
// Static defines static file
|
|
type Static struct {
|
|
Name string
|
|
Content string
|
|
}
|