SkunkyArt/app/router.go

150 lines
3.2 KiB
Go
Raw Normal View History

package app
import (
"io"
"net/http"
2024-09-23 06:38:32 +00:00
url "net/url"
"skunkyart/static"
"strconv"
"strings"
)
2024-11-15 19:43:27 +00:00
var Host string
func Router() {
parsepath := func(path string) map[int]string {
2024-08-01 19:48:05 +00:00
if l := len(CFG.URI); len(path) > l {
2024-07-05 21:46:25 +00:00
path = path[l-1:]
} else {
path = "/"
2024-07-05 21:46:25 +00:00
}
parsedpath := make(map[int]string)
for x := 0; true; x++ {
slash := strings.Index(path, "/") + 1
content := path[:slash]
path = path[slash:]
if slash == 0 {
parsedpath[x] = path
break
}
parsedpath[x] = content[:slash-1]
}
return parsedpath
}
next := func(path map[int]string, from int) (out string) {
for x, l := from, len(path)-1; x <= l; x++ {
out += path[x]
if x != l {
out += "/"
}
}
return
}
open := func(name string) []byte {
file, err := static.Templates.Open(name)
try(err)
fileReaded, err := io.ReadAll(file)
try(err)
return fileReaded
}
// функция, что управляет всем
handle := func(w http.ResponseWriter, r *http.Request) {
2024-11-15 19:43:27 +00:00
path := parsepath(r.URL.Path)
2024-09-03 12:36:19 +00:00
Host = "http://" + r.Host
if h := r.Header["X-Forwarded-Proto"]; len(h) != 0 && h[0] == "https" {
2024-09-03 12:36:19 +00:00
Host = "https://" + r.Host
}
2024-09-03 12:36:19 +00:00
var skunky = skunkyart{Version: Release.Version}
2024-11-15 19:43:27 +00:00
skunky._pth = r.URL.Path
2024-09-03 12:36:19 +00:00
2024-09-04 17:22:58 +00:00
skunky.Args = r.URL.Query()
2024-09-03 12:36:19 +00:00
arg := skunky.Args.Get
p, _ := strconv.Atoi(arg("p"))
2024-09-23 06:38:32 +00:00
2024-09-03 12:36:19 +00:00
skunky.Endpoint = path[1]
skunky.API.main = &skunky
skunky.Writer = w
2024-08-01 19:48:05 +00:00
skunky.BasePath = CFG.URI
2024-07-05 21:46:25 +00:00
skunky.QueryRaw = arg("q")
2024-09-23 06:38:32 +00:00
skunky.Query = url.QueryEscape(skunky.QueryRaw)
2024-09-03 12:36:19 +00:00
skunky.Page = p
2024-07-05 21:46:25 +00:00
if t := arg("type"); len(t) > 0 {
skunky.Type = rune(t[0])
}
2024-07-05 21:46:25 +00:00
if arg("atom") == "true" {
2024-07-05 21:46:25 +00:00
skunky.Atom = true
}
2024-09-23 06:38:32 +00:00
if CFG.Proxy {
w.Header().Add("Content-Security-Policy", "default-src 'self'; script-src 'none'; style-src 'self' 'unsafe-inline'")
} else {
w.Header().Add("Content-Security-Policy", "default-src 'self'; img-src 'self' *.wixmp.com; script-src 'none'; style-src 'self' 'unsafe-inline'")
}
w.Header().Add("X-Frame-Options", "DENY")
switch skunky.Endpoint {
2024-09-03 12:36:19 +00:00
// main
case "":
2024-09-02 08:16:54 +00:00
skunky.ExecuteTemplate("index.htm", "html", &CFG.URI)
2024-09-03 12:36:19 +00:00
case "about":
2024-09-23 06:38:32 +00:00
skunky.Templates.About = About
skunky.ExecuteTemplate("about.htm", "html", &skunky)
case "post":
skunky.Deviation(path[2], path[3])
case "search":
skunky.Search()
case "dd":
skunky.DD()
case "group_user":
2024-06-27 11:51:46 +00:00
skunky.GRUser()
2024-09-03 12:36:19 +00:00
// media
case "media":
2024-07-05 21:46:25 +00:00
switch path[2] {
case "file":
if a := arg("filename"); a != "" {
2024-09-03 12:36:19 +00:00
skunky.SetFilename(a)
}
2024-07-05 21:46:25 +00:00
skunky.DownloadAndSendMedia(path[3], next(path, 4))
case "emojitar":
skunky.Emojitar(path[3])
}
2024-07-13 18:32:04 +00:00
case "stylesheet":
w.Header().Add("content-type", "text/css")
w.Write(open("css/skunky.css"))
case "favicon.ico":
w.Write(open("images/logo.png"))
2024-09-02 08:16:54 +00:00
2024-09-03 12:36:19 +00:00
// API
2024-09-02 08:16:54 +00:00
case "api":
2024-09-03 12:36:19 +00:00
w.Header().Add("Content-Type", "application/json")
2024-09-02 08:16:54 +00:00
switch path[2] {
2024-09-23 06:38:32 +00:00
case "instance":
skunky.API.Info()
case "random":
skunky.API.Random()
default:
skunky.API.Error("Not Found", 404)
2024-09-02 08:16:54 +00:00
}
2024-09-03 12:36:19 +00:00
2024-09-04 17:22:58 +00:00
// 404
default:
skunky.ReturnHTTPError(404)
}
}
http.HandleFunc("/", handle)
println("SkunkyArt is listening on", CFG.Listen)
tryWithExitStatus(http.ListenAndServe(CFG.Listen, nil), 1)
}