dorfylegends/backend/main.go

100 lines
2.6 KiB
Go
Raw Permalink Normal View History

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) {
2022-05-10 10:51:04 +03:00
fmt.Println(`
__ __ ____
/ / ___ ____ ___ ____ ____/ /____ / __ )_________ _ __________ _____
/ / / _ \/ __ \/ _ \/ __ \/ __ / ___/ / __ / ___/ __ \ | /| / / ___/ _ \/ ___/
/ /___/ __/ /_/ / __/ / / / /_/ (__ ) / /_/ / / / /_/ / |/ |/ (__ ) __/ /
/_____/\___/\__, /\___/_/ /_/\__,_/____/ /_____/_/ \____/|__/|__/____/\___/_/
/____/ ` + "\n ")
2022-05-09 18:20:31 +03:00
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() {
2022-05-10 18:49:15 +03:00
cobra.MousetrapHelpText = ""
2022-05-09 18:20:31 +03:00
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")
}