db reinitialization flag

This commit is contained in:
zegkljan 2022-01-30 10:37:35 +01:00
parent b512bc0d2f
commit 7471c89143
2 changed files with 8 additions and 5 deletions

View File

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

View File

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