Exemple #1
0
func main() {
	m := armor.New("sticker", "1.0")
	r := m.GinRouter()

	backend := m.Config.GetString("backend")
	addHeaders := m.Config.Get("add_headers").(map[interface{}]interface{})
	removeHeaders := m.Config.Get("remove_headers").(map[interface{}]interface{})

	u, err := url.Parse(backend)
	if err != nil {
		m.Log.Fatalf("Cannot parse backend url %s, error: %s", u, err)
	}

	proxy := httputil.NewSingleHostReverseProxy(u)
	proxy.Transport = &HeaderPokingTransport{
		RemoveHeaders: removeHeaders,
		AddHeaders:    addHeaders,
	}

	r.GET("/api/*any", func(c *gin.Context) {
		defer m.Metrics.Timed("timed.request", time.Now())
		m.Metrics.Inc("request")
		c.Request.URL.Path = c.Request.URL.Path[5:]
		proxy.ServeHTTP(c.Writer, c.Request)
	})

	m.Run(r)
}
Exemple #2
0
func main() {
	arm := armor.New("resizer", "1.0")
	r := arm.GinRouter()

	client := &http.Client{}

	r.GET("/", func(c *gin.Context) {
		defer arm.Metrics.Timed("timed.request", time.Now())
		arm.Metrics.Inc("foobar")
		c.String(200, "hello world")
	})

	r.GET("/json", func(c *gin.Context) {
		start := time.Now()
		resp, err := client.Get("https://upload.wikimedia.org/wikipedia/commons/c/cb/Pocket-Gopher_Ano-Nuevo-SP.jpg")
		arm.Metrics.Timed("image.fetch", start)
		if err != nil {
			c.JSON(200, gin.H{"error": "true"})
			return
		}

		buffer, _ := ioutil.ReadAll(resp.Body)
		newImage, _ := bimg.NewImage(buffer).Resize(800, 600)
		arm.Metrics.Timed("image.resize", start)

		c.Data(200, "image/jpeg", newImage)

	})

	r.GET("/xml", func(c *gin.Context) {
		c.XML(200, gin.H{"message": "hey", "status": 200})
	})

	arm.Run(r)
}
Exemple #3
0
func main() {
	m := armor.New("redis", "1.0")
	r := m.GinRouter()

	r.GET("/", func(c *gin.Context) {
		defer m.Metrics.Timed("timed.request", time.Now())
		m.Metrics.Inc("foobar")
		c.String(200, "hello world")
	})

	m.Run(r)
}
Exemple #4
0
func main() {
	arm := armor.New("reverseproxy", "1.0")
	r := arm.GinRouter()

	//XXX goquery
	r.GET("/", func(c *gin.Context) {
		defer arm.Metrics.Timed("timed.request", time.Now())
		arm.Metrics.Inc("foobar")
		c.String(200, "hello world")
	})

	arm.Run(r)
}
Exemple #5
0
func main() {
	runtime.GOMAXPROCS(4)
	arm := armor.New("bench", "1.0")
	r := arm.GinRouter()

	r.GET("/", func(c *gin.Context) {
		defer arm.Metrics.Timed("timed.request", time.Now())
		arm.Metrics.Inc("bench")
		c.String(200, "bench me")
	})

	arm.Run(r)
}
Exemple #6
0
func main() {
	m := armor.New("rpc", "1.0")
	r := m.GinRouter()

	r.GET("/", func(c *gin.Context) {
		defer m.Metrics.Timed("timed.request", time.Now())
		m.Metrics.Inc("foobar")
		c.String(200, "hello world")
	})

	m.RunWithRPC(r,
		func(s *grpc.Server) {
			pb.RegisterGreeterServer(s, &HelloServer{m})
		})
}
Exemple #7
0
func main() {
	arm := armor.New("producer", "1.0")
	r := arm.GinRouter()

	amqp := NewAmqp(arm.Config.GetString("amqp"), arm.Config.GetString("exchange"))
	err := amqp.Dial()
	if err != nil {
		arm.Log.Fatalf("Cannot dial amqp AMQP: %s", err)
	}

	err = amqp.Declare("usage-logs")
	if err != nil {
		arm.Log.Fatalf("Cannot declare a queue: %s", err)
	}

	go amqp.AutoHeal()

	r.GET("/", func(c *gin.Context) {
		defer arm.Metrics.Timed("amqp.produce_sample", time.Now())
		err := amqp.Publish("usage-logs", []byte("test message"))
		if err != nil {
			c.String(500, fmt.Sprintf("error: %s", err))
			return
		}

		c.String(200, "hello world")
	})

	r.POST("/", func(c *gin.Context) {
		defer arm.Metrics.Timed("amqp.produce_post_request", time.Now())
		bytes, err := ioutil.ReadAll(c.Request.Body)
		if err != nil {
			arm.Log.Error("cannot publish", err)
		}
		amqp.Publish("usage-logs", bytes)
		c.String(200, "hello world")
	})

	arm.Run(r)
}