updated env variables to be from OS

This commit is contained in:
Ross Trottier 2024-06-27 11:53:42 -06:00
parent 147b79eceb
commit fbfee0edaa
2 changed files with 16 additions and 11 deletions

View File

@ -5,12 +5,10 @@ import (
) )
func (app *application) home(writer http.ResponseWriter, request *http.Request) { func (app *application) home(writer http.ResponseWriter, request *http.Request) {
baseURI := request.Host
data := templateData{ data := templateData{
AppLinks: getAppLinks(app.tier, baseURI), AppLinks: getAppLinks(app.tier, app.host),
Tier: app.tier, Tier: app.tier,
BaseUri: baseURI, BaseUri: app.host,
} }
app.render(writer, request, http.StatusOK, "home.tmpl.html", data) app.render(writer, request, http.StatusOK, "home.tmpl.html", data)

View File

@ -5,24 +5,30 @@ import (
"log/slog" "log/slog"
"net/http" "net/http"
"os" "os"
"flag"
) )
type application struct { type application struct {
logger *slog.Logger logger *slog.Logger
templateCache map[string]*template.Template templateCache map[string]*template.Template
tier string tier string
host string
} }
func main() { func main() {
//Get env variables
addr := flag.String("addr", ":8080", "HTTP network address")
tier := flag.String("tier", "starter", "tier the core is on")
flag.Parse()
//Set up logger //Set up logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true, AddSource: true,
})) }))
//Get env variables
tier := os.Getenv("TIER")
if len(tier) == 0 {
tier = "starter"
}
host := os.Getenv("HOST")
if len(host) == 0 {
logger.Error("Must specify host name.")
os.Exit(1)
}
//Set up template cache //Set up template cache
templateCache, err := newTemplateCache() templateCache, err := newTemplateCache()
if err != nil { if err != nil {
@ -33,10 +39,11 @@ func main() {
app := application { app := application {
logger: logger, logger: logger,
templateCache: templateCache, templateCache: templateCache,
tier: *tier, tier: tier,
host: host,
} }
err = http.ListenAndServe(*addr, app.routes()) err = http.ListenAndServe(":8080", app.routes())
logger.Error(err.Error()) logger.Error(err.Error())
os.Exit(1) os.Exit(1)
} }