diff --git a/main.go b/main.go index 09cf630..dbf4a8b 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ func main() { dbFileArg := flag.String("dbfile", "./data.sqlite3", "File that holds the server's sqlite3 database. Will be created if it does not exist. Default is \"./data.sqlite3\".") tilepackFileArg := flag.String("tilepack", "", "File that will be sent to clients when they request a tile pack. Required.") apkFileArg := flag.String("apk", "", "APK file with the client app. If not specified, no APK will be available (404).") + reinitDBArg := flag.Bool("reinit-db", false, "Reinitializes the DB, which means all the tables will be recreated, deleting all data.") flag.Parse() @@ -25,7 +26,7 @@ func main() { os.Exit(1) } - s := server.New(*dbFileArg, *tilepackFileArg, *apkFileArg) + s := server.New(*dbFileArg, *tilepackFileArg, *apkFileArg, *reinitDBArg) sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT) diff --git a/server/server.go b/server/server.go index 4fd94ac..f4890f3 100644 --- a/server/server.go +++ b/server/server.go @@ -35,14 +35,16 @@ type ServerConfig struct { DbPath string TilepackPath string ApkPath string + ReinitDB bool } -func New(dbPath, tilepackPath, apkPath string) *Server { +func New(dbPath, tilepackPath, apkPath string, reinitDB bool) *Server { return &Server{ config: ServerConfig{ DbPath: dbPath, TilepackPath: tilepackPath, ApkPath: apkPath, + ReinitDB: reinitDB, }, } } @@ -52,7 +54,7 @@ func (s *Server) Run(ctx context.Context) { s.log.SetLevel(logrus.DebugLevel) s.ctx = ctx - s.setupDB(true) + s.setupDB() s.setupTiles() router := s.setupRouter() @@ -89,14 +91,14 @@ func (s *Server) getDbConn() *sqlite.Conn { return conn } -func (s *Server) setupDB(reinit bool) { +func (s *Server) setupDB() { dbpool, err := sqlitex.Open(fmt.Sprintf("file:%s", s.config.DbPath), 0, 10) if err != nil { s.log.WithError(err).Fatal("Failed to open/create DB.") } s.dbpool = dbpool - if reinit { + if s.config.ReinitDB { conn := s.getDbConn() defer s.dbpool.Put(conn)