Compare commits

...

5 Commits

7 changed files with 153 additions and 17 deletions

View File

@ -9,11 +9,12 @@ 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"`
Ip string `json:"DbIp"`
Port string `json:"DbPort"`
Name string `json:"DbName"`
User string `json:"DbUser"`
Password string `json:"DbPassword"`
AutoMigrate bool `json:"DbAutoMigrate"`
}
Listen struct {

View File

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

105
database/migrate.go Normal file
View File

@ -0,0 +1,105 @@
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,7 +4,8 @@
"DbPort": "5432",
"DbName": "database",
"DbUser": "user",
"DbPassword": "password"
"DbPassword": "password",
"AutoMigrate": true
},
"Listen": {
"HttpIp": "127.0.0.1",

10
main.go
View File

@ -4,6 +4,7 @@ import (
"GoWeb/app"
"GoWeb/config"
"GoWeb/database"
"GoWeb/models"
"GoWeb/routes"
"embed"
"log"
@ -37,8 +38,15 @@ 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
// Connect to database and run migrations
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)

21
models/migrations.go Normal file
View File

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