package main

import (
	"html/template"
	"log"
	"net/http"
	"time"

	"donovankelly.xyz/templates"

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

func main() {
	logger := log.Default()
	router := gin.Default()
	router.Use(HeaderMiddleware())
	templ := template.New("")
	templ = template.Must(templ.ParseFS(templates.WebappTemplateFS, "webapp/*.html"))
	router.SetHTMLTemplate(templ)
	router.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", struct {
			Year string
		}{
			Year: time.Now().Format("2006"),
		})
	})
	router.Static("/assets/", "./assets")
	if err := router.Run(":8080"); err != nil {
		logger.Printf("Server error: %v", err)
	}
}

func HeaderMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
		c.Header("Pragma", "no-cache")
		c.Header("Expires", "0")
		c.Header("X-Content-Type-Options", "nosniff")
		c.Header("X-Frame-Options", "DENY")
		c.Header("X-XSS-Protection", "1; mode=block")
		c.Next()
	}
}
