remote DB reset

This commit is contained in:
zegkljan 2022-02-12 11:34:29 +01:00
parent e2aa0d1aea
commit 3615deb985
3 changed files with 23 additions and 5 deletions

View File

@ -4,6 +4,7 @@ const (
URIPing = "/ping"
URIHardFail = "/hard-fail"
URISoftFail = "/soft-fail"
URIReinit = "/reinit"
URIMapPack = "/mappack"
URIHandshake = "/handshake"
URIData = "/data"

View File

@ -22,6 +22,7 @@ func internalError(gc *gin.Context, err error) {
}
func (s *Server) setupRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
@ -68,6 +69,8 @@ func (s *Server) setupRouter() *gin.Engine {
entry.Error(msg)
} else if statusCode >= 400 {
entry.Warn(msg)
} else if path == URIPing {
entry.Debug(msg)
} else {
entry.Info(msg)
}
@ -84,6 +87,7 @@ func (s *Server) setupRouter() *gin.Engine {
router.GET(URISoftFail, func(gc *gin.Context) {
gc.JSON(http.StatusOK, map[string]string{"error": "artificial fail"})
})
router.POST(URIReinit, s.handlePOSTReset)
// resources
router.GET(URIMapPack, s.handleGETTilepack)
@ -102,6 +106,15 @@ func (s *Server) setupRouter() *gin.Engine {
return router
}
func (s *Server) handlePOSTReset(gc *gin.Context) {
err := s.reinitDB()
if err != nil {
internalError(gc, err)
return
}
gc.Status(http.StatusOK)
}
func (s *Server) handleGETTilepack(gc *gin.Context) {
gc.File(s.config.TilepackPath)
}

View File

@ -100,11 +100,7 @@ func (s *Server) setupDB() {
s.dbpool = dbpool
if s.config.ReinitDB {
s.log.Info("Reinitializing DB.")
conn := s.getDbConn()
defer s.dbpool.Put(conn)
err = sqlitex.ExecScript(conn, initDB)
err = s.reinitDB()
if err != nil {
s.log.WithError(err).Fatal("init DB transaction failed")
}
@ -139,6 +135,14 @@ func (s *Server) setupTiles() {
s.mapPackSize = info.Size()
}
func (s *Server) reinitDB() error {
s.log.Info("Reinitializing DB.")
conn := s.getDbConn()
defer s.dbpool.Put(conn)
return sqlitex.ExecScript(conn, initDB)
}
func (s *Server) handshake(hc models.HandshakeChallenge) (models.UserID, error) {
conn := s.getDbConn()
defer s.dbpool.Put(conn)