From e91153fa8f8dd8ae92ce7b89231d2d654c437068 Mon Sep 17 00:00:00 2001 From: nagee Date: Sun, 26 Jun 2022 19:47:22 +0000 Subject: [PATCH] Basic server --- cmd/serve.go | 6 +- server/server.go | 149 ++++++++++++++++++++++++++++------------------- 2 files changed, 93 insertions(+), 62 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index c52a13b..14f7fa2 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -5,17 +5,17 @@ Copyright © 2022 NAME HERE package cmd import ( - "github.com/spf13/cobra" "git.nagee.dev/isthisnagee/diary/server" + "github.com/spf13/cobra" ) // serveCmd represents the serve command var serveCmd = &cobra.Command{ Use: "serve", Short: "Run a web server at the specified port", - Long: `Run a web server at the specified port. Allows you to view, edit, and delete notes from a UI`, + Long: `Run a web server at the specified port. Allows you to view, edit, and delete notes from a UI`, Run: func(cmd *cobra.Command, args []string) { - server.Run() + server.Run(false, 37931) }, } diff --git a/server/server.go b/server/server.go index 0cf54d4..cd89924 100644 --- a/server/server.go +++ b/server/server.go @@ -1,40 +1,65 @@ package server - import ( - "runtime" - "os/exec" - "html/template" + "embed" + "fmt" + "html/template" + "io/fs" + "log" + "net" + "net/http" + "os" + "os/exec" + "path" + "runtime" + "git.nagee.dev/isthisnagee/diary/model" - "log" - "embed" - "net" - "net/http" - "fmt" - "path" - "os" ) - //go:embed templates/* var files embed.FS + var templates map[string]*template.Template var App *model.App +func LoadTemplates() error { + if templates == nil { + templates = make(map[string]*template.Template) + } + tmplFiles, err := fs.ReadDir(files, "templates") + if err != nil { + return err + } + + for _, tmpl := range tmplFiles { + if tmpl.IsDir() { + continue + } + + pt, err := template.ParseFS(files, path.Join("templates", tmpl.Name())) + if err != nil { + return err + } + + templates[tmpl.Name()] = pt + } + return nil +} + func getFreePort() (int, error) { - listener, err := net.Listen("tcp", ":0") - if err != nil { - return 0, err - } - defer listener.Close() - return listener.Addr().(*net.TCPAddr).Port, nil + listener, err := net.Listen("tcp", ":0") + if err != nil { + return 0, err + } + defer listener.Close() + return listener.Addr().(*net.TCPAddr).Port, nil } func openBrowser(url string) { var err error - log.Print(url) + log.Print(url) switch runtime.GOOS { case "linux": @@ -52,55 +77,61 @@ func openBrowser(url string) { } -func LoadTemplates() error { - template.PareseFS(content) - -} - func Index(w http.ResponseWriter, r *http.Request) { - tmpl, err = template.ParseFS(files, "index.html") - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - - log.Print(tmpl) - data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{}) - if err := tmpl.Execute(w, data); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } -} + log.Print(templates) + var tmpl, ok = templates["index.html"] + if !ok { + http.Error(w, "could not find template index", http.StatusInternalServerError) + return + } + data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{}) + if err := tmpl.Execute(w, data); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} func setup() { home, err := os.UserHomeDir() - if err != nil { - panic(err) - } - var db_path = path.Join(home, ".diary.sql") - app, err := model.NewApp(db_path) - if err != nil { - panic(err) - } - App = app + if err != nil { + panic(err) + } + var db_path = path.Join(home, ".diary.sql") + app, err := model.NewApp(db_path) + if err != nil { + panic(err) + } + err = LoadTemplates() + if err != nil { + panic(err) + } + App = app } -func Run() { - setup() +func Run(openBrowserAtUrl bool, defaultPort int) { + setup() - http.HandleFunc("/", Index) + http.HandleFunc("/", Index) - port, err := getFreePort() - if err != nil { - panic(err) - } + var port int + if defaultPort <= 0 { + freePort, err := getFreePort() + if err != nil { + panic(err) + } + port = freePort + } else { + port = defaultPort + } - log.Print("opening browser") - openBrowser(fmt.Sprintf("http://localhost:%d", port)) - log.Print("done") - if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { - panic(err) - } + log.Print("opening browser") + var url = fmt.Sprintf("http://localhost:%d", port) + log.Print(url) + if openBrowserAtUrl { + openBrowser(url) + } + if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { + panic(err) + } }