diff --git a/backend/env/env.go b/backend/env/env.go index 71bc8f2..c6d293e 100644 --- a/backend/env/env.go +++ b/backend/env/env.go @@ -17,12 +17,15 @@ const ( PrivKey EnvKey = "PRIVKEY" Fullchain EnvKey = "FULLCHAIN" Https EnvKey = "HTTPS" - Url EnvKey = "URL" - Port EnvKey = "PORT" + HostUrl EnvKey = "HOST_URL" + HostPort EnvKey = "HOST_PORT" WorkingDir EnvKey = "WORKING_DIR" Spa EnvKey = "SPA" AccessSecret EnvKey = "ACCESS_SECRET" RefreshSecret EnvKey = "REFRESH_SECRET" + Organization EnvKey = "ORGANIZATION" + DOMAIN EnvKey = "DOMAIN" + AllowOrigin EnvKey = "ALLOWORIGIN" ) const ( diff --git a/backend/example/example.env b/backend/example/example.env index 9122264..65f8a49 100644 --- a/backend/example/example.env +++ b/backend/example/example.env @@ -3,10 +3,12 @@ GIN_MODE=release DEBUG=false SPA=directory_of_spa_files WORKING_DIR=. -URL=your_local_url -PORT=your_local_port +HOST_URL=your_local_url +HOST_PORT=your_local_port HTTPS=true PRIVKEY=your_certificate_key_file FULLCHAIN=your_certificate_fullchain_file ACCESS_SECRET=your_32bit_long_access_secret -REFRESH_SECRET=your_32bit_long_referesh_secret \ No newline at end of file +REFRESH_SECRET=your_32bit_long_referesh_secret +ALLOWORIGIN=all_allowed_urls +DOMAIN=your_domain \ No newline at end of file diff --git a/backend/go.mod b/backend/go.mod index 2db0fba..6b12e18 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -3,7 +3,7 @@ module backend go 1.24.5 require ( - gitea.tecamino.com/paadi/access-handler v1.0.22 + gitea.tecamino.com/paadi/access-handler v1.0.23 gitea.tecamino.com/paadi/memberDB v1.1.2 gitea.tecamino.com/paadi/tecamino-dbm v0.1.1 gitea.tecamino.com/paadi/tecamino-logger v0.2.1 diff --git a/backend/go.sum b/backend/go.sum index 7f1085d..388c273 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,5 +1,5 @@ -gitea.tecamino.com/paadi/access-handler v1.0.22 h1:XFi2PQ1gWqe9YuSye4Ti1o5TpdV0AFpt4Fb58MFMagk= -gitea.tecamino.com/paadi/access-handler v1.0.22/go.mod h1:wKsB5/Rvaj580gdg3+GbUf5V/0N00XN6cID+C/8135M= +gitea.tecamino.com/paadi/access-handler v1.0.23 h1:bsQgGEyiV8nutZbkNKCDkHWhIpOUEMV22wePxQsn3QU= +gitea.tecamino.com/paadi/access-handler v1.0.23/go.mod h1:wKsB5/Rvaj580gdg3+GbUf5V/0N00XN6cID+C/8135M= gitea.tecamino.com/paadi/dbHandler v1.0.8 h1:ZWSBM/KFtLwTv2cBqwK1mOxWAxAfL0BcWEC3kJ9JALU= gitea.tecamino.com/paadi/dbHandler v1.0.8/go.mod h1:y/xn/POJg1DO++67uKvnO23lJQgh+XFQq7HZCS9Getw= gitea.tecamino.com/paadi/memberDB v1.1.2 h1:j/Tsr7JnzAkdOvgjG77TzTVBWd4vBrmEFzPXNpW7GYk= diff --git a/backend/main.go b/backend/main.go index 372a7c2..342b567 100644 --- a/backend/main.go +++ b/backend/main.go @@ -24,12 +24,8 @@ import ( ) func main() { - var allowOrigins models.StringSlice - - flag.Var(&allowOrigins, "allowOrigin", "Allowed origin (can repeat this flag)") - + // set cli flage envFile := flag.String("env", ".env", "enviroment file") - organization := flag.String("organization", "", "self signed ciertificate organization") flag.Parse() // load enviroment file if exists @@ -51,6 +47,16 @@ func main() { os.Chdir(workingDir) } + //set allowed origins + var allowOrigins models.StringSlice + + if strings.Contains(env.DOMAIN.GetValue(), "http") { + allowOrigins.Set(env.DOMAIN.GetValue()) + } + if env.AllowOrigin.GetValue() != "" { + allowOrigins.Set(env.AllowOrigin.GetValue()) + } + wd, err := os.Getwd() if err != nil { log.Fatalf("Could not get working directory: %v", err) @@ -103,6 +109,8 @@ func main() { allowOrigins = append(allowOrigins, fmt.Sprintf("%s%s:9000", httpString, localIP), fmt.Sprintf("%s%s:9500", httpString, localIP)) } + fmt.Println(100, allowOrigins) + s.Routes.Use(cors.New(cors.Config{ AllowOrigins: allowOrigins, //AllowOrigins: []string{"*"}, @@ -181,7 +189,7 @@ func main() { go func() { time.Sleep(500 * time.Millisecond) - if err := utils.OpenBrowser(fmt.Sprintf("%slocalhost:%s", httpString, env.Port.GetValue()), logger); err != nil { + if err := utils.OpenBrowser(fmt.Sprintf("%slocalhost:%s", httpString, env.HostPort.GetValue()), logger); err != nil { logger.Error("main", fmt.Sprintf("starting browser error : %s", err)) } }() @@ -197,16 +205,16 @@ func main() { } // start https server - logger.Info("main", fmt.Sprintf("https listen on ip: %s port: %s", env.Url.GetValue(), env.Port.GetValue())) - if err := s.ServeHttps(env.Url.GetValue(), env.Port.GetUIntValue(), cert.Cert{Organization: *organization, CertFile: env.Fullchain.GetValue(), KeyFile: env.PrivKey.GetValue()}); err != nil { + logger.Info("main", fmt.Sprintf("https listen on ip: %s port: %s", env.HostUrl.GetValue(), env.HostPort.GetValue())) + if err := s.ServeHttps(env.HostUrl.GetValue(), env.HostPort.GetUIntValue(), cert.Cert{Organization: env.Organization.GetValue(), CertFile: env.Fullchain.GetValue(), KeyFile: env.PrivKey.GetValue()}); err != nil { logger.Error("main", "error https server "+err.Error()) } return } // start http server - logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %s", env.Url.GetValue(), env.Port.GetValue())) - if err := s.ServeHttp(env.Url.GetValue(), env.Port.GetUIntValue()); err != nil { + logger.Info("main", fmt.Sprintf("http listen on ip: %s port: %s", env.HostUrl.GetValue(), env.HostPort.GetValue())) + if err := s.ServeHttp(env.HostUrl.GetValue(), env.HostPort.GetUIntValue()); err != nil { logger.Error("main", "error http server "+err.Error()) } } diff --git a/backend/models/stringSlice.go b/backend/models/stringSlice.go index 329a7a1..e6dc802 100644 --- a/backend/models/stringSlice.go +++ b/backend/models/stringSlice.go @@ -4,11 +4,10 @@ import "strings" type StringSlice []string -func (s *StringSlice) String() string { - return strings.Join(*s, ",") -} - -func (s *StringSlice) Set(value string) error { +func (s *StringSlice) Set(value string) { + if strings.Contains(value, ",") { + *s = append(*s, strings.Split(value, ",")...) + return + } *s = append(*s, value) - return nil }