readysite / readysite.org / main.go
1.8 KB
main.go
package main

import (
	"embed"
	"net/http"
	"os"
	"strings"

	"github.com/readysite/readysite/pkg/application"
	"github.com/readysite/readysite/pkg/application/emailers"
	"github.com/readysite/readysite/pkg/frontend"
	"github.com/readysite/readysite/pkg/frontend/esbuild"
	"github.com/readysite/readysite/readysite.org/controllers"
	"github.com/readysite/readysite/readysite.org/internal/websites"
)

//go:embed views
var views embed.FS

//go:embed all:emails
var emails embed.FS

func main() {
	// Setting up a health check for deployments
	http.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("ok"))
	})

	// Select emailer based on environment
	var emailer application.Emailer
	if key := os.Getenv("RESEND_API_KEY"); key != "" {
		emailer = emailers.NewResend(emails, key, "ReadySite <noreply@theskyscape.com>", nil)
	} else {
		emailer = emailers.NewLogger(emails, nil)
	}

	// Start background sleep/wake scheduler for free tier sites
	websites.StartSleepScheduler()

	// Then handing over control to the application
	application.Serve(views,
		application.WithValue("theme", "dark"),
		application.WithEmailer(emailer),
		application.WithFunc("upper", strings.ToUpper),
		application.WithFunc("divf", func(a, b float64) float64 { return a / b }),
		frontend.WithBundler(&esbuild.Config{
			Entry:   "frontend/index.js",
			Include: []string{"frontend"},
		}),
		application.WithMiddleware(controllers.WakeMiddleware),
		application.WithController(controllers.Docker()),
		application.WithController(controllers.Git()),
		application.WithController(controllers.Home()),
		application.WithController(controllers.Auth()),
		application.WithController(controllers.Billing()),
		application.WithController(controllers.Settings()),
		application.WithController(controllers.Sites()),
	)
}
← Back