mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-03-30 18:44:15 -04:00
Compare commits
2 Commits
fix-double
...
2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74782230ce | ||
|
|
a44272699f |
3
main.go
3
main.go
@@ -28,9 +28,6 @@ func wrapHandler(h handler) http.Handler {
|
||||
}()
|
||||
err := h(w, r)
|
||||
if err != nil {
|
||||
if render.IsRendererError(err) {
|
||||
return
|
||||
}
|
||||
fmt.Println(err)
|
||||
utils.RenderError(w, r, 500, err.Error())
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ func HandleUserAvatar(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
func handleMedia(w http.ResponseWriter, r *http.Request, url string) error {
|
||||
utils.SetHeaders(w)
|
||||
if !utils.Config.RestrictiveCORS {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
path := r.URL.Path
|
||||
|
||||
if utils.Config.ForceWebp &&
|
||||
@@ -84,6 +87,6 @@ func handleMedia(w http.ResponseWriter, r *http.Request, url string) error {
|
||||
w.Header().Set("Content-Range", res.Header.Get("Content-Range"))
|
||||
}
|
||||
|
||||
_, err = io.Copy(w, res.Body)
|
||||
return err
|
||||
io.Copy(w, res.Body)
|
||||
return nil // don't call RenderError after this point
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -64,46 +63,14 @@ func Initialize(views fs.FS) {
|
||||
func (r *renderer) Render(out io.Writer, name string, bind map[string]any) error {
|
||||
tmpl := r.templates[name]
|
||||
if tmpl == nil {
|
||||
return re(fmt.Errorf("render: template %s does not exist", name))
|
||||
return fmt.Errorf("render: template %s does not exist", name)
|
||||
}
|
||||
parsed, err := tmpl.Exec(bind)
|
||||
if err != nil {
|
||||
return re(fmt.Errorf("render: %w", err))
|
||||
return fmt.Errorf("render: %w", err)
|
||||
}
|
||||
if _, err = out.Write([]byte(parsed)); err != nil {
|
||||
return re(fmt.Errorf("render: %w", err))
|
||||
return fmt.Errorf("render: %w", err)
|
||||
}
|
||||
return re(err)
|
||||
}
|
||||
|
||||
var reSentinel = &struct{}{}
|
||||
|
||||
type RendererError struct {
|
||||
u *struct{}
|
||||
err error
|
||||
}
|
||||
|
||||
func re(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return RendererError{reSentinel, err}
|
||||
}
|
||||
func (re RendererError) Error() string {
|
||||
return re.Error()
|
||||
}
|
||||
func (re RendererError) Unwrap() error {
|
||||
e1, ok := re.err.(interface{ Unwrap() error })
|
||||
if ok {
|
||||
return e1.Unwrap()
|
||||
}
|
||||
e2, ok := re.err.(interface{ Unwrap() []error })
|
||||
if ok {
|
||||
return errors.Join(e2.Unwrap()...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func IsRendererError(err error) bool {
|
||||
re, ok := err.(RendererError)
|
||||
return ok && re.u == reSentinel
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ type config struct {
|
||||
ProtocolDetection bool
|
||||
Secure bool
|
||||
ForceWebp bool
|
||||
RestrictiveCORS bool
|
||||
ImageCache bool
|
||||
CleanupInterval time.Duration
|
||||
CacheDir string
|
||||
@@ -39,6 +40,7 @@ func LoadConfig() {
|
||||
ProtocolDetection: envBool("PROTOCOL_DETECTION"),
|
||||
Secure: envBool("SECURE"),
|
||||
ForceWebp: envBool("FORCE_WEBP"),
|
||||
RestrictiveCORS: envBool("RESTRICTIVE_CORS"),
|
||||
Privacy: map[string]interface{}{
|
||||
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
||||
"policy": os.Getenv("PRIVACY_POLICY"),
|
||||
|
||||
Reference in New Issue
Block a user