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 c112e696 [VIEWER-DESKTOP-GO] Backend sql connect (#704)
c112e696 is described below

commit c112e6967ee5c3be33ad98b3fa72877557d60fc2
Author: marodins <[email protected]>
AuthorDate: Fri Feb 24 03:50:48 2023 -0800

    [VIEWER-DESKTOP-GO] Backend sql connect (#704)
    
    * README screenshot and gif included
    
    * backend file and folder structure
    
    * file folder structure
    
    * re-run checks
    
    * setup backend session handling and db connection
    
    * update error handling and connection type
---
 backend/age-viewer-go        | Bin 0 -> 8737710 bytes
 backend/db/db.go             |   1 +
 backend/go.mod               |  23 +++++++++++++++++++++
 backend/go.sum               |  47 +++++++++++++++++++++++++++++++++++++++++++
 backend/main.go              |  19 +++++++++++++++++
 backend/models/connection.go |  46 ++++++++++++++++++++++++++++++++++++++++++
 backend/routes/connect.go    |  33 ++++++++++++++++++++++++++++++
 backend/session/session.go   |  20 ++++++++++++++++++
 backend/session/store.go     |   9 +++++++++
 9 files changed, 198 insertions(+)

diff --git a/backend/age-viewer-go b/backend/age-viewer-go
new file mode 100755
index 00000000..e8a2213e
Binary files /dev/null and b/backend/age-viewer-go differ
diff --git a/backend/db/db.go b/backend/db/db.go
new file mode 100644
index 00000000..3a49c63e
--- /dev/null
+++ b/backend/db/db.go
@@ -0,0 +1 @@
+package db
diff --git a/backend/go.mod b/backend/go.mod
new file mode 100644
index 00000000..6e887d6a
--- /dev/null
+++ b/backend/go.mod
@@ -0,0 +1,23 @@
+module age-viewer-go
+
+go 1.19
+
+require (
+       github.com/gorilla/sessions v1.2.1
+       github.com/labstack/echo v3.3.10+incompatible
+       github.com/lib/pq v1.10.7
+)
+
+require (
+       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
+       github.com/mattn/go-isatty v0.0.14 // indirect
+       github.com/stretchr/testify v1.8.1 // indirect
+       github.com/valyala/bytebufferpool v1.0.0 // indirect
+       github.com/valyala/fasttemplate v1.2.1 // indirect
+       golang.org/x/crypto v0.6.0 // indirect
+       golang.org/x/net v0.6.0 // indirect
+       golang.org/x/sys v0.5.0 // indirect
+       golang.org/x/text v0.7.0 // indirect
+)
diff --git a/backend/go.sum b/backend/go.sum
new file mode 100644
index 00000000..a550d91a
--- /dev/null
+++ b/backend/go.sum
@@ -0,0 +1,47 @@
+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/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=
+github.com/gorilla/sessions v1.2.1/go.mod 
h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
+github.com/labstack/echo v3.3.10+incompatible 
h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
+github.com/labstack/echo v3.3.10+incompatible/go.mod 
h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
+github.com/labstack/gommon v0.4.0 
h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
+github.com/labstack/gommon v0.4.0/go.mod 
h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
+github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
+github.com/lib/pq v1.10.7/go.mod 
h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
+github.com/mattn/go-colorable v0.1.11 
h1:nQ+aFkoE2TMGc0b68U2OKSexC+eq46+XwZzWXHRmPYs=
+github.com/mattn/go-colorable v0.1.11/go.mod 
h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
+github.com/mattn/go-isatty v0.0.14 
h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
+github.com/mattn/go-isatty v0.0.14/go.mod 
h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
+github.com/pmezard/go-difflib v1.0.0 
h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod 
h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod 
h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod 
h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod 
h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.7.0/go.mod 
h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.1/go.mod 
h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod 
h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.1 
h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
+github.com/stretchr/testify v1.8.1/go.mod 
h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
+github.com/valyala/bytebufferpool v1.0.0 
h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
+github.com/valyala/bytebufferpool v1.0.0/go.mod 
h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
+github.com/valyala/fasttemplate v1.2.1 
h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
+github.com/valyala/fasttemplate v1.2.1/go.mod 
h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
+golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
+golang.org/x/crypto v0.6.0/go.mod 
h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
+golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
+golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod 
h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod 
h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod 
h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod 
h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod 
h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod 
h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/backend/main.go b/backend/main.go
new file mode 100644
index 00000000..8e932361
--- /dev/null
+++ b/backend/main.go
@@ -0,0 +1,19 @@
+package main
+
+import (
+       "age-viewer-go/models"
+       "age-viewer-go/routes"
+       "age-viewer-go/session"
+       "encoding/gob"
+
+       "github.com/labstack/echo"
+)
+
+func main() {
+       app := echo.New()
+       gob.Register(models.Connection{})
+       app.Use(session.UserSessions())
+
+       app.POST("/connect", routes.ConnectToDb)
+       app.Start(":8080")
+}
diff --git a/backend/models/connection.go b/backend/models/connection.go
new file mode 100644
index 00000000..ac84bd09
--- /dev/null
+++ b/backend/models/connection.go
@@ -0,0 +1,46 @@
+package models
+
+import (
+       "database/sql"
+       "fmt"
+
+       "github.com/labstack/echo"
+       _ "github.com/lib/pq"
+)
+
+type Connection struct {
+       Port     string
+       Host     string
+       Password string
+       User     string
+       DbName   string
+       SSL      string `default:"require"`
+}
+
+func FromRequestBody(context echo.Context) (*Connection, error) {
+       user := new(Connection)
+       e := context.Bind(&user)
+       return user, e
+}
+
+func (c *Connection) ToSQLString(secure ...string) string {
+       if len(secure) > 0 {
+               c.SSL = secure[0]
+       }
+
+       str := fmt.Sprintf(
+               "user=%s port=%s host=%s password=%s sslmode=%s dbname=%s",
+               c.User,
+               c.Port,
+               c.Host,
+               c.Password,
+               c.SSL,
+               c.DbName,
+       )
+
+       return str
+}
+
+func (c Connection) GetConnection(secure ...string) (*sql.DB, error) {
+       return sql.Open("postgres", c.ToSQLString(secure...))
+}
diff --git a/backend/routes/connect.go b/backend/routes/connect.go
new file mode 100644
index 00000000..5f437517
--- /dev/null
+++ b/backend/routes/connect.go
@@ -0,0 +1,33 @@
+package routes
+
+import (
+       "age-viewer-go/models"
+       "fmt"
+
+       "github.com/gorilla/sessions"
+       "github.com/labstack/echo"
+)
+
+func ConnectToDb(c echo.Context) error {
+       udata, err := models.FromRequestBody(c)
+       if err != nil {
+               return echo.NewHTTPError(400, "invalid data")
+       }
+       db, err := udata.GetConnection()
+       if err != nil {
+               return echo.NewHTTPError(400, fmt.Sprintf("could not establish 
connection due to %s", err.Error()))
+       }
+       err = db.Ping()
+       if err != nil {
+               return echo.NewHTTPError(400, fmt.Sprintf("could not establish 
connection due to %s", err.Error()))
+       }
+       defer db.Close()
+       sess := c.Get("database").(*sessions.Session)
+       sess.Values["db"] = udata
+       err = sess.Save(c.Request(), c.Response().Writer)
+       if err != nil {
+               return echo.NewHTTPError(400, "could not save session")
+       }
+
+       return c.JSON(200, map[string]string{"status": "connected"})
+}
diff --git a/backend/session/session.go b/backend/session/session.go
new file mode 100644
index 00000000..8fd7e7a0
--- /dev/null
+++ b/backend/session/session.go
@@ -0,0 +1,20 @@
+package session
+
+import (
+       "github.com/labstack/echo"
+)
+
+func UserSessions() echo.MiddlewareFunc {
+
+       return func(next echo.HandlerFunc) echo.HandlerFunc {
+               return func(c echo.Context) error {
+                       sess, err := Store.Get(c.Request(), "database")
+                       if err != nil {
+                               return echo.NewHTTPError(400, err.Error())
+                       }
+                       c.Set("database", sess)
+                       return next(c)
+               }
+       }
+
+}
diff --git a/backend/session/store.go b/backend/session/store.go
new file mode 100644
index 00000000..3fb19d30
--- /dev/null
+++ b/backend/session/store.go
@@ -0,0 +1,9 @@
+package session
+
+import (
+       "os"
+
+       "github.com/gorilla/sessions"
+)
+
+var Store = sessions.NewCookieStore([]byte(os.Getenv("GO_SECRET")))

Reply via email to