Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将前端文件编译到可执行文件中 #556

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package router

import (
"embed"
"net/http"

"github.com/gin-gonic/gin"
Expand All @@ -36,10 +37,14 @@ import (
"github.com/caoyingjunz/pixiu/api/server/router/tenant"
"github.com/caoyingjunz/pixiu/api/server/router/user"
"github.com/caoyingjunz/pixiu/cmd/app/options"
"github.com/caoyingjunz/pixiu/pkg/static"
)

type RegisterFunc func(o *options.Options)

//go:embed static
var EmbedFS embed.FS

func InstallRouters(o *options.Options) {
fs := []RegisterFunc{
middleware.InstallMiddlewares,
Expand All @@ -54,6 +59,7 @@ func InstallRouters(o *options.Options) {

install(o, fs...)

o.HttpEngine.GET("/", static.ServeEmbed("static", EmbedFS))
// 启动健康检查
o.HttpEngine.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
// 启动 APIs 服务
Expand Down
43 changes: 43 additions & 0 deletions api/server/router/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>欢迎使用 Pixiu</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
color: #333;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
text-align: center;
}
.container {
width: 100%; /* 占满整个屏幕宽度 */
height: 100vh; /* 占满整个屏幕高度 */
padding: 20px;
background-color: #fff;
border-radius: 0; /* 移除圆角 */
box-shadow: none; /* 移除阴影 */
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>欢迎使用 Pixiu</h1>
</div>
</body>
</html>
67 changes: 67 additions & 0 deletions pkg/static/embedfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 Pixiu. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:*www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package static

import (
"embed"
"io/fs"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

type embedFileSystem struct {
http.FileSystem
}

func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
return err == nil
}

func EmbedFolder(fsEmbed embed.FS, reqPath string) (ServeFileSystem, error) {
targetPath := strings.TrimSpace(reqPath)
if targetPath == "" {
return embedFileSystem{
FileSystem: http.FS(fsEmbed),
}, nil
}

fsys, _ := fs.Sub(fsEmbed, targetPath)
_, err := fsEmbed.Open(targetPath)
if err != nil {
return nil, err
}

return embedFileSystem{
FileSystem: http.FS(fsys),
}, nil
}

func ServeEmbed(reqPath string, fsEmbed embed.FS) gin.HandlerFunc {
embedFS, err := EmbedFolder(fsEmbed, reqPath)
if err != nil {
return func(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "initialization of embed folder failed",
"error": err.Error(),
})
}
}
return gin.WrapH(http.FileServer(embedFS))
}
52 changes: 52 additions & 0 deletions pkg/static/servefilesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024 The Pixiu Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package static

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

type ServeFileSystem interface {
http.FileSystem
Exists(prefix string, path string) bool
}

// Serve returns a middleware handler that serves static files in the given directory.
func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc {
return ServeCached(urlPrefix, fs, 0)
}

// ServeCached returns a middleware handler that similar as Serve
// but with the Cache-Control Header set as passed in the cacheAge parameter
func ServeCached(urlPrefix string, fs ServeFileSystem, cacheAge uint) gin.HandlerFunc {
fileserver := http.FileServer(fs)
if urlPrefix != "" {
fileserver = http.StripPrefix(urlPrefix, fileserver)
}
return func(c *gin.Context) {
if fs.Exists(urlPrefix, c.Request.URL.Path) {
if cacheAge != 0 {
c.Writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d", cacheAge))
}
fileserver.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
}
}
Loading