Exemple #1
0
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")
}
Exemple #2
0
// 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)
}
Exemple #3
0
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"))
}
Exemple #4
0
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"))
}
Exemple #5
0
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"))
}
Exemple #6
0
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())
}
Exemple #7
0
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())
}
Exemple #8
0
func newServer() *gin.Engine {
	g := gin.New()
	g.Use(Middleware(Options{}))

	return g
}
Exemple #9
0
// 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
}