SkunkyArt/main.go

109 lines
2.3 KiB
Go

package main
import (
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
"skunkyart/misc"
"skunkyart/post"
"skunkyart/user"
"skunkyart/util"
)
const Compiler = "gccgo"
func main() {
util.ParseConfig()
print("Getting CSRF Token..\033[B\r")
util.CSRFToken()
print("CSRF Token has been getted.\033[B\r\n")
go func() {
for {
time.Sleep(30 * time.Minute)
util.CSRFToken()
println("CSRF token has been updated.")
}
}()
println("SkunkyArt listening on http://" + util.Conf.Listen)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
e, p := endpoint(r.URL.Path)
c := make(chan string, 100)
switch e {
case "image":
fmt.Fprintln(w, misc.Getimage(p, url.QueryEscape(r.URL.Query().Get("t"))))
case "avatar":
if p == "" {
w.WriteHeader(400)
fmt.Fprint(w, "Missing username.")
} else {
fmt.Fprintln(w, misc.Misc(strings.ToLower(p), false))
}
case "emoji":
fmt.Fprintln(w, misc.Misc(strings.ToLower(p), true))
case "group":
fmt.Fprintln(w, "Groups is not implemented yet :(")
case "user":
fmt.Fprintln(w, user.Get(p, r.URL.Query().Get("a"), r.URL.Query().Get("p"), url.QueryEscape(r.URL.Query().Get("q"))))
case "post":
intt, _ := strconv.ParseInt(r.URL.Query().Get("page"), 10, 32)
go post.Get(p, int(intt)+1, c)
fmt.Fprintln(w, <-c)
case "dd":
intt, _ := strconv.ParseInt(r.URL.Query().Get("p"), 10, 32)
go misc.DD(int(intt), c)
fmt.Fprintln(w, <-c)
case "search":
go misc.Search(url.QueryEscape(r.URL.Query().Get("q")), r.URL.Query().Get("p"), r.URL.Query().Get("scope"), c)
fmt.Fprintln(w, <-c)
case "about":
fmt.Fprintln(w, "Its an alternative frontend for deviantart.com. Compiled with Go:", runtime.Version())
case "static":
file, err := os.ReadFile("static/" + p)
if err != nil {
fmt.Fprintln(w, "No such file")
} else {
w.Header().Add("Content-Type", "text/css")
fmt.Fprintln(w, string(file))
}
case "":
f, _ := os.ReadFile("templates/home.htm")
fmt.Fprintln(w, string(f))
default:
w.WriteHeader(404)
fmt.Fprintln(w, "404 - Not found.")
}
})
http.ListenAndServe(util.Conf.Listen, nil)
}
func endpoint(url string) (string, string) {
end := strings.Index(url[1:], "/")
if end == -1 {
return url[1:], ""
}
return url[1 : end+1], url[end+2:]
}