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) {
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)

View File

@ -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)
}