4 Commits

Author SHA1 Message Date
Adrian Zuercher
5e1e4b9daf make new model for stringSlices allowOrigin helper 2025-07-13 20:01:13 +02:00
Adrian Zuercher
0f06128ce8 new send datapoint type and one fix typo 2025-07-11 17:47:12 +02:00
Adrian Zuercher
be07dc8749 add new argument for remote port 2025-07-11 17:46:32 +02:00
Adrian Zuercher
e75d7c8b03 add automatic localhost and local ip to allow origin 2025-06-29 21:18:15 +02:00
11 changed files with 63 additions and 31 deletions

View File

@@ -2,7 +2,6 @@ package args
import (
"flag"
"strings"
"github.com/tecamino/tecamino-dbm/cert"
"github.com/tecamino/tecamino-dbm/models"
@@ -19,21 +18,10 @@ type Args struct {
Debug bool
}
type StringSlice []string
func (s *StringSlice) String() string {
return strings.Join(*s, ",")
}
func (s *StringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
// initialte cli arguments
func Init() *Args {
var allowOrigins StringSlice
var allowOrigins models.StringSlice
flag.Var(&allowOrigins, "allowOrigin", "Allowed origin (can repeat this flag)")
@@ -43,6 +31,7 @@ func Init() *Args {
keyFile := flag.String("keyFile", "./cert/key.pem", "path of keyfile")
portHttp := flag.Uint("http-port", 8100, "json server communication for http/ws")
portHttps := flag.Uint("https-port", 8101, "json server communication for http/wss")
remotePort := flag.Uint("remotePort", 9500, "remote Port of gui user interface")
rootDir := flag.String("workingDir", "./", "working directory")
dbmFile := flag.String("dbm", "/test/test", "dbm file name")
debug := flag.Bool("debug", false, "debug flag")
@@ -56,8 +45,9 @@ func Init() *Args {
KeyFile: *keyFile,
},
Port: models.Port{
Http: *portHttp,
Https: *portHttps,
Http: *portHttp,
Https: *portHttps,
Remote: *remotePort,
},
RootDir: *rootDir,
DBMFile: *dbmFile,
@@ -65,7 +55,7 @@ func Init() *Args {
}
if len(allowOrigins) == 0 {
allowOrigins = StringSlice{"http://localhost:9500"}
allowOrigins = models.StringSlice{"http://localhost:9500"}
}
a.AllowOrigins = allowOrigins

View File

@@ -35,6 +35,7 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
Path: res.Path,
Type: res.Type,
Value: res.Value,
Drivers: &res.Drivers,
HasChild: res.Datapoints != nil,
Rights: res.ReadWrite,
})

2
go.mod
View File

@@ -7,7 +7,7 @@ require (
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.3
github.com/tecamino/tecamino-json_data v0.0.16
github.com/tecamino/tecamino-json_data v0.0.17
github.com/tecamino/tecamino-logger v0.2.0
)

4
go.sum
View File

@@ -71,8 +71,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tecamino/tecamino-json_data v0.0.16 h1:aZFxnhm4g6WMDPoqy4HosUk7vl0DB0iIcVs8bbT4MzU=
github.com/tecamino/tecamino-json_data v0.0.16/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
github.com/tecamino/tecamino-json_data v0.0.17 h1:M12UzKbfIgu/q3ERsEhGNDV3DYOvc0TioU288kQjDCA=
github.com/tecamino/tecamino-json_data v0.0.17/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
github.com/tecamino/tecamino-logger v0.2.0 h1:NPH/Gg9qRhmVoW8b39i1eXu/LEftHc74nyISpcRG+XU=
github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=

View File

@@ -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, a.Port.Remote)
//set routes
dbmHandler.Log.Debug("main", "setting routes")

View File

@@ -385,6 +385,7 @@ func (d *Datapoint) Publish(eventType string) error {
Event: eventType,
Uuid: d.Uuid,
Path: d.Path,
Type: d.Type,
Value: d.Value,
})

View File

@@ -1,6 +1,7 @@
package models
type Port struct {
Http uint
Https uint
Http uint
Https uint
Remote uint
}

14
models/stringSlices.go Normal file
View File

@@ -0,0 +1,14 @@
package models
import "strings"
type StringSlice []string
func (s *StringSlice) String() string {
return strings.Join(*s, ",")
}
func (s *StringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}

View File

@@ -142,7 +142,7 @@ func TestUpdateDps(t *testing.T) {
func TestServer(t *testing.T) {
fmt.Println("start")
server := ws.NewServer([]string{".*"})
server := ws.NewServer([]string{".*"}, 9500)
t.Fatal(server.ServeHttp("http://localhost", 8100))
}

View File

@@ -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")
}

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"},