Skip to content

Commit

Permalink
minor fixes v2.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnyu committed Jul 2, 2024
1 parent e9c718b commit 44426a4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
3 changes: 3 additions & 0 deletions controllers/errorController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controllers

import (
"log"

"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
Expand All @@ -9,6 +11,7 @@ func throwError(code int, msg string, c *gin.Context) {
session := sessions.Default(c)
session.AddFlash(msg, "WARNING")
ila := isLoggedIn(c)
log.Printf("[ERROR] %d %s", code, msg)
c.HTML(code, "error.html", gin.H{"flash": session.Flashes("WARNING"), "code": code, "isLoggedIn": ila})
session.Save()
}
Expand Down
1 change: 0 additions & 1 deletion controllers/sessionController.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ func DumpSession(c *gin.Context) {
adminCookie := session.Get(isAdmin).(bool)
sessionCookies.IsAdmin = adminCookie
c.JSON(200, sessionCookies)
return
}
1 change: 0 additions & 1 deletion controllers/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func AuthenticateUser(c *gin.Context) {

var authUser = UserForm{}
if err := c.Bind(&authUser); err != nil {
log.Println(err.Error())
throwError(http.StatusBadRequest, err.Error(), c)
return
}
Expand Down
9 changes: 8 additions & 1 deletion medialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ func main() {
}
} else {

log.Println("Getting Configuration")
env, err := config.GetEnvironment(configuration, environment)
if err != nil {
panic(err)
}

logFile, err := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
defer logFile.Close()
log.SetOutput(logFile)

log.Println("Medialog starting up")
log.Println("Setting Up Router")

r, err = router.SetupRouter(env, gormDebug, prod)
Expand Down
8 changes: 7 additions & 1 deletion migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/nyudlts/go-medialog/models"
)

const version = "v0.2.3-beta"
const version = "v0.2.5-beta"

var (
test bool
Expand All @@ -26,6 +26,7 @@ var (
conf string
sqlite bool
compare bool
clearSessions bool
)

func init() {
Expand All @@ -37,6 +38,7 @@ func init() {
flag.StringVar(&migrateTable, "migrate-table", "", "migrate a table")
flag.BoolVar(&createAdmin, "create-admin", false, "")
flag.BoolVar(&compare, "compare-data", false, "")
flag.BoolVar(&clearSessions, "clear-sessions", false, "")
}

func main() {
Expand Down Expand Up @@ -82,6 +84,10 @@ func main() {

db := database.GetDB()

if clearSessions {
fmt.Println("Clearing sessions")
}

if migrateTable != "" {

switch migrateTable {
Expand Down
31 changes: 20 additions & 11 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"io"
"log"
"os"

"github.com/gin-contrib/sessions"
Expand All @@ -13,17 +14,21 @@ import (
)

func SetupRouter(env config.Environment, gormDebug bool, prod bool) (*gin.Engine, error) {
/*
//configure
gin.DisableConsoleColor()
f, _ := os.Create(env.LogLocation)
defer f.Close()
gin.DefaultWriter = io.MultiWriter(f)
*/

log.Println("Medialog starting up")

log.Println(" ** Configuring Gin logger")
//configure logger
gin.DisableConsoleColor()
f, _ := os.OpenFile(env.LogLocation, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
defer f.Close()
gin.DefaultWriter = io.MultiWriter(f)

if prod {
gin.SetMode(gin.ReleaseMode)
}

log.Println(" ** Setting up router")
//initialize the router
r := gin.Default()

Expand All @@ -36,20 +41,24 @@ func SetupRouter(env config.Environment, gormDebug bool, prod bool) (*gin.Engine
r.Static("/public", "./public")
r.SetTrustedProxies([]string{"127.0.0.1"})

log.Println(" ** Connecting to database")
//connect the database
if err := database.ConnectMySQL(env.DatabaseConfig, gormDebug); err != nil {
os.Exit(2)
}

//configure session parametes
log.Println(" ** Configuring sessions")
//configure session parameters

store := gormsessions.NewStore(database.GetDB(), true, []byte("secret"))
options := sessions.Options{}
options.HttpOnly = true
options.Domain = "127.0.0.1"
options.MaxAge = 3600
r.Use(sessions.Sessions("mysession", store))

//load applicatin routes
//load application routes
log.Println(" ** Loading routes")
LoadRoutes(r)

return r, nil
Expand Down Expand Up @@ -79,15 +88,15 @@ func SetupSQRouter(env config.SQLiteEnv, gormDebug bool) (*gin.Engine, error) {
os.Exit(2)
}

//configure session parametes
//configure session parameters
store := gormsessions.NewStore(database.GetDB(), true, []byte("secret"))
options := sessions.Options{}
options.HttpOnly = true
options.Domain = "127.0.0.1"
options.MaxAge = 3600
r.Use(sessions.Sessions("mysession", store))

//load applicatin routes
//load application routes
LoadRoutes(r)

return r, nil
Expand Down
2 changes: 1 addition & 1 deletion templates/global/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="container">
<span class="text-muted ">
<div class="float-right">
<a href="https://github.com/nyudlts/go-medialog">go-medialog v0.2.4-beta</a>
<a href="https://github.com/nyudlts/go-medialog">go-medialog v0.2.5-beta</a>
</div>
</span>
</div>
Expand Down

0 comments on commit 44426a4

Please sign in to comment.