Basic server

server
nagee 2 years ago
parent 0ab9bca213
commit e91153fa8f

@ -5,8 +5,8 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd package cmd
import ( import (
"github.com/spf13/cobra"
"git.nagee.dev/isthisnagee/diary/server" "git.nagee.dev/isthisnagee/diary/server"
"github.com/spf13/cobra"
) )
// serveCmd represents the serve command // serveCmd represents the serve command
@ -15,7 +15,7 @@ var serveCmd = &cobra.Command{
Short: "Run a web server at the specified port", 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) { Run: func(cmd *cobra.Command, args []string) {
server.Run() server.Run(false, 37931)
}, },
} }

@ -1,27 +1,52 @@
package server package server
import ( import (
"runtime" "embed"
"os/exec" "fmt"
"html/template" "html/template"
"git.nagee.dev/isthisnagee/diary/model" "io/fs"
"log" "log"
"embed"
"net" "net"
"net/http" "net/http"
"fmt"
"path"
"os" "os"
) "os/exec"
"path"
"runtime"
"git.nagee.dev/isthisnagee/diary/model"
)
//go:embed templates/* //go:embed templates/*
var files embed.FS var files embed.FS
var templates map[string]*template.Template var templates map[string]*template.Template
var App *model.App 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) { func getFreePort() (int, error) {
listener, err := net.Listen("tcp", ":0") listener, err := net.Listen("tcp", ":0")
if err != nil { if err != nil {
@ -52,27 +77,20 @@ func openBrowser(url string) {
} }
func LoadTemplates() error {
template.PareseFS(content)
}
func Index(w http.ResponseWriter, r *http.Request) { func Index(w http.ResponseWriter, r *http.Request) {
tmpl, err = template.ParseFS(files, "index.html") log.Print(templates)
if err != nil { var tmpl, ok = templates["index.html"]
http.Error(w, err.Error(), http.StatusInternalServerError) if !ok {
http.Error(w, "could not find template index", http.StatusInternalServerError)
return return
} }
log.Print(tmpl)
data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{}) data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{})
if err := tmpl.Execute(w, data); err != nil { if err := tmpl.Execute(w, data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
} }
func setup() { func setup() {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
@ -83,22 +101,35 @@ func setup() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = LoadTemplates()
if err != nil {
panic(err)
}
App = app App = app
} }
func Run() { func Run(openBrowserAtUrl bool, defaultPort int) {
setup() setup()
http.HandleFunc("/", Index) http.HandleFunc("/", Index)
port, err := getFreePort() var port int
if defaultPort <= 0 {
freePort, err := getFreePort()
if err != nil { if err != nil {
panic(err) panic(err)
} }
port = freePort
} else {
port = defaultPort
}
log.Print("opening browser") log.Print("opening browser")
openBrowser(fmt.Sprintf("http://localhost:%d", port)) var url = fmt.Sprintf("http://localhost:%d", port)
log.Print("done") log.Print(url)
if openBrowserAtUrl {
openBrowser(url)
}
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(err) panic(err)
} }

Loading…
Cancel
Save