From 4c6342bc7cd4c5c1081d6404298a3dfffb631f34 Mon Sep 17 00:00:00 2001 From: nagee Date: Sun, 3 Jul 2022 23:08:57 +0000 Subject: [PATCH] initial server --- server/server.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/server/server.go b/server/server.go index cd89924..57ecab7 100644 --- a/server/server.go +++ b/server/server.go @@ -21,7 +21,7 @@ var files embed.FS var templates map[string]*template.Template -var App *model.App +type App struct{ *model.App } func LoadTemplates() error { if templates == nil { @@ -77,21 +77,20 @@ func openBrowser(url string) { } -func Index(w http.ResponseWriter, r *http.Request) { - log.Print(templates) +func (app *App) ListDiaryEntries(w http.ResponseWriter, r *http.Request) { var tmpl, ok = templates["index.html"] if !ok { http.Error(w, "could not find template index", http.StatusInternalServerError) return } - data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{}) + data := app.GetDiaryEntries(model.GetDiaryEntriesQuery{}) if err := tmpl.Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } -func setup() { +func setup() *App { home, err := os.UserHomeDir() if err != nil { panic(err) @@ -105,13 +104,13 @@ func setup() { if err != nil { panic(err) } - App = app + return &App{app} } func Run(openBrowserAtUrl bool, defaultPort int) { - setup() + app := setup() - http.HandleFunc("/", Index) + http.HandleFunc("/", app.ListDiaryEntries) var port int if defaultPort <= 0 { @@ -124,10 +123,10 @@ func Run(openBrowserAtUrl bool, defaultPort int) { port = defaultPort } - log.Print("opening browser") var url = fmt.Sprintf("http://localhost:%d", port) log.Print(url) if openBrowserAtUrl { + log.Print("opening browser") openBrowser(url) } if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {