SkunkyArt/app/router.go

108 lines
2.1 KiB
Go
Raw Normal View History

package app
import (
"io"
"net/http"
u "net/url"
"strconv"
"strings"
)
2024-07-13 18:32:04 +00:00
var Host string
func Router() {
parsepath := func(path string) map[int]string {
2024-07-05 21:46:25 +00:00
if l := len(CFG.BasePath); len(path) > l {
path = path[l-1:]
} else {
path = "/"
}
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
}
// функция, что управляет всем
handle := func(w http.ResponseWriter, r *http.Request) {
2024-07-13 18:32:04 +00:00
if h := r.Header["Scheme"]; len(h) != 0 && h[0] == "https" {
Host = h[0] + "://" + r.Host
} else {
Host = "http://" + r.Host
}
2024-07-13 18:32:04 +00:00
path := parsepath(r.URL.Path)
// структура с функциями
var skunky skunkyart
skunky.Writer = w
2024-07-13 18:32:04 +00:00
skunky.Args = r.URL.Query()
2024-07-05 21:46:25 +00:00
skunky.BasePath = CFG.BasePath
arg := skunky.Args.Get
2024-07-05 21:46:25 +00:00
skunky.QueryRaw = arg("q")
skunky.Query = u.QueryEscape(skunky.QueryRaw)
if t := arg("type"); len(t) > 0 {
skunky.Type = rune(t[0])
}
p, _ := strconv.Atoi(arg("p"))
skunky.Page = p
2024-07-05 21:46:25 +00:00
if arg("atom") == "true" {
2024-07-05 21:46:25 +00:00
skunky.Atom = true
}
// пути
switch path[1] {
default:
skunky.ReturnHTTPError(404)
case "":
2024-07-05 21:46:25 +00:00
skunky.ExecuteTemplate("html/index.htm", &CFG.BasePath)
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()
case "media":
2024-07-05 21:46:25 +00:00
switch path[2] {
case "file":
skunky.DownloadAndSendMedia(path[3], next(path, 4))
case "emojitar":
skunky.Emojitar(path[3])
}
case "about":
2024-07-05 21:46:25 +00:00
skunky.About()
2024-07-13 18:32:04 +00:00
case "stylesheet":
w.Header().Add("content-type", "text/css")
2024-07-13 18:32:04 +00:00
io.WriteString(w, Templates["css/skunky.css"])
}
}
http.HandleFunc("/", handle)
2024-07-05 21:46:25 +00:00
http.ListenAndServe(CFG.Listen, nil)
}