refactor api package comments

* use encoding/json for comment parsing

* refactor by moving loop code to an UnmarshalJSON

* use a preallocated array and indices to maintain order while using
  goroutines again, this was removed a while ago

* use new struct in comment.hbs and contextComment.hbs

* rewriteUrl partial to reduce rimgo-specific code in api

* move RenderError into pages package to avoid import cycle between render and utils
This commit is contained in:
orangix
2026-01-25 05:08:10 +01:00
parent c208a55f40
commit 02be603dcc
15 changed files with 234 additions and 137 deletions

View File

@@ -1,43 +0,0 @@
package utils
import (
"fmt"
"io"
"net/http"
"strconv"
"codeberg.org/rimgo/rimgo/render"
"codeberg.org/rimgo/rimgo/static"
)
func RenderError(w http.ResponseWriter, r *http.Request, code int, str ...string) (err error) {
if len(str) != 1 {
str = []string{""}
}
codeStr := "generic"
if code == 0 {
code = 500
}
if code != 500 {
codeStr = strconv.Itoa(code)
}
if !Accepts(r, "text/html") && r.PathValue("extension") != "" {
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(code)
file, _ := static.GetFiles().Open("img/error-" + codeStr + ".png")
defer file.Close()
_, err = io.Copy(w, file)
} else {
w.WriteHeader(code)
err = render.Render(w, "errors/"+codeStr, map[string]any{
"path": r.URL.Path,
"err": str[0],
})
}
if err != nil {
// don't panic or return error, it will loop
fmt.Println("error in RenderError: " + err.Error())
}
return nil
}

View File

@@ -1,6 +1,7 @@
package utils
import (
"encoding/json"
"fmt"
"io"
"net/http"
@@ -9,6 +10,42 @@ import (
"github.com/tidwall/gjson"
)
func GetJSONNew(url string) (json.RawMessage, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return json.RawMessage{}, err
}
SetReqHeaders(req)
client := http.Client{}
res, err := client.Do(req)
if err != nil {
return json.RawMessage{}, err
}
rateLimitRemaining := res.Header.Get("X-RateLimit-UserRemaining")
if rateLimitRemaining != "" {
ratelimit, _ := strconv.Atoi(rateLimitRemaining)
if ratelimit <= 0 {
return json.RawMessage{}, fmt.Errorf("ratelimited by imgur")
}
}
body, err := io.ReadAll(res.Body)
if err != nil {
return json.RawMessage{}, err
}
switch res.StatusCode {
case 200:
return body, nil
case 429:
return json.RawMessage{}, fmt.Errorf("ratelimited by imgur")
default:
return json.RawMessage{}, fmt.Errorf("received status %s, expected 200 OK.\n%s", res.Status, string(body))
}
}
func GetJSON(url string) (gjson.Result, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {

25
utils/rewriteUrl.go Normal file
View File

@@ -0,0 +1,25 @@
package utils
import (
"fmt"
"net/url"
"strings"
)
func RewriteUrl(link string) (string, error) {
url, err := url.Parse(link)
if err != nil {
return "", err
}
path := url.Path
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
switch url.Host {
case "", "imgur.com", "www.imgur.com", "i.imgur.com":
return path, nil
case "i.stack.imgur.com":
return "/stack" + path, nil
}
return "", fmt.Errorf("unknown host %s", url.Host)
}