Exemplo n.º 1
0
func main() {
	r := gin.Default()
	r.POST("/event", func(c *gin.Context) {
		messagesReceived.Inc()

		defer c.Request.Body.Close()
		body, err := ioutil.ReadAll(c.Request.Body)
		if err != nil {
			log.WithFields(log.Fields{
				"origErr": err.Error(),
			}).Error("could not read request body")
			messagesErrored.Inc()
			return
		}

		if *debug {
			log.WithFields(log.Fields{
				"client": c.ClientIP(),
				"body":   string(body),
			}).Debug("received request")
		}

		err = forwardToSNS(body)

		if err != nil {
			if awsErr, ok := err.(awserr.Error); ok {
				log.WithFields(log.Fields{
					"code":    awsErr.Code(),
					"origErr": awsErr.OrigErr(),
				}).Error(awsErr.Message())
				if reqErr, ok := err.(awserr.RequestFailure); ok {
					log.WithFields(log.Fields{
						"code":       reqErr.Code(),
						"statusCode": reqErr.StatusCode(),
						"requestId":  reqErr.RequestID(),
					}).Error(reqErr.Message())
					c.String(http.StatusInternalServerError, "ERR")
					messagesErrored.Inc()
					return
				}
				c.String(http.StatusBadGateway, "ERR")
			} else {
				log.Error(err.Error())
				c.String(http.StatusInternalServerError, "ERR")
			}
			messagesErrored.Inc()
			return
		}

		log.WithFields(log.Fields{
			"body": string(body),
		}).Info("successfully forwarded message")
		c.String(http.StatusNoContent, "")
	})

	r.GET("/metrics", gin.WrapH(prometheus.Handler()))

	r.Run(":8080")
}
Exemplo n.º 2
0
func InitBridge(b *Bridge) {
	fmt.Println(bridge)
	if bridge != nil {
		fmt.Println("bridge already initialized")
	}
	bridge = b
	bridge.isInit = true
	bridge.Gin.GET("/socket.io/", gin.WrapH(bridge.SocketServer))
}
Exemplo n.º 3
0
func (mm *ModuleManager) RegisterRouterGroup(group *gin.RouterGroup) {
	group.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, (*mm).GetAllModules())
	})

	group.POST("/", func(c *gin.Context) {
		var req shared.Module
		err := c.BindJSON(&req)
		if err != nil {
			c.JSON(400, shared.HttpError{"Decoding HTTP body failed.", err})
			return
		}

		if strings.TrimSpace(req.ModuleURL) == "" {
			c.JSON(400, shared.HttpError{"Attribute 'module-url' is missing.", nil})
			return
		}

		if mm.GetModule(req.Metadata.ID) != nil {
			c.JSON(423, shared.HttpError{"Module already registered.", nil})
			return
		}

		mm.AddModule(&req)
	})

	group.Any("/:module/proxy/*path", func(c *gin.Context) {
		module := mm.GetModule(c.Param("module"))
		if module == nil {
			c.JSON(404, shared.HttpError{"Module not found.", nil})
			return
		}

		target, err := url.Parse(module.ModuleURL)
		if module == nil {
			c.JSON(500, shared.HttpError{"Error connecting to module.", err})
			return
		}

		oldPrefix := fmt.Sprintf("%s/%s/proxy", group.BasePath(), module.Metadata.ID)
		newPrefix := "/module"

		var handler http.Handler
		handler = httputil.NewSingleHostReverseProxy(target)
		handler = HTTPAddPrefix(newPrefix, handler)
		handler = http.StripPrefix(oldPrefix, handler)

		wrapper := gin.WrapH(handler)
		wrapper(c)
	})
}
Exemplo n.º 4
0
// TokenLoginEndpointGen Hand out tokens for valid username-password
// and maybe some other stuff
func TokenEndpointGen(host *url.URL) gin.HandlerFunc {
	return gin.WrapH(httputil.NewSingleHostReverseProxy(host))
}