Skip to content

Commit

Permalink
Merge branch 'v1.0.3' into 'master'
Browse files Browse the repository at this point in the history
V1.0.3

See merge request cploutarchou/MicroGO!48
  • Loading branch information
cploutarchou committed Aug 8, 2022
2 parents 04221b2 + c555390 commit 739212a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ app/migrations/*.sql
cache/testdata/tmp/badger/*
app/views/login.jet
app/views/forgot.jet
app/views/reset-password.jet
app/views/reset-password.jet
.vscode/*
3 changes: 3 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 38 additions & 16 deletions microGo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}
Expand All @@ -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 {
Expand All @@ -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").
Expand Down
10 changes: 5 additions & 5 deletions terminal/cli/templates/env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion terminal/cli/templates/handlers/auth-handlers.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion terminal/cli/templates/middleware/auth-token.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion terminal/cli/templates/views/reset-password.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2 class="mt-5 text-center">Reset Password</h2>
>

<input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
<input type="hidden" name="email" value="{{email}}">
<input type="hidden" name="email" value="">

<div class="mb-3">
<label for="password" class="form-label">Password</label>
Expand Down

0 comments on commit 739212a

Please sign in to comment.