mirror of
https://git.macaw.me/skunky/SkunkyArt.git
synced 2025-04-28 12:05:06 +03:00
альфа: работают только посты и поиск
This commit is contained in:
commit
6f872fe2f7
12 changed files with 544 additions and 0 deletions
81
app/router.go
Normal file
81
app/router.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
u "net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const addr string = "0.0.0.0:3003"
|
||||
|
||||
// роутер
|
||||
func Router() {
|
||||
// расшифровка эндпоинта из урл
|
||||
endpoint := func(url string) (string, string) {
|
||||
if CFG.Base_uri != "" {
|
||||
url = strings.Replace(url, CFG.Base_uri, "/", 1)
|
||||
}
|
||||
|
||||
end := strings.Index(url[1:], "/")
|
||||
if end == -1 {
|
||||
return url[1:], ""
|
||||
}
|
||||
return url[1 : end+1], url[end+2:]
|
||||
}
|
||||
|
||||
// функция, что управляет всем
|
||||
handle := func(w http.ResponseWriter, r *http.Request) {
|
||||
e, url := endpoint(r.URL.Path)
|
||||
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
|
||||
|
||||
arg := skunky.Args.Get
|
||||
skunky.Query = u.QueryEscape(arg("q"))
|
||||
if t := arg("type"); len(t) > 0 {
|
||||
skunky.Type = rune(t[0])
|
||||
}
|
||||
p, _ := strconv.Atoi(arg("p"))
|
||||
skunky.Page = p
|
||||
|
||||
// пути
|
||||
switch e {
|
||||
default:
|
||||
skunky.httperr(404)
|
||||
case "/", "":
|
||||
open_n_send("html/index.htm")
|
||||
case "post":
|
||||
slash := strings.Index(url, "/")
|
||||
skunky.Deviation(url[:slash], url[slash+1:])
|
||||
case "search":
|
||||
skunky.Search()
|
||||
case "media":
|
||||
skunky.Emojitar(url)
|
||||
case "about":
|
||||
open_n_send("html/about.htm")
|
||||
case "gui":
|
||||
w.Header().Add("content-type", "text/css")
|
||||
open_n_send(url)
|
||||
}
|
||||
}
|
||||
|
||||
http.HandleFunc("/", handle)
|
||||
http.ListenAndServe(addr, nil)
|
||||
}
|
||||
|
||||
func err(e error) {
|
||||
if e != nil {
|
||||
println(e.Error())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue