cavepedia/src/main.go

78 lines
1.7 KiB
Go

package main
import (
"crypto/rand"
"embed"
"log"
"net/http"
"runtime"
"git.seaturtle.pw/pew/cavepedia/utils"
"github.com/blevesearch/bleve/v2"
"github.com/zserge/lorca"
)
var JSON string
var index bleve.Index
//go:embed templates
var templatesHTML embed.FS
//go:embed static
var staticFiles embed.FS
func main() {
log.SetFlags(log.Lshortfile)
FILES := "./cavepedia-data/00_files/"
JSON = "./cavepedia-data/02_json"
INDEX := "cavepedia.bleve"
/* Setup Search */
exists, ok := openIndex(INDEX)
if !ok {
return
}
/* Web Server */
key = make([]byte, 512)
_, err := rand.Read(key)
if err != nil {
log.Fatal(err)
}
http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(FILES))))
http.Handle("/static/", http.FileServer(http.FS(staticFiles)))
http.Handle("/favicon.ico", http.RedirectHandler("/static/favicon.ico", http.StatusMovedPermanently))
http.HandleFunc("/", GetHome) // Redirects to /search if auth'd
http.HandleFunc("/search", GetSearch) // Redirects to / if not auth'd
http.HandleFunc("/quiz", PostQuiz)
http.HandleFunc("/pdf", GetPDF)
/* Index data if needed */
if !exists && !indexDocuments() {
return
}
if utils.GetConfig().Proxy {
log.Println("Server is running!")
log.Fatal(http.ListenAndServe(":3000", nil))
} else {
// Start in separate goroutine
go func() {
log.Println("Server is running!")
log.Fatal(http.ListenAndServe(":3000", nil))
}()
/* GUI */
args := []string{}
if runtime.GOOS == "linux" {
args = append(args, "--class=Lorca")
}
ui, err := lorca.New("http://localhost:3000", "", 1000, 800, args...)
if err != nil {
log.Fatal(err)
}
defer ui.Close()
<-ui.Done()
}
}