add automatic localhost and local ip to allow origin

This commit is contained in:
Adrian Zuercher
2025-06-29 21:18:15 +02:00
parent 59c7705ca1
commit e75d7c8b03
3 changed files with 34 additions and 9 deletions

View File

@@ -2,12 +2,14 @@ package websocket
import (
"fmt"
"log"
"sync"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/tecamino/tecamino-dbm/cert"
"github.com/tecamino/tecamino-dbm/utils"
"github.com/tecamino/tecamino-logger/logging"
)
@@ -19,8 +21,17 @@ type Server struct {
}
// initalizes new dbm server
func NewServer(allowOrigins []string) *Server {
func NewServer(allowOrigins []string, port uint) *Server {
r := gin.Default()
allowOrigins = append(allowOrigins, fmt.Sprintf("http://localhost:%d", port))
localIP, err := utils.GetLocalIP()
if err != nil {
log.Printf("get local ip : %s", err.Error())
} else {
allowOrigins = append(allowOrigins, fmt.Sprintf("http://%s:%d", localIP, port))
}
r.Use(cors.New(cors.Config{
AllowOrigins: allowOrigins,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},