Compare commits

..

No commits in common. "fcd6477ec35d7aff7cdb9367490e19aa44c1017a" and "a2077131a7d580791c40acaeea4ea2ae060c01d0" have entirely different histories.

7 changed files with 17 additions and 153 deletions

View File

@ -9,12 +9,11 @@ import (
type Configuration struct {
Db struct {
Ip string `json:"DbIp"`
Port string `json:"DbPort"`
Name string `json:"DbName"`
User string `json:"DbUser"`
Password string `json:"DbPassword"`
AutoMigrate bool `json:"DbAutoMigrate"`
Ip string `json:"DbIp"`
Port string `json:"DbPort"`
Name string `json:"DbName"`
User string `json:"DbUser"`
Password string `json:"DbPassword"`
}
Listen struct {

View File

@ -4,8 +4,9 @@ import (
"GoWeb/app"
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
_ "github.com/lib/pq"
)
// ConnectDB returns a new database connection

View File

@ -1,105 +0,0 @@
package database
import (
"GoWeb/app"
"errors"
"fmt"
"github.com/lib/pq"
"log"
"reflect"
)
// Migrate given a dummy object of any type, it will create a table with the same name as the type and create columns with the same name as the fields of the object
func Migrate(app *app.App, anyStruct interface{}) error {
valueOfStruct := reflect.ValueOf(anyStruct)
typeOfStruct := valueOfStruct.Type()
tableName := typeOfStruct.Name()
err := createTable(app, tableName)
if err != nil {
return err
}
for i := 0; i < valueOfStruct.NumField(); i++ {
fieldType := typeOfStruct.Field(i)
fieldName := fieldType.Name
if fieldName != "Id" && fieldName != "id" {
err := createColumn(app, tableName, fieldName, fieldType.Type.Name())
if err != nil {
return err
}
}
}
return nil
}
// createTable creates a table with the given name if it doesn't exist, it is assumed that id will be the primary key
func createTable(app *app.App, tableName string) error {
sanitizedTableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS \"%s\" (\"Id\" serial primary key)", tableName)
_, err := app.Db.Query(sanitizedTableQuery)
if err != nil {
log.Println("Error creating table: " + tableName)
return err
}
log.Println("Table created successfully (or already exists): " + tableName)
return nil
}
// createColumn creates a column with the given name and type if it doesn't exist
func createColumn(app *app.App, tableName, columnName, columnType string) error {
postgresType, err := getPostgresType(columnType)
if err != nil {
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
sanitizedTableName := pq.QuoteIdentifier(tableName)
query := fmt.Sprintf("ALTER TABLE %s ADD COLUMN IF NOT EXISTS \"%s\" %s", sanitizedTableName, columnName, postgresType)
_, err = app.Db.Query(query)
if err != nil {
log.Println("Error creating column: " + columnName + " in table: " + tableName + " with type: " + postgresType)
return err
}
log.Println("Column created successfully (or already exists):", columnName)
return nil
}
// Given a type in Go, return the corresponding type in Postgres
func getPostgresType(goType string) (string, error) {
switch goType {
case "int":
case "int32":
case "uint":
case "uint32":
return "integer", nil
case "int64":
case "uint64":
return "bigint", nil
case "int16":
case "int8":
case "uint16":
case "uint8":
case "byte":
return "smallint", nil
case "string":
return "text", nil
case "float64":
return "double precision", nil
case "bool":
return "boolean", nil
case "time.Time":
return "timestamp", nil
case "[]byte":
return "bytea", nil
default:
return "text", nil
}
return "", errors.New("Unknown type: " + goType)
}

View File

@ -4,8 +4,7 @@
"DbPort": "5432",
"DbName": "database",
"DbUser": "user",
"DbPassword": "password",
"AutoMigrate": true
"DbPassword": "password"
},
"Listen": {
"HttpIp": "127.0.0.1",

10
main.go
View File

@ -4,7 +4,6 @@ import (
"GoWeb/app"
"GoWeb/config"
"GoWeb/database"
"GoWeb/models"
"GoWeb/routes"
"embed"
"log"
@ -38,15 +37,8 @@ func main() {
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
log.SetOutput(file)
// Connect to database and run migrations
// Connect to database
appLoaded.Db = database.ConnectDB(&appLoaded)
if appLoaded.Config.Db.AutoMigrate {
err = models.RunAllMigrations(&appLoaded)
if err != nil {
log.Println(err)
return
}
}
// Define Routes
routes.GetRoutes(&appLoaded)

View File

@ -1,21 +0,0 @@
package models
import (
"GoWeb/app"
"GoWeb/database"
)
// RunAllMigrations defines the structs that should be represented in the database
func RunAllMigrations(app *app.App) error {
// Declare new dummy user for reflection
user := User{
Id: 1, // Id is handled automatically, but it is added here to show it will be skipped during column creation
Username: "migrate",
Password: "migrate",
AuthToken: "migrate",
CreatedAt: "2021-01-01 00:00:00",
UpdatedAt: "2021-01-01 00:00:00",
}
return database.Migrate(app, user)
}

View File

@ -18,7 +18,6 @@ type User struct {
Id int64
Username string
Password string
AuthToken string
CreatedAt string
UpdatedAt string
}
@ -35,7 +34,7 @@ func GetCurrentUser(app *app.App, r *http.Request) (User, error) {
var userId int64
// Query row by session cookie
err = app.Db.QueryRow("SELECT Id FROM User WHERE session = $1", cookie.Value).Scan(&userId)
err = app.Db.QueryRow("SELECT user_id FROM sessions WHERE session = $1", cookie.Value).Scan(&userId)
if err != nil {
log.Println("Error querying session row with session: " + cookie.Value)
return User{}, err
@ -49,7 +48,7 @@ func GetUserById(app *app.App, id int64) (User, error) {
user := User{}
// Query row by id
row, err := app.Db.Query("SELECT Id, Username, Password, AuthToken, CreatedAt, UpdatedAt FROM User WHERE Id = $1", id)
row, err := app.Db.Query("SELECT id, username, password, created_at, updated_at FROM users WHERE id = $1", id)
if err != nil {
log.Println("Error querying user row with id: " + strconv.FormatInt(id, 10))
return User{}, err
@ -86,7 +85,7 @@ func CreateUser(app *app.App, username string, password string, createdAt time.T
var lastInsertId int64
sqlStatement := "INSERT INTO User (Username, Password, CreatedAt, UpdatedAt) VALUES ($1, $2, $3, $4) RETURNING Id"
sqlStatement := "INSERT INTO users (username, password, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING id"
err = app.Db.QueryRow(sqlStatement, username, string(hash), createdAt, updatedAt).Scan(&lastInsertId)
if err != nil {
log.Println("Error creating user row")
@ -102,7 +101,7 @@ func AuthenticateUser(app *app.App, w http.ResponseWriter, username string, pass
var hashedPassword []byte
// Query row by username, scan password column
err := app.Db.QueryRow("SELECT Password FROM User WHERE Username = $1", username).Scan(&hashedPassword)
err := app.Db.QueryRow("SELECT password FROM users WHERE username = $1", username).Scan(&hashedPassword)
if err != nil {
log.Println("Unable to find row with username: " + username)
log.Println(err)
@ -135,7 +134,7 @@ func createSessionCookie(app *app.App, w http.ResponseWriter, username string) (
// If the auth_token column for any user matches the token, set existingAuthToken to true
var existingAuthToken bool
err = app.Db.QueryRow("SELECT EXISTS(SELECT 1 FROM User WHERE AuthToken = $1)", token).Scan(&existingAuthToken)
err = app.Db.QueryRow("SELECT EXISTS(SELECT 1 FROM users WHERE auth_token = $1)", token).Scan(&existingAuthToken)
if err != nil {
log.Println("Error checking for existing auth token")
log.Println(err)
@ -149,7 +148,7 @@ func createSessionCookie(app *app.App, w http.ResponseWriter, username string) (
}
// Store token in auth_token column of the users table
_, err = app.Db.Exec("UPDATE User SET AuthToken = $1 WHERE Username = $2", token, username)
_, err = app.Db.Exec("UPDATE users SET auth_token = $1 WHERE username = $2", token, username)
if err != nil {
log.Println("Error setting auth_token column in users table")
log.Println(err)
@ -183,7 +182,7 @@ func ValidateSessionCookie(app *app.App, r *http.Request) (string, error) {
// Query row by token
var username string
err = app.Db.QueryRow("SELECT Username FROM User WHERE AuthToken = $1", cookie.Value).Scan(&username)
err = app.Db.QueryRow("SELECT username FROM users WHERE auth_token = $1", cookie.Value).Scan(&username)
if err != nil {
log.Println("Error querying row by token")
log.Println(err)
@ -204,7 +203,7 @@ func LogoutUser(app *app.App, w http.ResponseWriter, r *http.Request) {
}
// Set token to empty string
sqlStatement := "UPDATE User SET AuthToken = $1 WHERE AuthToken = $2"
sqlStatement := "UPDATE users SET auth_token = $1 WHERE auth_token = $2"
_, err = app.Db.Exec(sqlStatement, "", cookie.Value)
if err != nil {
log.Println("Error setting auth_token column in users table")