|
|
|
@ -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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|