2024-06-13 20:56:03 +00:00
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
u "net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Router() {
|
2024-07-04 08:26:52 +00:00
|
|
|
|
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 = "/"
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 08:26:52 +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]
|
2024-06-13 20:56:03 +00:00
|
|
|
|
}
|
2024-07-04 08:26:52 +00:00
|
|
|
|
return parsedpath
|
|
|
|
|
}
|
2024-06-13 20:56:03 +00:00
|
|
|
|
|
2024-07-04 08:26:52 +00:00
|
|
|
|
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 += "/"
|
|
|
|
|
}
|
2024-06-13 20:56:03 +00:00
|
|
|
|
}
|
2024-07-04 08:26:52 +00:00
|
|
|
|
return
|
2024-06-13 20:56:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// функция, что управляет всем
|
|
|
|
|
handle := func(w http.ResponseWriter, r *http.Request) {
|
2024-07-04 08:26:52 +00:00
|
|
|
|
path := parsepath(r.URL.Path)
|
2024-06-13 20:56:03 +00:00
|
|
|
|
var wr = io.WriteString
|
|
|
|
|
open_n_send := func(name string) {
|
|
|
|
|
f, e := os.ReadFile(name)
|
|
|
|
|
err(e)
|
|
|
|
|
wr(w, string(f))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// структура с функциями
|
|
|
|
|
var skunky skunkyart
|
|
|
|
|
skunky.Args = r.URL.Query()
|
|
|
|
|
skunky.Writer = w
|
2024-07-05 21:46:25 +00:00
|
|
|
|
skunky.BasePath = CFG.BasePath
|
2024-06-13 20:56:03 +00:00
|
|
|
|
|
|
|
|
|
arg := skunky.Args.Get
|
2024-07-05 21:46:25 +00:00
|
|
|
|
skunky.QueryRaw = arg("q")
|
|
|
|
|
skunky.Query = u.QueryEscape(skunky.QueryRaw)
|
|
|
|
|
|
2024-06-13 20:56:03 +00:00
|
|
|
|
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
|
|
|
|
|
2024-07-05 08:08:32 +00:00
|
|
|
|
if arg("atom") == "true" {
|
2024-07-05 21:46:25 +00:00
|
|
|
|
skunky.Atom = true
|
2024-07-05 08:08:32 +00:00
|
|
|
|
}
|
2024-06-13 20:56:03 +00:00
|
|
|
|
|
|
|
|
|
// пути
|
2024-07-04 08:26:52 +00:00
|
|
|
|
switch path[1] {
|
2024-06-13 20:56:03 +00:00
|
|
|
|
default:
|
2024-06-30 11:40:27 +00:00
|
|
|
|
skunky.ReturnHTTPError(404)
|
2024-07-04 08:26:52 +00:00
|
|
|
|
case "":
|
2024-07-05 21:46:25 +00:00
|
|
|
|
skunky.ExecuteTemplate("html/index.htm", &CFG.BasePath)
|
2024-06-13 20:56:03 +00:00
|
|
|
|
case "post":
|
2024-07-04 08:26:52 +00:00
|
|
|
|
skunky.Deviation(path[2], path[3])
|
2024-06-13 20:56:03 +00:00
|
|
|
|
case "search":
|
|
|
|
|
skunky.Search()
|
2024-06-14 17:04:23 +00:00
|
|
|
|
case "dd":
|
|
|
|
|
skunky.DD()
|
2024-06-30 11:40:27 +00:00
|
|
|
|
case "group_user":
|
2024-06-27 11:51:46 +00:00
|
|
|
|
skunky.GRUser()
|
|
|
|
|
|
2024-06-13 20:56:03 +00:00
|
|
|
|
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])
|
|
|
|
|
}
|
2024-06-13 20:56:03 +00:00
|
|
|
|
case "about":
|
2024-07-05 21:46:25 +00:00
|
|
|
|
skunky.About()
|
2024-06-13 20:56:03 +00:00
|
|
|
|
case "gui":
|
|
|
|
|
w.Header().Add("content-type", "text/css")
|
2024-07-04 08:26:52 +00:00
|
|
|
|
open_n_send(next(path, 2))
|
2024-06-13 20:56:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", handle)
|
2024-07-05 21:46:25 +00:00
|
|
|
|
http.ListenAndServe(CFG.Listen, nil)
|
2024-06-13 20:56:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func err(e error) {
|
|
|
|
|
if e != nil {
|
|
|
|
|
println(e.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|