federated-dash/cmd/web/main.go

53 lines
1001 B
Go
Raw Permalink Normal View History

2024-06-25 17:35:30 +00:00
package main
import (
"html/template"
"log/slog"
"net/http"
"os"
)
type application struct {
logger *slog.Logger
templateCache map[string]*template.Template
2024-06-25 20:17:08 +00:00
tier string
2024-07-09 16:11:48 +00:00
domain string
2024-09-24 20:16:08 +00:00
appLinks []AppLink
2024-06-25 17:35:30 +00:00
}
func main() {
//Set up logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
}))
2024-06-27 17:53:42 +00:00
//Get env variables
tier := os.Getenv("TIER")
if len(tier) == 0 {
2024-09-24 20:16:08 +00:00
tier = "good"
2024-06-27 17:53:42 +00:00
}
2024-07-09 16:11:48 +00:00
domain := os.Getenv("DOMAIN")
if len(domain) == 0 {
logger.Error("Must specify domain name.")
2024-06-27 17:53:42 +00:00
os.Exit(1)
}
2024-06-25 17:35:30 +00:00
//Set up template cache
templateCache, err := newTemplateCache()
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
2024-09-24 20:16:08 +00:00
//Set up app links cache
appLinks := getAppLinks(tier, domain)
2024-06-25 17:35:30 +00:00
//Set up application data
app := application {
logger: logger,
templateCache: templateCache,
2024-06-27 17:53:42 +00:00
tier: tier,
2024-07-09 16:11:48 +00:00
domain: domain,
2024-09-24 20:16:08 +00:00
appLinks: appLinks,
2024-06-25 17:35:30 +00:00
}
2024-09-24 20:16:08 +00:00
//start server
2024-06-27 17:53:42 +00:00
err = http.ListenAndServe(":8080", app.routes())
2024-06-25 17:35:30 +00:00
logger.Error(err.Error())
os.Exit(1)
}