add automatic localhost and local ip to allow origin
This commit is contained in:
@@ -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"},
|
||||
|
Reference in New Issue
Block a user