diff --git a/.gitignore b/.gitignore index bf5934a..843d489 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ app/migrations/*.sql cache/testdata/tmp/badger/* app/views/login.jet app/views/forgot.jet -app/views/reset-password.jet \ No newline at end of file +app/views/reset-password.jet +.vscode/* \ No newline at end of file diff --git a/driver.go b/driver.go index 6ae11c8..76ed61a 100644 --- a/driver.go +++ b/driver.go @@ -37,6 +37,9 @@ func (m *MicroGo) OpenDB(driverName, dataSourceName string) (*sql.DB, error) { driverName = "pgx" } + if driverName == "mysql" || driverName == "mariadb" { + driverName = "mysql" + } db, err := sql.Open(driverName, dataSourceName) if err != nil { return nil, err diff --git a/microGo.go b/microGo.go index e4443d7..c716733 100644 --- a/microGo.go +++ b/microGo.go @@ -3,6 +3,13 @@ package MicroGO import ( "database/sql" "fmt" + "log" + "net/http" + "os" + "strconv" + "strings" + "time" + "github.com/CloudyKit/jet/v6" "github.com/alexedwards/scs/v2" "github.com/cploutarchou/MicroGO/cache" @@ -15,15 +22,9 @@ import ( "github.com/joho/godotenv" "github.com/kataras/blocks" "github.com/robfig/cron/v3" - "log" - "net/http" - "os" - "strconv" - "strings" - "time" ) -const version = "1.0.1" +const version = "1.0.3" var ( redisCache *cache.RedisCache @@ -105,10 +106,24 @@ func (m *MicroGo) New(rootPath string) error { // Database connection if os.Getenv("DATABASE_TYPE") != "" { - db, err := m.OpenDB(os.Getenv("DATABASE_TYPE"), m.BuildDataSourceName()) - if err != nil { - errorLog.Println(err) - os.Exit(1) + var db *sql.DB + switch os.Getenv("DATABASE_TYPE") { + case "": + m.ErrorLog.Println("DATABASE_TYPE is not set") + + case "mysql", "mariadb": + db, err = m.OpenDB("mysql", m.BuildDataSourceName()) + if err != nil { + errorLog.Println(err) + os.Exit(1) + } + case "postgres", "postgresql": + db, err = m.OpenDB("postgres", m.BuildDataSourceName()) + if err != nil { + errorLog.Println(err) + os.Exit(1) + } + } m.DB = Database{ DatabaseType: os.Getenv("DATABASE_TYPE"), @@ -273,7 +288,7 @@ func (m *MicroGo) checkDotEnv(path string) error { return nil } -//startLoggers Initializes all loggers for microGo application. +// startLoggers Initializes all loggers for microGo application. func (m *MicroGo) startLoggers() (*log.Logger, *log.Logger, *log.Logger, *log.Logger) { var infoLog *log.Logger var errorLog *log.Logger @@ -286,7 +301,7 @@ func (m *MicroGo) startLoggers() (*log.Logger, *log.Logger, *log.Logger, *log.Lo return infoLog, errorLog, warnLog, buildLog } -//createRenderer Create a Renderer for microGo application. +// createRenderer Create a Renderer for microGo application. func (m *MicroGo) createRenderer() { renderer := render.Render{ Renderer: m.config.renderer, @@ -299,7 +314,7 @@ func (m *MicroGo) createRenderer() { m.Render = &renderer } -//createRenderer Create a Renderer for microGo application. +// createRenderer Create a Renderer for microGo application. func (m *MicroGo) createMailer() mailer.Mailer { port, _ := strconv.Atoi(os.Getenv("SMTP_PORT")) _mailer := mailer.Mailer{ @@ -336,11 +351,18 @@ func (m *MicroGo) BuildDataSourceName() string { if os.Getenv("DATABASE_PASS") != "" { dsn = fmt.Sprintf("%s password=%s", dsn, os.Getenv("DATABASE_PASS")) } - + return dsn + case "mysql", "mariadb": + return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", + os.Getenv("DATABASE_USER"), + os.Getenv("DATABASE_PASS"), + os.Getenv("DATABASE_HOST"), + os.Getenv("DATABASE_PORT"), + os.Getenv("DATABASE_NAME")) default: } - return dsn + return "" } func (m *MicroGo) createRedisPool() *redis.Pool { diff --git a/render/render.go b/render/render.go index dbb7683..fadbf28 100644 --- a/render/render.go +++ b/render/render.go @@ -60,8 +60,7 @@ func (r *Render) Page(writer http.ResponseWriter, request *http.Request, view, l case "jet": return r.JetPage(writer, request, view, variables, data) case "blocks": - data := data.(map[string]interface{}) - return r.BlocksPage(writer, request, view, layout, nil, data) + return r.BlocksPage(writer, request, view, layout, nil, data.(map[string]interface{})) default: } @@ -87,7 +86,7 @@ func (r *Render) GoPage(writer http.ResponseWriter, request *http.Request, view return nil } -//JetPage The jet engine template renderer function. +// JetPage The jet engine template renderer function. func (r *Render) JetPage(writer http.ResponseWriter, request *http.Request, view string, variables, data interface{}) error { var vars jet.VarMap if variables == nil { @@ -114,7 +113,7 @@ func (r *Render) JetPage(writer http.ResponseWriter, request *http.Request, view return nil } -//BlocksPage The Blocks' engine template renderer function. +// BlocksPage The Blocks' engine template renderer function. func (r *Render) BlocksPage(writer http.ResponseWriter, request *http.Request, view, layout string, variables interface{}, data map[string]interface{}) error { writer.Header().Set("Content-Type", "text/html; charset=utf-8") r.BlocksViews = blocks.New("./views"). diff --git a/terminal/cli/templates/env.txt b/terminal/cli/templates/env.txt index 301ed42..e5d9012 100644 --- a/terminal/cli/templates/env.txt +++ b/terminal/cli/templates/env.txt @@ -15,18 +15,18 @@ SERVER_NAME=localhost SECURE=false # database config - postgres or mysql -DATABASE_TYPE=postgres +DATABASE_TYPE=mysql DATABASE_HOST=localhost -DATABASE_PORT=5432 -DATABASE_USER=postgres +DATABASE_PORT=3306 +DATABASE_USER=mariadb DATABASE_PASS=password DATABASE_NAME=microGo DATABASE_SSL_MODE=disable DATABASE_TIME_ZONE=UTC # redis config -REDIS_HOST=" -REDIS_PORT="6379" +REDIS_HOST=localhost +REDIS_PORT=6379 REDIS_PASSWORD= REDIS_PREFIX=test diff --git a/terminal/cli/templates/handlers/auth-handlers.go.txt b/terminal/cli/templates/handlers/auth-handlers.go.txt index 80ef79b..8df6e56 100644 --- a/terminal/cli/templates/handlers/auth-handlers.go.txt +++ b/terminal/cli/templates/handlers/auth-handlers.go.txt @@ -33,7 +33,7 @@ func (h *Handlers) PostUserLogin(w http.ResponseWriter, r *http.Request) { } email := r.Form.Get("email") password := r.Form.Get("password") - user, err := h.Models.Users.GetByEmail(email) + user, err := h.Models.User.GetByEmail(email) if err != nil { _, err := w.Write([]byte(err.Error())) diff --git a/terminal/cli/templates/middleware/auth-token.go.txt b/terminal/cli/templates/middleware/auth-token.go.txt index 0ac7da5..d9cf9b0 100644 --- a/terminal/cli/templates/middleware/auth-token.go.txt +++ b/terminal/cli/templates/middleware/auth-token.go.txt @@ -4,7 +4,7 @@ import "net/http" func (m *Middleware) AuthToken(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, err := m.Models.Tokens.AuthenticateToken(r) + _, err := m.Models.Token.AuthenticateToken(r) if err != nil { var payload struct { Error bool `json:"error"` diff --git a/terminal/cli/templates/views/reset-password.html b/terminal/cli/templates/views/reset-password.html index 24724eb..dc5ff00 100644 --- a/terminal/cli/templates/views/reset-password.html +++ b/terminal/cli/templates/views/reset-password.html @@ -19,7 +19,7 @@