mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 04:17:23 +00:00
serve frontend
This commit is contained in:
@@ -22,6 +22,10 @@ type ServerConfig struct {
|
||||
CloudflareTurnstileSiteKey string `json:"cloudflare_turnstile_site_key"`
|
||||
// CloudflareTurnstileSecretKey is the secret key for Cloudflare Turnstile.
|
||||
CloudflareTurnstileSecretKey string `json:"cloudflare_turnstile_secret_key"`
|
||||
|
||||
ServerName string `json:"server_name"`
|
||||
|
||||
ServerDescription string `json:"server_description"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -34,6 +38,8 @@ func init() {
|
||||
AllowRegister: true,
|
||||
CloudflareTurnstileSiteKey: "",
|
||||
CloudflareTurnstileSecretKey: "",
|
||||
ServerName: "Nysoure",
|
||||
ServerDescription: "Nysoure is a file sharing service.",
|
||||
}
|
||||
} else {
|
||||
data, err := os.ReadFile(filepath)
|
||||
@@ -78,3 +84,15 @@ func AllowRegister() bool {
|
||||
func MaxDownloadsPerDayForSingleIP() int {
|
||||
return config.MaxDownloadsPerDayForSingleIP
|
||||
}
|
||||
|
||||
func CloudflareTurnstileSiteKey() string {
|
||||
return config.CloudflareTurnstileSiteKey
|
||||
}
|
||||
|
||||
func ServerName() string {
|
||||
return config.ServerName
|
||||
}
|
||||
|
||||
func ServerDescription() string {
|
||||
return config.ServerDescription
|
||||
}
|
||||
|
106
server/middleware/frontend_middleware.go
Normal file
106
server/middleware/frontend_middleware.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nysoure/server/config"
|
||||
"nysoure/server/service"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/k3a/html2text"
|
||||
)
|
||||
|
||||
func FrontendMiddleware(c fiber.Ctx) error {
|
||||
if strings.HasPrefix(c.Path(), "/api") {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
path := c.Path()
|
||||
file := "static" + path
|
||||
|
||||
if _, err := os.Stat(file); path == "/" || os.IsNotExist(err) {
|
||||
return serveIndexHtml(c)
|
||||
} else {
|
||||
return c.SendFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
func serveIndexHtml(c fiber.Ctx) error {
|
||||
data, err := os.ReadFile("static/index.html")
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||||
}
|
||||
content := string(data)
|
||||
|
||||
siteName := config.ServerName()
|
||||
description := config.ServerDescription()
|
||||
preview := "/icon-192.png"
|
||||
title := siteName
|
||||
url := c.OriginalURL()
|
||||
cfTurnstileSiteKey := config.CloudflareTurnstileSiteKey()
|
||||
|
||||
if strings.HasPrefix(url, "/resources/") {
|
||||
idStr := strings.TrimPrefix(url, "/resources/")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err == nil {
|
||||
r, err := service.GetResource(uint(id))
|
||||
if err == nil {
|
||||
if len(r.Images) > 0 {
|
||||
preview = fmt.Sprintf("/images/%d", r.Images[0].ID)
|
||||
}
|
||||
title = r.Title
|
||||
description = getResourceDescription(r.Article)
|
||||
}
|
||||
}
|
||||
} else if strings.HasPrefix(url, "/user/") {
|
||||
username := strings.TrimPrefix(url, "/user/")
|
||||
u, err := service.GetUserByUsername(username)
|
||||
if err == nil {
|
||||
preview = fmt.Sprintf("/avatar/%d", u.ID)
|
||||
title = u.Username
|
||||
description = "User " + u.Username + "'s profile"
|
||||
}
|
||||
}
|
||||
|
||||
content = strings.ReplaceAll(content, "{{SiteName}}", siteName)
|
||||
content = strings.ReplaceAll(content, "{{Description}}", description)
|
||||
content = strings.ReplaceAll(content, "{{Preview}}", preview)
|
||||
content = strings.ReplaceAll(content, "{{Title}}", title)
|
||||
content = strings.ReplaceAll(content, "{{Url}}", url)
|
||||
content = strings.ReplaceAll(content, "{{CFTurnstileSiteKey}}", cfTurnstileSiteKey)
|
||||
|
||||
c.Set("Content-Type", "text/html; charset=utf-8")
|
||||
return c.SendString(content)
|
||||
}
|
||||
|
||||
func getResourceDescription(article string) string {
|
||||
htmlContent := mdToHTML([]byte(article))
|
||||
plain := html2text.HTML2Text(string(htmlContent))
|
||||
if len([]rune(plain)) > 100 {
|
||||
plain = string([]rune(plain)[:100])
|
||||
}
|
||||
plain = strings.ReplaceAll(plain, "\n", " ")
|
||||
plain = strings.ReplaceAll(plain, "\r", "")
|
||||
plain = strings.ReplaceAll(plain, "\t", "")
|
||||
plain = strings.TrimSpace(plain)
|
||||
return plain
|
||||
}
|
||||
|
||||
func mdToHTML(md []byte) []byte {
|
||||
// create Markdown parser with extensions
|
||||
extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock | parser.MathJax
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
doc := p.Parse(md)
|
||||
|
||||
// create HTML renderer with extensions
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||
opts := html.RendererOptions{Flags: htmlFlags}
|
||||
renderer := html.NewRenderer(opts)
|
||||
|
||||
return markdown.Render(doc, renderer)
|
||||
}
|
Reference in New Issue
Block a user