<commit_before>package router

import (
	"fmt"

	"gopkg.in/alexcesaro/statsd.v2"
	"gopkg.in/gin-gonic/gin.v1"

	"github.com/ghmeier/bloodlines/config"
	"github.com/ghmeier/bloodlines/gateways"
	"github.com/ghmeier/bloodlines/handlers"
	"github.com/ghmeier/bloodlines/workers"
)

/*Bloodlines is the main server object which routes requests*/
type Bloodlines struct {
	router     *gin.Engine
	content    handlers.ContentI
	receipt    handlers.ReceiptI
	job        handlers.JobI
	trigger    handlers.TriggerI
	preference handlers.PreferenceI
	workers    []workers.Send
}

/*New returns a ready-to-run Bloodlines struct from the given config*/
func New(config *config.Root) (*Bloodlines, error) {
	sql, err := gateways.NewSQL(config.SQL)
	if err != nil {
		fmt.Println("ERROR: could not connect to mysql.")
		fmt.Println(err.Error())
		return nil, err
	}

	stats, err := statsd.New(
		statsd.Address(config.Statsd.Host+":"+config.Statsd.Port),
		statsd.Prefix(config.Statsd.Prefix),
	)
	if err != nil {
		fmt.Println(err.Error())
	}

	sendgrid := gateways.NewSendgrid(config.Sendgrid)
	towncenter := gateways.NewTownCenter(config.TownCenter)

	rabbit, err := gateways.NewRabbit(config.Rabbit)
	if err != nil {
		fmt.Println("ERROR: coud not connect to RabbitMQ")
		fmt.Println(err.Error())
	}

	ctx := &handlers.GatewayContext{
		Sql:        sql,
		Sendgrid:   sendgrid,
		TownCenter: towncenter,
		Rabbit:     rabbit,
		Stats:      stats,
	}

	b := &Bloodlines{
		content:    handlers.NewContent(ctx),
		receipt:    handlers.NewReceipt(ctx),
		job:        handlers.NewJob(ctx),
		trigger:    handlers.NewTrigger(ctx),
		preference: handlers.NewPreference(ctx),
		workers:    []workers.Send{workers.NewSend(ctx)},
	}

	InitRouter(b)
	return b, nil
}

/*InitRouter connects the handlers to endpoints with gin*/
func InitRouter(b *Bloodlines) {
	b.router = gin.Default()
	b.router.Use(handlers.GetCors())

	content := b.router.Group("/api/content")
	{
		content.POST("", b.content.New)
		content.GET("", b.content.ViewAll)
		content.GET("/:contentId", b.content.View)
		content.PUT("/:contentId", b.content.Update)
		content.DELETE("/:contentId", b.content.Deactivate)
	}
	receipt := b.router.Group("/api/receipt")
	{
		receipt.GET("", b.receipt.ViewAll)
		receipt.POST("/send", b.receipt.Send)
		receipt.GET("/:receiptId", b.receipt.View)
	}
	job := b.router.Group("/api/job")
	{
		job.GET("", b.job.ViewAll)
		job.POST("", b.job.New)
		job.GET("/:jobId", b.job.View)
		job.PUT("/:jobId", b.job.Update)
		job.DELETE("/:jobId", b.job.Stop)
	}
	trigger := b.router.Group("/api/trigger")
	{
		trigger.POST("", b.trigger.New)
		trigger.GET("", b.trigger.ViewAll)
		trigger.GET("/:key", b.trigger.View)
		trigger.PUT("/:key", b.trigger.Update)
		trigger.DELETE("/:key", b.trigger.Remove)
		trigger.POST("/:key/activate", b.trigger.Activate)
	}
	pref := b.router.Group("/api/preference")
	{
		pref.POST("", b.preference.New)
		pref.GET("/:userId", b.preference.View)
		pref.PATCH("/:userId", b.preference.Update)
		pref.DELETE("/:userId", b.preference.Deactivate)
	}

	for _, w := range b.workers {
		w.Consume()
	}
}

/*Start begins the Bloodlines server*/
func (b *Bloodlines) Start(port string, tls config.TLS) {
	if tls.Enabled {
		b.router.RunTLS(port, tls.Cert, tls.Key)
	} else {
		b.router.Run(port)
	}
}<commit_msg>Remove TLS<commit_after>package router

import (
	"fmt"

	"gopkg.in/alexcesaro/statsd.v2"
	"gopkg.in/gin-gonic/gin.v1"

	"github.com/ghmeier/bloodlines/config"
	"github.com/ghmeier/bloodlines/gateways"
	"github.com/ghmeier/bloodlines/handlers"
	"github.com/ghmeier/bloodlines/workers"
)

/*Bloodlines is the main server object which routes requests*/
type Bloodlines struct {
	router     *gin.Engine
	content    handlers.ContentI
	receipt    handlers.ReceiptI
	job        handlers.JobI
	trigger    handlers.TriggerI
	preference handlers.PreferenceI
	workers    []workers.Send
}

/*New returns a ready-to-run Bloodlines struct from the given config*/
func New(config *config.Root) (*Bloodlines, error) {
	sql, err := gateways.NewSQL(config.SQL)
	if err != nil {
		fmt.Println("ERROR: could not connect to mysql.")
		fmt.Println(err.Error())
		return nil, err
	}

	stats, err := statsd.New(
		statsd.Address(config.Statsd.Host+":"+config.Statsd.Port),
		statsd.Prefix(config.Statsd.Prefix),
	)
	if err != nil {
		fmt.Println(err.Error())
	}

	sendgrid := gateways.NewSendgrid(config.Sendgrid)
	towncenter := gateways.NewTownCenter(config.TownCenter)

	rabbit, err := gateways.NewRabbit(config.Rabbit)
	if err != nil {
		fmt.Println("ERROR: coud not connect to RabbitMQ")
		fmt.Println(err.Error())
	}

	ctx := &handlers.GatewayContext{
		Sql:        sql,
		Sendgrid:   sendgrid,
		TownCenter: towncenter,
		Rabbit:     rabbit,
		Stats:      stats,
	}

	b := &Bloodlines{
		content:    handlers.NewContent(ctx),
		receipt:    handlers.NewReceipt(ctx),
		job:        handlers.NewJob(ctx),
		trigger:    handlers.NewTrigger(ctx),
		preference: handlers.NewPreference(ctx),
		workers:    []workers.Send{workers.NewSend(ctx)},
	}

	InitRouter(b)
	return b, nil
}

/*InitRouter connects the handlers to endpoints with gin*/
func InitRouter(b *Bloodlines) {
	b.router = gin.Default()
	b.router.Use(handlers.GetCors())

	content := b.router.Group("/api/content")
	{
		content.POST("", b.content.New)
		content.GET("", b.content.ViewAll)
		content.GET("/:contentId", b.content.View)
		content.PUT("/:contentId", b.content.Update)
		content.DELETE("/:contentId", b.content.Deactivate)
	}
	receipt := b.router.Group("/api/receipt")
	{
		receipt.GET("", b.receipt.ViewAll)
		receipt.POST("/send", b.receipt.Send)
		receipt.GET("/:receiptId", b.receipt.View)
	}
	job := b.router.Group("/api/job")
	{
		job.GET("", b.job.ViewAll)
		job.POST("", b.job.New)
		job.GET("/:jobId", b.job.View)
		job.PUT("/:jobId", b.job.Update)
		job.DELETE("/:jobId", b.job.Stop)
	}
	trigger := b.router.Group("/api/trigger")
	{
		trigger.POST("", b.trigger.New)
		trigger.GET("", b.trigger.ViewAll)
		trigger.GET("/:key", b.trigger.View)
		trigger.PUT("/:key", b.trigger.Update)
		trigger.DELETE("/:key", b.trigger.Remove)
		trigger.POST("/:key/activate", b.trigger.Activate)
	}
	pref := b.router.Group("/api/preference")
	{
		pref.POST("", b.preference.New)
		pref.GET("/:userId", b.preference.View)
		pref.PATCH("/:userId", b.preference.Update)
		pref.DELETE("/:userId", b.preference.Deactivate)
	}

	for _, w := range b.workers {
		w.Consume()
	}
}

/*Start begins the Bloodlines server*/
func (b *Bloodlines) Start(port string, tls config.TLS) {
	b.router.Run(port)
}
{'fraction_non_alphanumeric': 0.17022900763358778, 'fraction_numerical': 0.0006106870229007634, 'mean_word_length': 3.84900074019245, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799368', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2449, 'n_words': 429}