pixivfe/main.go

180 lines
3.2 KiB
Go
Raw Normal View History

2023-05-13 03:34:31 +00:00
package main
import (
2023-06-05 06:00:27 +00:00
"fmt"
2023-05-13 12:25:32 +00:00
"html/template"
2023-05-18 13:18:44 +00:00
"pixivfe/configs"
2023-05-16 12:35:39 +00:00
"pixivfe/views"
"regexp"
"strconv"
2023-06-04 04:03:05 +00:00
"strings"
2023-05-13 03:34:31 +00:00
"github.com/gin-gonic/gin"
)
2023-05-16 12:35:39 +00:00
func setupRouter() *gin.Engine {
server := gin.Default()
2023-05-16 12:35:39 +00:00
2023-05-13 12:25:32 +00:00
server.SetFuncMap(template.FuncMap{
2023-05-16 12:35:39 +00:00
"inc": func(n int) int {
// For rankings to increment a number by 1
2023-05-16 12:35:39 +00:00
return n + 1
},
"add": func(a int, b int) int {
return a + b
},
"dec": func(n int) int {
return n - 1
},
2023-06-04 13:49:33 +00:00
"toInt": func(s string) int {
n, _ := strconv.Atoi(s)
return n
},
"proxyImage": func(url string) string {
2023-06-04 04:03:05 +00:00
if strings.Contains(url, "s.pximg.net") {
// This subdomain didn't get proxied
return url
}
regex := regexp.MustCompile(`.*?pximg\.net`)
proxy := "https://" + configs.ProxyServer
return regex.ReplaceAllString(url, proxy)
},
2023-06-05 06:00:27 +00:00
"parseEmojis": func(s string) template.HTML {
regex := regexp.MustCompile(`\(([^)]+)\)`)
parsedString := regex.ReplaceAllStringFunc(s, func(s string) string {
s = s[1 : len(s)-1] // Get the string inside
var id string
switch s {
case "normal":
id = "101"
case "surprise":
id = "102"
case "serious":
id = "103"
case "heaven":
id = "104"
case "happy":
id = "105"
case "excited":
id = "106"
case "sing":
id = "107"
case "cry":
id = "108"
case "normal2":
id = "201"
case "shame2":
id = "202"
case "love2":
id = "203"
case "interesting2":
id = "204"
case "blush2":
id = "205"
case "fire2":
id = "206"
case "angry2":
id = "207"
case "shine2":
id = "208"
case "panic2":
id = "209"
case "normal3":
id = "301"
case "satisfaction3":
id = "302"
case "surprise3":
id = "303"
case "smile3":
id = "304"
case "shock3":
id = "305"
case "gaze3":
id = "306"
case "wink3":
id = "307"
case "happy3":
id = "308"
case "excited3":
id = "309"
case "love3":
id = "310"
case "normal4":
id = "401"
case "surprise4":
id = "402"
case "serious4":
id = "403"
case "love4":
id = "404"
case "shine4":
id = "405"
case "sweat4":
id = "406"
case "shame4":
id = "407"
case "sleep4":
id = "408"
case "heart":
id = "501"
case "teardrop":
id = "502"
case "star":
id = "503"
}
return fmt.Sprintf(`<img src="https://s.pximg.net/common/images/emoji/%s.png" alt="(%s)" class="emoji" />`, id, s)
})
return template.HTML(parsedString)
},
"isEmpty": func(s string) bool {
return len(s) < 1
},
"isEmphasize": func(s string) bool {
switch s {
case
"R-18",
"R-18G":
return true
}
return false
},
2023-05-13 12:25:32 +00:00
})
2023-05-16 12:35:39 +00:00
// Static files
2023-05-14 12:30:03 +00:00
server.StaticFile("/favicon.ico", "./template/favicon.ico")
server.Static("css/", "./template/css")
2023-06-02 03:19:27 +00:00
server.Static("assets/", "./template/assets")
2023-05-16 12:35:39 +00:00
// HTML templates, automatically loaded
server.LoadHTMLGlob("template/*.html")
2023-05-16 12:35:39 +00:00
// Routes/Views
views.SetupRoutes(server)
// Disable trusted proxies since we do not use any for now
server.SetTrustedProxies(nil)
2023-05-16 12:35:39 +00:00
return server
}
func main() {
err := configs.ParseConfig()
if err != nil {
panic(err)
}
2023-05-18 13:18:44 +00:00
2023-05-16 12:35:39 +00:00
r := setupRouter()
r.Run(":" + configs.Port)
2023-05-13 03:34:31 +00:00
}