コード例 #1
0
ファイル: main.go プロジェクト: ZhuZhengyi/eris-db
func StartGin() {
	gin.SetMode(gin.ReleaseMode)

	router := gin.New()
	router.Use(rateLimit, gin.Recovery())
	router.LoadHTMLGlob("resources/*.templ.html")
	router.Static("/static", "resources/static")
	router.GET("/", index)
	router.GET("/room/:roomid", roomGET)
	router.POST("/room-post/:roomid", roomPOST)
	router.GET("/stream/:roomid", streamRoom)

	router.Run(":80")
}
コード例 #2
0
ファイル: hello.go プロジェクト: ZhuZhengyi/eris-db
// This function's name is a must. App Engine uses it to drive the requests properly.
func init() {
	// Starts a new Gin instance with no middle-ware
	r := gin.New()

	// Define your handlers
	r.GET("/", func(c *gin.Context) {
		c.String(200, "Hello World!")
	})
	r.GET("/ping", func(c *gin.Context) {
		c.String(200, "pong")
	})

	// Handle all requests using net/http
	http.Handle("/", r)
}
コード例 #3
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func TestMaxAge(t *testing.T) {
	g := gin.New()
	g.Use(Middleware(Options{
		MaxAge: time.Hour,
	}))
	assert := assert.New(t)

	r := request(g, requestOptions{
		Method: "OPTIONS",
		URL:    "/",
		Headers: map[string]string{
			"Origin": "http://maji.moe",
		},
	})

	assert.Equal("3600", r.Header().Get("Access-Control-Max-Age"))
}
コード例 #4
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func TestAllowHeaders(t *testing.T) {
	g := gin.New()
	g.Use(Middleware(Options{
		AllowHeaders: []string{"X-Custom-Header", "X-Auth-Token"},
	}))
	assert := assert.New(t)

	r := request(g, requestOptions{
		Method: "OPTIONS",
		URL:    "/",
		Headers: map[string]string{
			"Origin": "http://maji.moe",
		},
	})

	assert.Equal("X-Custom-Header,X-Auth-Token", r.Header().Get("Access-Control-Allow-Headers"))
}
コード例 #5
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func TestAllowMethods(t *testing.T) {
	g := gin.New()
	g.Use(Middleware(Options{
		AllowMethods: []string{"GET", "POST", "PUT"},
	}))
	assert := assert.New(t)

	r := request(g, requestOptions{
		Method: "OPTIONS",
		URL:    "/",
		Headers: map[string]string{
			"Origin": "http://maji.moe",
		},
	})

	assert.Equal("GET,POST,PUT", r.Header().Get("Access-Control-Allow-Methods"))
}
コード例 #6
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func TestAllowCredentials(t *testing.T) {
	g := gin.New()
	g.Use(Middleware(Options{
		AllowCredentials: true,
	}))
	assert := assert.New(t)

	g.GET("/test", func(c *gin.Context) {
		c.String(http.StatusOK, "OK")
	})

	r := request(g, requestOptions{
		URL: "/test",
		Headers: map[string]string{
			"Origin": "http://maji.moe",
		},
	})

	assert.Equal("true", r.Header().Get("Access-Control-Allow-Credentials"))
	assert.Equal("OK", r.Body.String())
}
コード例 #7
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func TestExposeHeaders(t *testing.T) {
	g := gin.New()
	g.Use(Middleware(Options{
		ExposeHeaders: []string{"Foo", "Bar"},
	}))
	assert := assert.New(t)

	g.GET("/test", func(c *gin.Context) {
		c.String(http.StatusOK, "OK")
	})

	r := request(g, requestOptions{
		URL: "/test",
		Headers: map[string]string{
			"Origin": "http://maji.moe",
		},
	})

	assert.Equal("Foo,Bar", r.Header().Get("Access-Control-Expose-Headers"))
	assert.Equal("OK", r.Body.String())
}
コード例 #8
0
ファイル: cors_test.go プロジェクト: ZhuZhengyi/eris-db
func newServer() *gin.Engine {
	g := gin.New()
	g.Use(Middleware(Options{}))

	return g
}
コード例 #9
0
ファイル: server.go プロジェクト: ZhuZhengyi/eris-db
// Initializes all the servers and starts listening for connections.
func (this *ServeProcess) Start() error {

	router := gin.New()

	config := this.config

	InitLogger(config)

	ch := NewCORSMiddleware(config.CORS)
	router.Use(gin.Recovery(), logHandler, contentTypeMW, ch)

	address := config.Bind.Address
	port := config.Bind.Port

	if port == 0 {
		return fmt.Errorf("0 is not a valid port.")
	}

	listenAddress := address + ":" + fmt.Sprintf("%d", port)
	srv := &graceful.Server{
		Server: &http.Server{
			Handler: router,
		},
	}

	// Start the servers/handlers.
	for _, s := range this.servers {
		s.Start(config, router)
	}

	var lst net.Listener
	l, lErr := net.Listen("tcp", listenAddress)
	if lErr != nil {
		return lErr
	}

	// For secure connections.
	if config.TLS.TLS {
		addr := srv.Addr
		if addr == "" {
			addr = ":https"
		}

		tConfig := &tls.Config{}
		if tConfig.NextProtos == nil {
			tConfig.NextProtos = []string{"http/1.1"}
		}

		var tErr error
		tConfig.Certificates = make([]tls.Certificate, 1)
		tConfig.Certificates[0], tErr = tls.LoadX509KeyPair(config.TLS.CertPath, config.TLS.KeyPath)
		if tErr != nil {
			return tErr
		}

		lst = tls.NewListener(l, tConfig)
	} else {
		lst = l
	}
	this.srv = srv
	log.Info("Server started.")
	for _, c := range this.startListenChans {
		c <- struct{}{}
	}
	// Start the serve routine.
	go func() {
		this.srv.Serve(lst)
		for _, s := range this.servers {
			s.ShutDown()
		}
	}()
	// Listen to the process stop event, it will call 'Stop'
	// on the graceful Server. This happens when someone
	// calls 'Stop' on the process.
	go func() {
		<-this.stopChan
		log.Info("Close signal sent to server.")
		this.srv.Stop(killTime)
	}()
	// Listen to the servers stop event. It is triggered when
	// the server has been fully shut down.
	go func() {
		<-this.srv.StopChan()
		log.Info("Server stop event fired. Good bye.")
		for _, c := range this.stopListenChans {
			c <- struct{}{}
		}
	}()
	return nil
}