예제 #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
// 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
}