2022-04-09 12:01:04 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-04-15 17:46:51 +03:00
|
|
|
"embed"
|
2022-05-07 18:08:42 +03:00
|
|
|
"fmt"
|
2022-05-01 13:29:39 +03:00
|
|
|
"log"
|
2022-04-09 12:01:04 +03:00
|
|
|
"net/http"
|
2022-04-11 00:27:37 +03:00
|
|
|
_ "net/http/pprof"
|
2022-05-09 18:20:31 +03:00
|
|
|
"os"
|
2022-04-11 00:27:37 +03:00
|
|
|
"runtime"
|
2022-04-09 12:01:04 +03:00
|
|
|
|
2022-04-13 08:28:07 +03:00
|
|
|
"github.com/pkg/profile"
|
2022-04-14 18:39:18 +03:00
|
|
|
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
|
2022-04-26 10:24:16 +03:00
|
|
|
"github.com/robertjanetzko/LegendsBrowser2/backend/server"
|
2022-04-26 18:39:24 +03:00
|
|
|
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
|
2022-05-09 18:20:31 +03:00
|
|
|
"github.com/spf13/cobra"
|
2022-04-09 12:01:04 +03:00
|
|
|
)
|
|
|
|
|
2022-04-19 12:46:43 +03:00
|
|
|
//go:embed static
|
|
|
|
var static embed.FS
|
2022-04-15 17:46:51 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
var (
|
|
|
|
f, c, subUri string
|
|
|
|
l, p, d, s *bool
|
|
|
|
port *int
|
|
|
|
)
|
2022-05-03 22:39:00 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "legendsbrowser",
|
|
|
|
Short: "A Legends Browser for Dwarf Fortress",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if *p {
|
|
|
|
defer profile.Start(profile.ProfilePath(".")).Stop()
|
|
|
|
go func() {
|
|
|
|
http.ListenAndServe(":8081", nil)
|
|
|
|
}()
|
|
|
|
}
|
2022-05-05 13:55:33 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
config, err := server.LoadConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-26 18:39:24 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
templates.DebugTemplates = config.DebugTemplates
|
2022-05-01 13:29:39 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
server.DebugJSON = *d
|
|
|
|
config.Port = *port
|
|
|
|
config.ServerMode = *s
|
|
|
|
config.SubUri = subUri
|
|
|
|
|
|
|
|
var world *model.DfWorld
|
2022-05-05 13:55:33 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
if *l {
|
|
|
|
f = config.LastFile
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f) > 0 {
|
|
|
|
w, err := model.Parse(f, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
runtime.GC()
|
|
|
|
world = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.StartServer(config, world, static)
|
2022-04-13 15:01:20 +03:00
|
|
|
if err != nil {
|
2022-05-09 18:20:31 +03:00
|
|
|
log.Fatal(err)
|
2022-04-13 15:01:20 +03:00
|
|
|
}
|
2022-05-09 18:20:31 +03:00
|
|
|
},
|
|
|
|
}
|
2022-04-09 19:37:44 +03:00
|
|
|
|
2022-05-09 18:20:31 +03:00
|
|
|
func main() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
2022-04-26 18:39:24 +03:00
|
|
|
}
|
2022-04-09 12:01:04 +03:00
|
|
|
}
|
2022-05-09 18:20:31 +03:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&f, "world", "w", "", "path to legends.xml")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&c, "config", "c", "", "config file")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&subUri, "subUri", "u", "", "run on /<subUri>")
|
|
|
|
l = rootCmd.PersistentFlags().BoolP("last", "l", false, "open last file")
|
|
|
|
p = rootCmd.PersistentFlags().BoolP("profile", "P", false, "start profiling")
|
|
|
|
d = rootCmd.PersistentFlags().BoolP("debug", "d", false, "show debug data")
|
|
|
|
s = rootCmd.PersistentFlags().BoolP("serverMode", "s", false, "run in server mode (disables file chooser)")
|
|
|
|
port = rootCmd.PersistentFlags().IntP("port", "p", 58881, "use specific port")
|
|
|
|
}
|