router.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package app
  2. import (
  3. "io"
  4. "net/http"
  5. url "net/url"
  6. "skunkyart/static"
  7. "strconv"
  8. "strings"
  9. )
  10. var Host string
  11. func Router() {
  12. parsepath := func(path string) map[int]string {
  13. if l := len(CFG.URI); len(path) > l {
  14. path = path[l-1:]
  15. } else {
  16. path = "/"
  17. }
  18. parsedpath := make(map[int]string)
  19. for x := 0; true; x++ {
  20. slash := strings.Index(path, "/") + 1
  21. content := path[:slash]
  22. path = path[slash:]
  23. if slash == 0 {
  24. parsedpath[x] = path
  25. break
  26. }
  27. parsedpath[x] = content[:slash-1]
  28. }
  29. return parsedpath
  30. }
  31. next := func(path map[int]string, from int) (out string) {
  32. for x, l := from, len(path)-1; x <= l; x++ {
  33. out += path[x]
  34. if x != l {
  35. out += "/"
  36. }
  37. }
  38. return
  39. }
  40. open := func(name string) []byte {
  41. file, err := static.Templates.Open(name)
  42. try(err)
  43. fileReaded, err := io.ReadAll(file)
  44. try(err)
  45. return fileReaded
  46. }
  47. // функция, что управляет всем
  48. handle := func(w http.ResponseWriter, r *http.Request) {
  49. path := parsepath(r.URL.Path)
  50. Host = "http://" + r.Host
  51. if h := r.Header["X-Forwarded-Proto"]; len(h) != 0 && h[0] == "https" {
  52. Host = "https://" + r.Host
  53. }
  54. var skunky = skunkyart{Version: Release.Version}
  55. skunky._pth = r.URL.Path
  56. skunky.Args = r.URL.Query()
  57. arg := skunky.Args.Get
  58. p, _ := strconv.Atoi(arg("p"))
  59. skunky.Endpoint = path[1]
  60. skunky.API.main = &skunky
  61. skunky.Writer = w
  62. skunky.BasePath = CFG.URI
  63. skunky.QueryRaw = arg("q")
  64. skunky.Query = url.QueryEscape(skunky.QueryRaw)
  65. skunky.Page = p
  66. if t := arg("type"); len(t) > 0 {
  67. skunky.Type = rune(t[0])
  68. }
  69. if arg("atom") == "true" {
  70. skunky.Atom = true
  71. }
  72. if CFG.Proxy {
  73. w.Header().Add("Content-Security-Policy", "default-src 'self'; script-src 'none'; style-src 'self' 'unsafe-inline'")
  74. } else {
  75. w.Header().Add("Content-Security-Policy", "default-src 'self'; img-src 'self' *.wixmp.com; script-src 'none'; style-src 'self' 'unsafe-inline'")
  76. }
  77. w.Header().Add("X-Frame-Options", "DENY")
  78. switch skunky.Endpoint {
  79. // main
  80. case "":
  81. skunky.ExecuteTemplate("index.htm", "html", &CFG.URI)
  82. case "about":
  83. skunky.Templates.About = About
  84. skunky.ExecuteTemplate("about.htm", "html", &skunky)
  85. case "post":
  86. skunky.Deviation(path[2], path[3])
  87. case "search":
  88. skunky.Search()
  89. case "dd":
  90. skunky.DD()
  91. case "group_user":
  92. skunky.GRUser()
  93. // media
  94. case "media":
  95. switch path[2] {
  96. case "file":
  97. if a := arg("filename"); a != "" {
  98. skunky.SetFilename(a)
  99. }
  100. skunky.DownloadAndSendMedia(path[3], next(path, 4))
  101. case "emojitar":
  102. skunky.Emojitar(path[3])
  103. }
  104. case "stylesheet":
  105. w.Header().Add("content-type", "text/css")
  106. w.Write(open("css/skunky.css"))
  107. case "favicon.ico":
  108. w.Write(open("images/logo.png"))
  109. // API
  110. case "api":
  111. w.Header().Add("Content-Type", "application/json")
  112. switch path[2] {
  113. case "instance":
  114. skunky.API.Info()
  115. case "random":
  116. skunky.API.Random()
  117. default:
  118. skunky.API.Error("Not Found", 404)
  119. }
  120. // 404
  121. default:
  122. skunky.ReturnHTTPError(404)
  123. }
  124. }
  125. http.HandleFunc("/", handle)
  126. println("SkunkyArt is listening on", CFG.Listen)
  127. tryWithExitStatus(http.ListenAndServe(CFG.Listen, nil), 1)
  128. }