-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
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
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
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> |
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
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)) | ||
} |
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
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() | ||
} | ||
} | ||
} |