Basic server

server
nagee 2 years ago
parent 0ab9bca213
commit e91153fa8f

@ -5,17 +5,17 @@ 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
var serveCmd = &cobra.Command{ var serveCmd = &cobra.Command{
Use: "serve", Use: "serve",
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,40 +1,65 @@
package server package server
import ( import (
"runtime" "embed"
"os/exec" "fmt"
"html/template" "html/template"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/exec"
"path"
"runtime"
"git.nagee.dev/isthisnagee/diary/model" "git.nagee.dev/isthisnagee/diary/model"
"log"
"embed"
"net"
"net/http"
"fmt"
"path"
"os"
) )
//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 {
return 0, err return 0, err
} }
defer listener.Close() defer listener.Close()
return listener.Addr().(*net.TCPAddr).Port, nil return listener.Addr().(*net.TCPAddr).Port, nil
} }
func openBrowser(url string) { func openBrowser(url string) {
var err error var err error
log.Print(url) log.Print(url)
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
@ -52,55 +77,61 @@ 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 {
return http.Error(w, "could not find template index", 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)
}
}
data := App.GetDiaryEntries(model.GetDiaryEntriesQuery{})
if err := tmpl.Execute(w, data); err != nil {
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 {
panic(err) panic(err)
} }
var db_path = path.Join(home, ".diary.sql") var db_path = path.Join(home, ".diary.sql")
app, err := model.NewApp(db_path) app, err := model.NewApp(db_path)
if err != nil { if err != nil {
panic(err) panic(err)
} }
App = app err = LoadTemplates()
if err != nil {
panic(err)
}
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 err != nil { if defaultPort <= 0 {
panic(err) freePort, err := getFreePort()
} if err != nil {
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 err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { if openBrowserAtUrl {
panic(err) openBrowser(url)
} }
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(err)
}
} }

Loading…
Cancel
Save