|
|
|
@ -1,27 +1,52 @@
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"runtime"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"html/template"
|
|
|
|
|
"git.nagee.dev/isthisnagee/diary/model"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"log"
|
|
|
|
|
"embed"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path"
|
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
|
|
"git.nagee.dev/isthisnagee/diary/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//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 {
|
|
|
|
@ -52,27 +77,20 @@ 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)
|
|
|
|
|
log.Print(templates)
|
|
|
|
|
var tmpl, ok = templates["index.html"]
|
|
|
|
|
if !ok {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func setup() {
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
@ -83,22 +101,35 @@ func setup() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
err = LoadTemplates()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
App = app
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Run() {
|
|
|
|
|
func Run(openBrowserAtUrl bool, defaultPort int) {
|
|
|
|
|
setup()
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", Index)
|
|
|
|
|
|
|
|
|
|
port, err := getFreePort()
|
|
|
|
|
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")
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|