Esempio n. 1
0
// CORS allow cross domain resources sharing
func CORS() gin.HandlerFunc {
	config := cors.Config{}
	config.AllowedHeaders = []string{"Content-Type", "Auth-Token", "X-Geetest-Challenge", "X-Geetest-Validate", "X-Geetest-Seccode"}
	config.AllowedMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"}
	config.AbortOnError = true
	config.AllowAllOrigins = true
	config.AllowCredentials = true
	config.MaxAge = time.Hour * 12
	return cors.New(config)
}
Esempio n. 2
0
/*
CORS allow CORS.
See: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
*/
func CORS() gin.HandlerFunc {
	return cors.New(cors.Config{
		AbortOnError:    false,
		AllowAllOrigins: true,
		// AllowedOrigins:   []string{"*"}, // TODO: set GUI url
		AllowedMethods:   []string{"GET", "POST", "PUT", "PATCH", "HEAD", "OPTIONS"},
		AllowedHeaders:   []string{"Content-Type", "Authorization"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	})
}
Esempio n. 3
0
func Express(p string) *GinExpress {

	env := EnvironmentFromString(os.Getenv("APPLICATION_ENV"))
	gin.SetMode(env.Mode())

	debug("Mode: %s", env.String())
	debug("Base path: %s", p)

	var (
		CONFIG_PATH = path.Join(p, "config")
		VIEWS_PATH  = path.Join(p, "views")
		STATIC_PATH = path.Join(p, "public")
	)

	c, err := NewConfig(env, CONFIG_PATH)
	if err != nil {
		panic(err)
	}

	s := &GinExpress{}
	s.Engine = gin.New()
	s.Config = c

	s.Engine.Use(gin.Recovery())
	s.Engine.HTMLRender = NewHtmlMaster(VIEWS_PATH)

	lc, _ := c.Get("config.log")
	if lc != nil {

		s.Logger = NewLogger(&LogConfig{
			lc.UString("level"),
			lc.UString("file"),
			lc.UString("format"),
			lc.UBool("rotate", true),
		})

	} else {
		s.Logger = NewDefaultLogger()
	}

	s.Engine.Use(func(ctx *gin.Context) {
		start := time.Now()
		path := ctx.Request.URL.Path

		ctx.Next()

		end := time.Now()
		latency := end.Sub(start)

		clientIP := ctx.ClientIP()
		method := ctx.Request.Method
		statusCode := ctx.Writer.Status()
		comment := ctx.Errors.ByType(gin.ErrorTypePrivate).String()

		s.Logger.Info(statusCode,
			latency,
			clientIP,
			method,
			path,
			comment,
		)
	})

	cc, _ := c.Get("config.cors")
	if cc != nil {

		all := false
		origins := interfaceToString(cc.UList("allowed.origins"))
		if len(origins) == 0 || origins[0] == "*" {
			all = true
		}

		methods := interfaceToString(cc.UList("allowed.methods"))
		if len(methods) == 0 {
			methods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
		}

		headers := interfaceToString(cc.UList("allowed.headers"))
		exposed := interfaceToString(cc.UList("exposed"))

		s.Engine.Use(cors.New(cors.Config{
			AbortOnError:     false,
			AllowAllOrigins:  all,
			AllowedOrigins:   origins,
			AllowedMethods:   methods,
			AllowedHeaders:   headers,
			ExposedHeaders:   exposed,
			AllowCredentials: cc.UBool("credentials", false),
			MaxAge:           time.Duration(cc.UInt("max_age", int(12*time.Hour))),
		}))
	}

	sc, _ := c.Get("config.smtp")
	if sc != nil {

		s.Mailer = NewSimpleMailer(&MailerConfig{
			sc.UString("host"),
			sc.UInt("port"),
			sc.UString("username"),
			sc.UString("password"),
			&mail.Address{sc.UString("from.name"), sc.UString("from.email")},
			VIEWS_PATH,
		})
	}

	ss, _ := c.Get("config.session")
	if ss != nil {

		n := ss.UString("name", "go-default-session")

		if sess, err := session(ss); err != nil {

			debug("[ERR] Session", err.Error())

		} else {

			s.Engine.Use(sessions.Sessions(n, sess))
		}

	}

	s.Engine.Use(Gzip(DefaultCompression))

	s.Engine.Static("/public", STATIC_PATH)
	return s
}