Skip to content

Commit

Permalink
Recovered removed helper files and removed unnecessary endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgomes28 committed Apr 29, 2024
1 parent 364c20b commit a725a85
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 63 deletions.
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
r.POST("/contact-send", makeContactFormHandler())

r.Static("/static", "./static")
r.Static("/media", "./media")
return r
}

Expand Down
62 changes: 0 additions & 62 deletions common/app_settings.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package common

import (
"fmt"
"os"
"strconv"

"github.com/BurntSushi/toml"
)

Expand All @@ -31,64 +27,6 @@ type AppSettings struct {
Shortcodes []Shortcode `toml:"shortcodes"`
}

func LoadSettings() (AppSettings, error) {
// Want to load the environment variables
database_address := os.Getenv("URCHIN_DATABASE_ADDRESS")
if len(database_address) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_ADDRESS is not defined")
}

database_user := os.Getenv("URCHIN_DATABASE_USER")
if len(database_user) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_USER is not defined")
}

database_password := os.Getenv("URCHIN_DATABASE_PASSWORD")
if len(database_password) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_PASSWORD is not defined")
}

database_name := os.Getenv("URCHIN_DATABASE_NAME")
if len(database_name) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_NAME is not defined")
}

database_port_str := os.Getenv("URCHIN_DATABASE_PORT")
if len(database_port_str) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_PORT is not defined")
}

database_port, err := strconv.Atoi(database_port_str)
if err != nil {
return AppSettings{}, fmt.Errorf("URCHIN_DATABASE_PORT is not a valid integer: %v", err)
}

webserver_port_str := os.Getenv("URCHIN_WEBSERVER_PORT")
if webserver_port_str == "" {
return AppSettings{}, fmt.Errorf("URCHIN_WEBSERVER_PORT is not defined")
}

webserver_port, err := strconv.Atoi(webserver_port_str)
if err != nil {
return AppSettings{}, fmt.Errorf("URCHIN_WEBSERVER_PORT is not valid: %v", err)
}

image_directory := os.Getenv("URCHIN_IMAGE_DIRECTORY")
if len(image_directory) == 0 {
return AppSettings{}, fmt.Errorf("URCHIN_IMAGE_DIRECTORY is not defined\n")
}

return AppSettings{
DatabaseUser: database_user,
DatabasePassword: database_password,
DatabaseAddress: database_address,
DatabasePort: database_port,
DatabaseName: database_name,
WebserverPort: webserver_port,
ImageDirectory: image_directory,
}, nil
}

func ReadConfigToml(filepath string) (AppSettings, error) {
var config AppSettings
_, err := toml.DecodeFile(filepath, &config)
Expand Down
94 changes: 94 additions & 0 deletions tests/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package helpers

import (
"context"
_ "database/sql"
"embed"
"fmt"
"time"

_ "github.com/go-sql-driver/mysql"

sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
"github.com/dolthub/go-mysql-server/sql"
"github.com/matheusgomes28/urchin/common"
"github.com/matheusgomes28/urchin/database"
)

//go:generate ../../../migrations ./migrations

//go:embed migrations/*.sql
var EmbedMigrations embed.FS

func RunDatabaseServer(app_settings common.AppSettings) {
pro := CreateTestDatabase(app_settings.DatabaseName)
engine := sqle.NewDefault(pro)
engine.Analyzer.Catalog.MySQLDb.AddRootAccount()

session := memory.NewSession(sql.NewBaseSession(), pro)
ctx := sql.NewContext(context.Background(), sql.WithSession(session))
ctx.SetCurrentDatabase(app_settings.DatabaseName)

config := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("%s:%d", app_settings.DatabaseAddress, app_settings.DatabasePort),
}
s, err := server.NewServer(config, engine, memory.NewSessionBuilder(pro), nil)
if err != nil {
panic(err)
}
if err = s.Start(); err != nil {
panic(err)
}
}

func CreateTestDatabase(name string) *memory.DbProvider {
db := memory.NewDatabase(name)
db.BaseDatabase.EnablePrimaryKeyIndexes()

pro := memory.NewDBProvider(db)
return pro
}

func WaitForDb(app_settings common.AppSettings) (database.SqlDatabase, error) {

for range 400 {
database, err := database.MakeSqlConnection(
app_settings.DatabaseUser,
app_settings.DatabasePassword,
app_settings.DatabaseAddress,
app_settings.DatabasePort,
app_settings.DatabaseName,
)

if err == nil {
return database, nil
}

time.Sleep(25 * time.Millisecond)
}

return database.SqlDatabase{}, fmt.Errorf("database did not start")
}

// GetAppSettings gets the settings for the http servers
// taking into account a unique port. Very hacky way to
// get a unique port: manually have to pass a new number
// for every test...
// TODO : Find a way to assign a unique port at compile
//
// time
func GetAppSettings(app_num int) common.AppSettings {
app_settings := common.AppSettings{
DatabaseAddress: "localhost",
DatabasePort: 3336 + app_num, // Initial port
DatabaseUser: "root",
DatabasePassword: "",
DatabaseName: "urchin",
WebserverPort: 8080,
}

return app_settings
}
35 changes: 35 additions & 0 deletions tests/helpers/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package helpers

import (
"fmt"
"image"
"io"
"mime/multipart"
"net/textproto"
)

func CreateFormImagePart(writer *multipart.Writer, fieldname string, filename string, contentType string) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldname, filename))
h.Set("Content-Type", contentType)
return writer.CreatePart(h)
}

func CreateTextFormHeader(writer *multipart.Writer, fieldname string) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"`, fieldname))
return writer.CreatePart(h)
}

// Creating an image in memory for testing: https://yourbasic.org/golang/create-image/
func CreateImage() image.Image {
width := 1
height := 1

upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}

return image.NewRGBA(image.Rectangle{upLeft, lowRight})
}

0 comments on commit a725a85

Please sign in to comment.