add automatic localhost and local ip for allowOrigin
This commit is contained in:
@@ -48,6 +48,10 @@ func main() {
|
|||||||
Debug: *debug,
|
Debug: *debug,
|
||||||
TerminalOut: true,
|
TerminalOut: true,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("main new logger", err.Error())
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
//new login manager
|
//new login manager
|
||||||
loginManager, err := login.NewLoginManager(".")
|
loginManager, err := login.NewLoginManager(".")
|
||||||
@@ -62,8 +66,20 @@ func main() {
|
|||||||
// new server
|
// new server
|
||||||
s := server.NewServer()
|
s := server.NewServer()
|
||||||
|
|
||||||
|
//get local ip
|
||||||
|
origins := []string{"http://localhost:9000"}
|
||||||
|
origins = append(origins, "http://localhost:9500")
|
||||||
|
|
||||||
|
localIP, err := utils.GetLocalIP()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("main", fmt.Sprintf("get local ip : %s", err.Error()))
|
||||||
|
} else {
|
||||||
|
origins = append(origins, fmt.Sprintf("http://%s:9000", localIP))
|
||||||
|
origins = append(origins, fmt.Sprintf("http://%s:9500", localIP))
|
||||||
|
}
|
||||||
|
fmt.Println(123, origins)
|
||||||
s.Routes.Use(cors.New(cors.Config{
|
s.Routes.Use(cors.New(cors.Config{
|
||||||
AllowOrigins: []string{"http://localhost:9000"},
|
AllowOrigins: origins,
|
||||||
AllowMethods: []string{"POST", "GET", "DELETE", "OPTIONS"},
|
AllowMethods: []string{"POST", "GET", "DELETE", "OPTIONS"},
|
||||||
AllowHeaders: []string{"Origin", "Content-Type"},
|
AllowHeaders: []string{"Origin", "Content-Type"},
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
@@ -104,7 +120,6 @@ func main() {
|
|||||||
logger.Error("main", fmt.Sprintf("starting browser error : %s", err.Error()))
|
logger.Error("main", fmt.Sprintf("starting browser error : %s", err.Error()))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fmt.Println(3, *ip, *port)
|
|
||||||
// start http server
|
// start http server
|
||||||
logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %d", *ip, *port))
|
logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %d", *ip, *port))
|
||||||
if err := s.ServeHttp(*ip, *port); err != nil {
|
if err := s.ServeHttp(*ip, *port); err != nil {
|
||||||
|
22
backend/utils/ip.go
Normal file
22
backend/utils/ip.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
@@ -4,8 +4,8 @@ import { initWebSocket } from 'src/services/websocket';
|
|||||||
|
|
||||||
export default boot(({ app }) => {
|
export default boot(({ app }) => {
|
||||||
const $q = app.config.globalProperties.$q as QVueGlobals;
|
const $q = app.config.globalProperties.$q as QVueGlobals;
|
||||||
const host = window.location.hostname; // gets current domain or IP
|
const host = window.location.hostname;
|
||||||
const port = 8100; // your WebSocket port
|
const port = 8100;
|
||||||
|
|
||||||
const randomId = Math.floor(Math.random() * 10001); // random number from 0 to 10000
|
const randomId = Math.floor(Math.random() * 10001); // random number from 0 to 10000
|
||||||
const ws = initWebSocket(`ws://${host}:${port}/ws?id=q${randomId}`, $q);
|
const ws = initWebSocket(`ws://${host}:${port}/ws?id=q${randomId}`, $q);
|
||||||
|
@@ -133,9 +133,12 @@ const newScene = reactive<Scene>({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const scenes = ref<Scene[]>([]);
|
const scenes = ref<Scene[]>([]);
|
||||||
|
const host = window.location.hostname;
|
||||||
|
const port = 9500;
|
||||||
|
const baseURL = `http://${host}:${port}`;
|
||||||
|
|
||||||
const quasarApi = axios.create({
|
const quasarApi = axios.create({
|
||||||
baseURL: `http://localhost:9500`,
|
baseURL: baseURL,
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -150,7 +153,9 @@ onMounted(() => {
|
|||||||
scenes.value = resp.data;
|
scenes.value = resp.data;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => NotifyResponse($q, err.response.data.error, 'error'));
|
.catch((err) => {
|
||||||
|
NotifyResponse($q, err.response.data.error, 'error');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function removeScene(name: string) {
|
function removeScene(name: string) {
|
||||||
|
Reference in New Issue
Block a user