From a794c5919b3e632fbdb1917e3587901ac53a993b Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sat, 20 Sep 2025 15:57:29 -0600 Subject: [PATCH] added database connection and ability to retrieve "Anonymous Honyak" user --- config/config.go | 4 ++++ config/default.yaml | 3 +++ database/base.go | 34 +++++++++++++++++++++++++++++ database/user.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 9 ++++++++ main.go | 7 ++++++ ui/session_mgr.go | 7 ++++++ 8 files changed, 119 insertions(+) create mode 100644 database/base.go create mode 100644 database/user.go diff --git a/config/config.go b/config/config.go index af8e475..06311d2 100644 --- a/config/config.go +++ b/config/config.go @@ -46,6 +46,10 @@ type AmConfig struct { Site struct { Title string `yaml:"title"` } `yaml:"site"` + Database struct { + Driver string `yaml:"driver"` + Dsn string `yaml:"dsn"` + } `yaml:"database"` Rendering struct { TemplateDir string `yaml:"templatedir"` CookieKey string `yaml:"cookiekey"` diff --git a/config/default.yaml b/config/default.yaml index 950ac38..290e7e8 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -8,6 +8,9 @@ # site: title: "Amsterdam Web Communities System" +database: + driver: "mysql" + dsn: "amsdb:x00yes2k@tcp(localhost)/amsterdam" rendering: templatedir: custom_templates cookiekey: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz diff --git a/database/base.go b/database/base.go new file mode 100644 index 0000000..9ab0998 --- /dev/null +++ b/database/base.go @@ -0,0 +1,34 @@ +/* + * Amsterdam Web Communities System + * Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ +// The database package contains database management and storage logic. +package database + +import ( + "git.erbosoft.com/amy/amsterdam/config" + _ "github.com/go-sql-driver/mysql" + "github.com/jmoiron/sqlx" +) + +// amdb is the reference to the Amsterdam database. +var amdb *sqlx.DB + +// SetupDb sets up the database and associated items. +func SetupDb() error { + db, err := sqlx.Open(config.GlobalConfig.Database.Driver, config.GlobalConfig.Database.Dsn) + if err == nil { + amdb = db + // TODO: additional initialization + } + return err +} + +// ClosedownDb closes down the database and associated items. +func ClosedownDb() { + amdb.Close() +} diff --git a/database/user.go b/database/user.go new file mode 100644 index 0000000..b6e9042 --- /dev/null +++ b/database/user.go @@ -0,0 +1,52 @@ +/* + * Amsterdam Web Communities System + * Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ +// The database package contains database management and storage logic. +package database + +import ( + "fmt" + "time" +) + +// User represents a user in the Amsterdam database. +type User struct { + Uid int32 `db:"uid"` + Username string `db:"username"` + Passhash string `db:"passhash"` + Tokenauth string `db:"tokenauth"` + ContactID int32 `db:"contactid"` + IsAnon bool `db:"is_anon"` + VerifyEMail bool `db:"verify_email"` + Lockout bool `db:"lockout"` + AccessTries int16 `db:"access_tries"` + EmailConfNum int32 `db:"email_confnum"` + BaseLevel uint16 `db:"base_lvl"` + Created time.Time `db:"created"` + LastAccess time.Time `db:"lastaccess"` + PassReminder string `db:"passreminder"` + Description string `db:"description"` + DOB time.Time `db:"dob"` +} + +/* AmGetAmonUser returns a reference to the anonymous user. + * Returns: + * Pointer to User containing anonymous user data + * Standard Go error status + */ +func AmGetAnonUser() (*User, error) { + var rc []User + err := amdb.Select(&rc, "SELECT * from users WHERE uid = 1") + if err != nil { + return nil, err + } + if len(rc) > 1 { + return nil, fmt.Errorf("AmGetAnonUser: too many responses(%d)", len(rc)) + } + return &(rc[0]), err +} diff --git a/go.mod b/go.mod index 89236ee..2812388 100644 --- a/go.mod +++ b/go.mod @@ -11,12 +11,15 @@ require ( ) require ( + filippo.io/edwards25519 v1.1.0 // indirect github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect github.com/alexflint/go-arg v1.6.0 // indirect github.com/alexflint/go-scalar v1.2.0 // indirect + github.com/go-sql-driver/mysql v1.9.3 // indirect github.com/gorilla/context v1.1.2 // indirect github.com/gorilla/securecookie v1.1.2 // indirect github.com/gorilla/sessions v1.4.0 // indirect + github.com/jmoiron/sqlx v1.4.0 // indirect github.com/labstack/echo-contrib v0.17.4 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 801d3f1..68e1ef2 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 h1:sR+/8Yb4slttB4vD+b9btVEnWgL3Q00OBTzVT8B9C0c= github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno= github.com/CloudyKit/jet/v6 v6.3.1 h1:6IAo5Cx21xrHVaR8zzXN5gJatKV/wO7Nf6bfCnCSbUw= @@ -9,22 +11,29 @@ github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oy 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/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= github.com/gorilla/context v1.1.2 h1:WRkNAv2uoa03QNIc1A6u4O7DAGMUVoopZhkiXWA2V1o= github.com/gorilla/context v1.1.2/go.mod h1:KDPwT9i/MeWHiLl90fuTgrt4/wPcv75vFAZLaOOcbxM= github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ= github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/labstack/echo-contrib v0.17.4 h1:g5mfsrJfJTKv+F5uNKCyrjLK7js+ZW6HTjg4FnDxxgk= github.com/labstack/echo-contrib v0.17.4/go.mod h1:9O7ZPAHUeMGTOAfg80YqQduHzt0CzLak36PZRldYrZ0= github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= diff --git a/main.go b/main.go index 1ff7203..31c37b1 100644 --- a/main.go +++ b/main.go @@ -12,12 +12,14 @@ package main import ( "context" + "fmt" "net/http" "os" "os/signal" "time" "git.erbosoft.com/amy/amsterdam/config" + "git.erbosoft.com/amy/amsterdam/database" "git.erbosoft.com/amy/amsterdam/ui" "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" @@ -43,6 +45,11 @@ func setupEcho() *echo.Echo { func main() { // Configure the system. config.SetupConfig() + err := database.SetupDb() + if err != nil { + panic(fmt.Sprintf("Database open failure: %v", err)) + } + defer database.ClosedownDb() ui.SetupTemplates() ui.SetupSessionManager() diff --git a/ui/session_mgr.go b/ui/session_mgr.go index e3db50c..6fa3897 100644 --- a/ui/session_mgr.go +++ b/ui/session_mgr.go @@ -12,6 +12,7 @@ package ui import ( "git.erbosoft.com/amy/amsterdam/config" + "git.erbosoft.com/amy/amsterdam/database" "github.com/gorilla/sessions" log "github.com/sirupsen/logrus" ) @@ -28,4 +29,10 @@ func SetupSessionManager() { // SetupAmSession sets up a newly created Amsterdam session. func SetupAmSession(session *sessions.Session) { session.Values["temp"] = "Active" + u, err := database.AmGetAnonUser() + if err == nil { + session.Values["user"] = u + } else { + log.Errorf("Unable to load anon user: %v", err) + } }