diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 78f5fb9..6d113a1 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -5,12 +5,10 @@ import ( ) func (app *application) home(writer http.ResponseWriter, request *http.Request) { - baseURI := request.Host - data := templateData{ - AppLinks: getAppLinks(app.tier, baseURI), + AppLinks: getAppLinks(app.tier, app.host), Tier: app.tier, - BaseUri: baseURI, + BaseUri: app.host, } app.render(writer, request, http.StatusOK, "home.tmpl.html", data) diff --git a/cmd/web/main.go b/cmd/web/main.go index c440fd8..5ed21f4 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,24 +5,30 @@ import ( "log/slog" "net/http" "os" - "flag" ) type application struct { logger *slog.Logger templateCache map[string]*template.Template tier string + host string } 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 logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ 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 templateCache, err := newTemplateCache() if err != nil { @@ -33,10 +39,11 @@ func main() { app := application { logger: logger, templateCache: templateCache, - tier: *tier, + tier: tier, + host: host, } - err = http.ListenAndServe(*addr, app.routes()) + err = http.ListenAndServe(":8080", app.routes()) logger.Error(err.Error()) os.Exit(1) } \ No newline at end of file