refactor: replace custom array search utility with slices.Contains for improved readability and performance.

This commit is contained in:
Miroslav Šedivý
2026-08-04 12:49:38 +02:00
parent d2ec13a236
commit db8452416b
6 changed files with 18 additions and 32 deletions
+8 -11
View File
@@ -2,12 +2,11 @@ package config
import ( import (
"path" "path"
"slices"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/m1k1o/neko/server/pkg/utils"
) )
type Server struct { type Server struct {
@@ -121,8 +120,7 @@ func (s *Server) Set() {
s.Metrics = viper.GetBool("server.metrics") s.Metrics = viper.GetBool("server.metrics")
s.CORS = viper.GetStringSlice("server.cors") s.CORS = viper.GetStringSlice("server.cors")
in, _ := utils.ArrayIn("*", s.CORS) if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") {
if len(s.CORS) == 0 || in {
s.CORS = []string{"*"} s.CORS = []string{"*"}
} }
} }
@@ -162,8 +160,7 @@ func (s *Server) SetV2() {
} }
if viper.IsSet("cors") { if viper.IsSet("cors") {
s.CORS = viper.GetStringSlice("cors") s.CORS = viper.GetStringSlice("cors")
in, _ := utils.ArrayIn("*", s.CORS) if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") {
if len(s.CORS) == 0 || in {
s.CORS = []string{"*"} s.CORS = []string{"*"}
} }
log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead") log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead")
@@ -182,12 +179,12 @@ func (s *Server) HasCors() bool {
} }
func (s *Server) AllowOrigin(origin string) bool { func (s *Server) AllowOrigin(origin string) bool {
// if CORS is disabled, allow all origins // if CORS is not configured, or a wildcard is present,
if len(s.CORS) == 0 { // allow WebSocket connections from any origin
if len(s.CORS) == 0 || s.CORS[0] == "*" {
return true return true
} }
// if CORS is enabled, allow only origins in the list // allow only origins in the list
in, _ := utils.ArrayIn(origin, s.CORS) return slices.Contains(s.CORS, origin)
return in || s.CORS[0] == "*"
} }
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
"slices"
"strings" "strings"
"github.com/m1k1o/neko/server/pkg/types" "github.com/m1k1o/neko/server/pkg/types"
@@ -55,7 +56,7 @@ func (b *batchHandler) Handle(w http.ResponseWriter, r *http.Request) error {
continue continue
} }
if exists, _ := utils.ArrayIn(request.Path, b.Excluded); exists { if slices.Contains(b.Excluded, request.Path) {
res.Error(utils.HttpBadRequest("this path is excluded from batch requests")) res.Error(utils.HttpBadRequest("this path is excluded from batch requests"))
responses[i] = res responses[i] = res
continue continue
+4 -3
View File
@@ -2,6 +2,7 @@ package session
import ( import (
"errors" "errors"
"slices"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -303,7 +304,7 @@ func (manager *SessionManagerCtx) Broadcast(event string, payload any, exclude .
} }
if len(exclude) > 0 { if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in { if slices.Contains(exclude, session.ID()) {
continue continue
} }
} }
@@ -319,7 +320,7 @@ func (manager *SessionManagerCtx) AdminBroadcast(event string, payload any, excl
} }
if len(exclude) > 0 { if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in { if slices.Contains(exclude, session.ID()) {
continue continue
} }
} }
@@ -335,7 +336,7 @@ func (manager *SessionManagerCtx) InactiveCursorsBroadcast(event string, payload
} }
if len(exclude) > 0 { if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in { if slices.Contains(exclude, session.ID()) {
continue continue
} }
} }
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"net/http" "net/http"
"slices"
"sync" "sync"
"time" "time"
@@ -333,7 +334,7 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer type
} }
// log events if not ignored // log events if not ignored
if ok, _ := utils.ArrayIn(data.Event, nologEvents); !ok { if !slices.Contains(nologEvents, data.Event) {
payload := data.Payload payload := data.Payload
if len(payload) > maxPayloadLogLength { if len(payload) > maxPayloadLogLength {
payload = []byte("<truncated>") payload = []byte("<truncated>")
+2 -2
View File
@@ -3,6 +3,7 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"slices"
"sync" "sync"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@@ -11,7 +12,6 @@ import (
"github.com/m1k1o/neko/server/pkg/types" "github.com/m1k1o/neko/server/pkg/types"
"github.com/m1k1o/neko/server/pkg/types/event" "github.com/m1k1o/neko/server/pkg/types/event"
"github.com/m1k1o/neko/server/pkg/types/message" "github.com/m1k1o/neko/server/pkg/types/message"
"github.com/m1k1o/neko/server/pkg/utils"
) )
type WebSocketPeerCtx struct { type WebSocketPeerCtx struct {
@@ -51,7 +51,7 @@ func (peer *WebSocketPeerCtx) Send(event string, payload any) {
} }
// log events if not ignored // log events if not ignored
if ok, _ := utils.ArrayIn(event, nologEvents); !ok { if !slices.Contains(nologEvents, event) {
if len(raw) > maxPayloadLogLength { if len(raw) > maxPayloadLogLength {
raw = []byte("<truncated>") raw = []byte("<truncated>")
} }
-14
View File
@@ -1,14 +0,0 @@
package utils
func ArrayIn[T comparable](val T, array []T) (exists bool, index int) {
exists, index = false, -1
for i, a := range array {
if a == val {
exists, index = true, i
return
}
}
return
}