From 0ab9bca213a9b7090253e4ba39dddc82e87bdfca Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Sun, 29 May 2022 22:39:21 -0700 Subject: [PATCH] wip --- cmd/serve.go | 24 ++++++++ server/server.go | 106 ++++++++++++++++++++++++++++++++++++ server/templates/index.html | 16 ++++++ 3 files changed, 146 insertions(+) create mode 100644 cmd/serve.go create mode 100644 server/server.go create mode 100644 server/templates/index.html diff --git a/cmd/serve.go b/cmd/serve.go new file mode 100644 index 0000000..c52a13b --- /dev/null +++ b/cmd/serve.go @@ -0,0 +1,24 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "github.com/spf13/cobra" + "git.nagee.dev/isthisnagee/diary/server" +) + +// 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`, + Run: func(cmd *cobra.Command, args []string) { + server.Run() + }, +} + +func init() { + rootCmd.AddCommand(serveCmd) +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..0cf54d4 --- /dev/null +++ b/server/server.go @@ -0,0 +1,106 @@ +package server + + +import ( + "runtime" + "os/exec" + "html/template" + "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 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 +} + +func openBrowser(url string) { + var err error + + log.Print(url) + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + log.Fatal(err) + } + +} + +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) + } +} + + +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 +} + +func Run() { + setup() + + http.HandleFunc("/", Index) + + port, err := getFreePort() + if err != nil { + panic(err) + } + + 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) + } + +} diff --git a/server/templates/index.html b/server/templates/index.html new file mode 100644 index 0000000..04f430b --- /dev/null +++ b/server/templates/index.html @@ -0,0 +1,16 @@ + + + + + + HTML 5 Boilerplate + + +

diary

+ + +