add automatic localhost and local ip to allow origin
This commit is contained in:
2
main.go
2
main.go
@@ -23,7 +23,7 @@ func main() {
|
||||
|
||||
//initialize new server
|
||||
dbmHandler.Log.Debug("main", "initialize new server instance")
|
||||
s := ws.NewServer(a.AllowOrigins)
|
||||
s := ws.NewServer(a.AllowOrigins, 9000)
|
||||
|
||||
//set routes
|
||||
dbmHandler.Log.Debug("main", "setting routes")
|
||||
|
@@ -1,5 +1,10 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func ListofA2ZZ() (list []string) {
|
||||
for i := 'A'; i <= 'Z'; i++ {
|
||||
list = append(list, string(i))
|
||||
@@ -9,12 +14,21 @@ func ListofA2ZZ() (list []string) {
|
||||
list = append(list, string(i)+string(j))
|
||||
}
|
||||
}
|
||||
// for i := 'A'; i <= 'Z'; i++ {
|
||||
// for j := 'A'; j <= 'Z'; j++ {
|
||||
// for k := 'A'; k <= 'Z'; k++ {
|
||||
// list = append(list, string(i)+string(j)+string(k))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
func GetLocalIP() (string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
||||
if ipNet.IP.To4() != nil {
|
||||
return ipNet.IP.String(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("no local IP address found")
|
||||
}
|
||||
|
@@ -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