<commit_before>package main

import (
	"net/http"

	"github.com/byuoitav/event-router-microservice/eventinfrastructure"
	"github.com/byuoitav/touchpanel-ui-microservice/events"
	"github.com/byuoitav/touchpanel-ui-microservice/handlers"
	"github.com/jessemillar/health"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
)

func main() {
	pub := eventinfrastructure.NewPublisher("7003")

	filters := []string{eventinfrastructure.UI}
	sub := eventinfrastructure.NewSubscriber(filters)

	//	ip := eventinfrastructure.GetIP()

	var req eventinfrastructure.ConnectionRequest
	req.PublisherAddr = "localhost:7003"
	req.SubscriberEndpoint = "http://localhost:8888/subscribe"

	// post to the router with the subscription request
	go eventinfrastructure.SendConnectionRequest("http://localhost:6999/subscribe", req, true)

	go events.WriteMessagesToSocket(sub)
	go events.Refresh(sub)

	port := ":8888"
	router := echo.New()
	router.Pre(middleware.RemoveTrailingSlash())
	router.Use(middleware.CORS())

	router.GET("/health", echo.WrapHandler(http.HandlerFunc(health.Check)))

	// event endpoints
	router.POST("/subscribe", Subscribe, BindSubscriber(sub))
	router.POST("/publish", handlers.PublishEvent, BindPublisher(pub))
	router.POST("/publishfeature", handlers.PublishFeature, BindPublisher(pub))

	router.GET("/websocket", handlers.OpenWebSocket)
	router.GET("/hostname", handlers.GetHostname)
	router.GET("/deviceinfo", handlers.GetDeviceInfo)
	router.GET("/reboot", handlers.Reboot)
	router.GET("/dockerstatus", handlers.GetDockerStatus)
	router.GET("/json", handlers.GetJSON)

	router.PUT("/screenoff", handlers.SendScreenOff, BindSubscriber(sub))

	router.POST("/help", handlers.Help)
	router.POST("/confirmhelp", handlers.ConfirmHelp)
	router.POST("/cancelhelp", handlers.CancelHelp)

	router.Static("/", "redirect.html")
	router.Static("/circle-default", "circle-default")

	router.Start(port)
}

func Subscribe(context echo.Context) error {
	return eventinfrastructure.HandleSubscriptionRequest(context)
}

func BindPublisher(p *eventinfrastructure.Publisher) echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Set(eventinfrastructure.ContextPublisher, p)
			return next(c)
		}
	}
}

func BindSubscriber(s *eventinfrastructure.Subscriber) echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Set(eventinfrastructure.ContextSubscriber, s)
			return next(c)
		}
	}
}<commit_msg>access control origin<commit_after>package main

import (
	"net/http"

	"github.com/byuoitav/event-router-microservice/eventinfrastructure"
	"github.com/byuoitav/touchpanel-ui-microservice/events"
	"github.com/byuoitav/touchpanel-ui-microservice/handlers"
	"github.com/jessemillar/health"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
)

func main() {
	pub := eventinfrastructure.NewPublisher("7003")

	filters := []string{eventinfrastructure.UI}
	sub := eventinfrastructure.NewSubscriber(filters)

	//	ip := eventinfrastructure.GetIP()

	var req eventinfrastructure.ConnectionRequest
	req.PublisherAddr = "localhost:7003"
	req.SubscriberEndpoint = "http://localhost:8888/subscribe"

	// post to the router with the subscription request
	go eventinfrastructure.SendConnectionRequest("http://localhost:6999/subscribe", req, true)

	go events.WriteMessagesToSocket(sub)
	go events.Refresh(sub)

	port := ":8888"
	router := echo.New()
	router.Pre(middleware.RemoveTrailingSlash())
	router.Use(middleware.CORS())
	router.Use(CORS())

	router.GET("/health", echo.WrapHandler(http.HandlerFunc(health.Check)))

	// event endpoints
	router.POST("/subscribe", Subscribe, BindSubscriber(sub))
	router.POST("/publish", handlers.PublishEvent, BindPublisher(pub))
	router.POST("/publishfeature", handlers.PublishFeature, BindPublisher(pub))

	router.GET("/websocket", handlers.OpenWebSocket)
	router.GET("/hostname", handlers.GetHostname)
	router.GET("/deviceinfo", handlers.GetDeviceInfo)
	router.GET("/reboot", handlers.Reboot)
	router.GET("/dockerstatus", handlers.GetDockerStatus)
	router.GET("/json", handlers.GetJSON)

	router.PUT("/screenoff", handlers.SendScreenOff, BindSubscriber(sub))

	router.POST("/help", handlers.Help)
	router.POST("/confirmhelp", handlers.ConfirmHelp)
	router.POST("/cancelhelp", handlers.CancelHelp)

	router.Static("/", "redirect.html")
	router.Static("/circle-default", "circle-default")

	router.Start(port)
}

func Subscribe(context echo.Context) error {
	return eventinfrastructure.HandleSubscriptionRequest(context)
}

func BindPublisher(p *eventinfrastructure.Publisher) echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Set(eventinfrastructure.ContextPublisher, p)
			return next(c)
		}
	}
}

func BindSubscriber(s *eventinfrastructure.Subscriber) echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Set(eventinfrastructure.ContextSubscriber, s)
			return next(c)
		}
	}
}

func CORS() echo.MiddlewareFunc {
	return func(h echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			c.Response().Header().Set("Access-Control-Allow-Origin", "*")
			return h(c)
		}
	}
}
{'fraction_non_alphanumeric': 0.1388993592159819, 'fraction_numerical': 0.007538635506973238, 'mean_word_length': 5.874352331606218, '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': '1001227', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1673, 'n_words': 269}