This is an automated email from the ASF dual-hosted git repository.
ako pushed a commit to branch ageviewer_go
in repository https://gitbox.apache.org/repos/asf/age.git
The following commit(s) were added to refs/heads/ageviewer_go by this push:
new 03cb1670 [Age-Viewer-Go-Desktop] Update CORS origin policy, and
connection functions to match web viewer response (#927)
03cb1670 is described below
commit 03cb167067183511d924b5106397b64cab9ee46a
Author: Sarthak <[email protected]>
AuthorDate: Tue May 30 09:50:14 2023 +0530
[Age-Viewer-Go-Desktop] Update CORS origin policy, and connection functions
to match web viewer response (#927)
* Fixed CORS Error
* update connection status to match web age-viewer
* update function to match web viewer response
---
backend/go.mod | 1 +
backend/go.sum | 2 ++
backend/main.go | 7 ++++++-
backend/models/connection.go | 7 +++++--
backend/routes/connect.go | 9 ++++++---
5 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/backend/go.mod b/backend/go.mod
index 6e887d6a..ff202c15 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -9,6 +9,7 @@ require (
)
require (
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index a550d91a..7ecdc7ef 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1
h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible
h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod
h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gorilla/securecookie v1.1.1
h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod
h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1
h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI=
diff --git a/backend/main.go b/backend/main.go
index 8224e56e..07614104 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -8,18 +8,23 @@ import (
"encoding/gob"
"github.com/labstack/echo"
+ "github.com/labstack/echo/middleware"
)
// main is the entry point for the Backend, it starts the server, and sets up
the routes.
func main() {
app := echo.New()
+
+ app.Use(middleware.CORSWithConfig(middleware.CORSConfig{
+ AllowOrigins: []string{"*"},
+ AllowHeaders: []string{echo.HeaderOrigin,
echo.HeaderContentType, echo.HeaderAccept},
+ }))
gob.Register(models.Connection{})
app.Use(session.UserSessions())
app.POST("/connect", routes.ConnectToDb)
app.GET("/status", routes.StatusDB)
app.POST("/disconnect", routes.DisconnectFromDb)
-
cypher := app.Group("/query", routes.CypherMiddleWare)
cypher.Use(m.ValidateContentTypeMiddleWare)
cypher.POST("/metadata", routes.GraphMetaData)
diff --git a/backend/models/connection.go b/backend/models/connection.go
index eced564e..3f2617a1 100644
--- a/backend/models/connection.go
+++ b/backend/models/connection.go
@@ -10,7 +10,7 @@ import (
// Connection struct represents the connection parameters for a database
type Connection struct {
- Port string
+ Port int
Host string
Password string
User string
@@ -18,6 +18,9 @@ type Connection struct {
SSL string `default:"require"`
GraphInit bool
Version int
+ Graphs []string
+ Graph string
+
}
/*
@@ -41,7 +44,7 @@ func (c *Connection) ToSQLString(secure ...string) string {
}
str := fmt.Sprintf(
- "user=%s port=%s host=%s password=%s sslmode=%s dbname=%s",
+ "user=%s port=%d host=%s password=%s sslmode=%s dbname=%s",
c.User,
c.Port,
c.Host,
diff --git a/backend/routes/connect.go b/backend/routes/connect.go
index dd2aa50c..bac2e226 100644
--- a/backend/routes/connect.go
+++ b/backend/routes/connect.go
@@ -11,8 +11,10 @@ import (
/*
This function takes user data from the request body, establishes a database
connection, saves the
-connection information in the session, and returns a JSON response with a
"status" field
-set to "connected". It handles errors related to invalid data, connection
establishment, and session saving.
+connection information in the session, and it returns a JSON response with
fields
+containing the host, postgres version, port, database, user, password, list of
graphs
+and current graph. It handles errors related to invalid data, connection
establishment, and session saving.
+
*/
func ConnectToDb(c echo.Context) error {
udata, err := models.FromRequestBody(c)
@@ -30,6 +32,7 @@ func ConnectToDb(c echo.Context) error {
}
defer db.Close()
sess := c.Get("database").(*sessions.Session)
+ udata.Graphs = []string{}
sess.Values["db"] = udata
err = sess.Save(c.Request(), c.Response().Writer)
@@ -37,7 +40,7 @@ func ConnectToDb(c echo.Context) error {
return echo.NewHTTPError(400, "could not save session")
}
- return c.JSON(200, map[string]string{"status": "connected"})
+ return c.JSON(200, udata)
}
/*