package main

import (
	"os"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

type App struct {
	Engine *gin.Engine
	Stream *SseStream
}

func InitHttp() *App {

	app := &App{
		Engine: nil,
		Stream: nil,
	}

	app.Stream = InitSse()

	app.Engine = gin.Default()
	app.Engine.Use(cors.Default())

	app.RegisterWebRoutes()
	app.RegisterApiRoutes()

	return app
}

func (app *App) Run() {

	port := os.Getenv("PORT")

	if port == "" {
		port = "8080"
	}

	app.Engine.Run(":" + port)
}

func (app *App) RegisterWebRoutes() {

	display_h := app.Engine.Group("/display-h")
	display_h.Static("/", "./static/display-h")

	display := app.Engine.Group("/display")
	display.Static("/", "./static/display")

	mobile := app.Engine.Group("/mobile")
	mobile.Static("/", "./static/mobile")

	priority := app.Engine.Group("/priority")
	priority.Static("/", "./static/priority")

	sapp := app.Engine.Group("/_app")
	sapp.Static("/", "./static/_app")

	static := app.Engine.Group("/static")
	static.Static("/", "./static/static")

	app.Engine.StaticFile("/", "./static/index.html")
	app.Engine.StaticFile("/favicon.png", "./static/favicon.png")

	app.Engine.ForwardedByClientIP = true
	app.Engine.SetTrustedProxies([]string{"127.0.0.1"})
}
