package router

import (
	"encoding/gob"
	"net/http"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	//"github.com/gin-contrib/sessions/memstore"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
)

var Router = gin.Default()

func init() {
	store := cookie.NewStore([]byte("jy_data_extract"))
	Router.Use(sessions.Sessions("mysession", store))
	gob.Register(&map[string]interface{}{})
	gob.Register(&[]map[string]interface{}{})
	gob.Register(&time.Time{})
	gob.Register(bson.ObjectIdHex("518781f5904a2ea61544f852"))
}

func AuthMiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		session := sessions.Default(c)
		if session.Get("user") != nil {
			c.Next()
			return
		}
		c.JSON(http.StatusUnauthorized, gin.H{
			"error": "Unauthorized",
		})
		c.Abort()
		return
	}
}
