diff --git a/controllers/errorController.go b/controllers/errorController.go index aadb352..76af0f7 100644 --- a/controllers/errorController.go +++ b/controllers/errorController.go @@ -1,6 +1,8 @@ package controllers import ( + "log" + "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) @@ -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() } diff --git a/controllers/sessionController.go b/controllers/sessionController.go index 553e8e8..e0aeb64 100644 --- a/controllers/sessionController.go +++ b/controllers/sessionController.go @@ -83,5 +83,4 @@ func DumpSession(c *gin.Context) { adminCookie := session.Get(isAdmin).(bool) sessionCookies.IsAdmin = adminCookie c.JSON(200, sessionCookies) - return } diff --git a/controllers/userController.go b/controllers/userController.go index ef3221d..7dbf840 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -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 } diff --git a/medialog.go b/medialog.go index e3fdf8b..2e2a25b 100644 --- a/medialog.go +++ b/medialog.go @@ -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) diff --git a/migration/migrate.go b/migration/migrate.go index fc42532..f54dbde 100644 --- a/migration/migrate.go +++ b/migration/migrate.go @@ -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 @@ -26,6 +26,7 @@ var ( conf string sqlite bool compare bool + clearSessions bool ) func init() { @@ -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() { @@ -82,6 +84,10 @@ func main() { db := database.GetDB() + if clearSessions { + fmt.Println("Clearing sessions") + } + if migrateTable != "" { switch migrateTable { diff --git a/router/router.go b/router/router.go index 88c43d0..2c1e018 100644 --- a/router/router.go +++ b/router/router.go @@ -2,6 +2,7 @@ package router import ( "io" + "log" "os" "github.com/gin-contrib/sessions" @@ -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() @@ -36,12 +41,15 @@ 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 @@ -49,7 +57,8 @@ func SetupRouter(env config.Environment, gormDebug bool, prod bool) (*gin.Engine options.MaxAge = 3600 r.Use(sessions.Sessions("mysession", store)) - //load applicatin routes + //load application routes + log.Println(" ** Loading routes") LoadRoutes(r) return r, nil @@ -79,7 +88,7 @@ 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 @@ -87,7 +96,7 @@ func SetupSQRouter(env config.SQLiteEnv, gormDebug bool) (*gin.Engine, error) { options.MaxAge = 3600 r.Use(sessions.Sessions("mysession", store)) - //load applicatin routes + //load application routes LoadRoutes(r) return r, nil diff --git a/templates/global/footer.html b/templates/global/footer.html index 93428db..66adf00 100644 --- a/templates/global/footer.html +++ b/templates/global/footer.html @@ -4,7 +4,7 @@