make an additional log when calling panic

This commit is contained in:
knoxfighter 2021-01-18 04:45:46 +01:00
parent eb3fde0d8b
commit 71924b3f9c
2 changed files with 18 additions and 1 deletions

View File

@ -29,6 +29,7 @@ func SetupAuth() {
cookieEncryptionKey, err := base64.StdEncoding.DecodeString(config.CookieEncryptionKey) cookieEncryptionKey, err := base64.StdEncoding.DecodeString(config.CookieEncryptionKey)
if err != nil { if err != nil {
log.Printf("Error decoding base64 cookie encryption key: %s", err)
panic(err) panic(err)
} }
sessionStore = sessions.NewCookieStore(cookieEncryptionKey) sessionStore = sessions.NewCookieStore(cookieEncryptionKey)
@ -39,11 +40,13 @@ func SetupAuth() {
auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), nil) auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), nil)
if err != nil { if err != nil {
log.Printf("Error opening sqlite or goem database: %s", err)
panic(err) panic(err)
} }
err = auth.db.AutoMigrate(&User{}) err = auth.db.AutoMigrate(&User{})
if err != nil { if err != nil {
log.Printf("Error AutoMigrating gorm database: %s", err)
panic(err) panic(err)
} }
@ -61,6 +64,7 @@ func SetupAuth() {
err := auth.addUser(user) err := auth.addUser(user)
if err != nil { if err != nil {
log.Printf("Error adding admin user to db: %s", err)
panic(err) panic(err)
} }
@ -210,6 +214,7 @@ func AuthMiddleware(next http.Handler) http.Handler {
username, ok := session.Values["username"] username, ok := session.Values["username"]
if !ok { if !ok {
http.Error(w, "Could not read username from sessioncookie", http.StatusUnauthorized)
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
return return
} }

View File

@ -23,12 +23,14 @@ type User struct {
func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
oldDB, err := leveldb.OpenFile(oldDBFile, nil) oldDB, err := leveldb.OpenFile(oldDBFile, nil)
if err != nil { if err != nil {
log.Printf("Error opening old leveldb: %s", err)
panic(err) panic(err)
} }
defer oldDB.Close() defer oldDB.Close()
newDB, err := gorm.Open(sqlite.Open(newDBFile), nil) newDB, err := gorm.Open(sqlite.Open(newDBFile), nil)
if err != nil { if err != nil {
log.Printf("Error open sqlite and gorm: %s", err)
panic(err) panic(err)
} }
defer func() { defer func() {
@ -39,15 +41,20 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
if db != nil { if db != nil {
err2 = db.Close() err2 = db.Close()
if err2 != nil { if err2 != nil {
log.Printf("Error closing real DB of gorm: %s", err2)
panic(err2) panic(err2)
} }
} }
}() }()
newDB.AutoMigrate(&User{}) err = newDB.AutoMigrate(&User{})
if err != nil {
log.Printf("Error autoMigrating sqlite database with user: %s", err)
}
oldUserData, err := oldDB.Get([]byte("httpauth::userdata"), nil) oldUserData, err := oldDB.Get([]byte("httpauth::userdata"), nil)
if err != nil { if err != nil {
log.Printf("Error getting `httpauth::userdata` from leveldb: %s", err)
panic(err) panic(err)
} }
@ -59,6 +66,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
} }
err = json.Unmarshal(oldUserData, &migrationData) err = json.Unmarshal(oldUserData, &migrationData)
if err != nil { if err != nil {
log.Printf("Error unmarshalling old ")
panic(err) panic(err)
} }
@ -66,6 +74,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
// check if password is "factorio", which was the default password in the old system // check if password is "factorio", which was the default password in the old system
decodedHash, err := base64.StdEncoding.DecodeString(datum.Hash) decodedHash, err := base64.StdEncoding.DecodeString(datum.Hash)
if err != nil { if err != nil {
log.Printf("Error decoding base64 hash: %s", err)
panic(err) panic(err)
} }
@ -76,6 +85,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
bcryptPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) bcryptPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
if err != nil { if err != nil {
log.Printf("Error generating has from password: %s", err)
panic(err) panic(err)
} }
@ -99,8 +109,10 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) {
oldDB.Close() oldDB.Close()
// delete oldDB // delete oldDB
log.Println("Deleting old leveldb database.")
err = os.RemoveAll(oldDBFile) err = os.RemoveAll(oldDBFile)
if err != nil { if err != nil {
log.Printf("Error removing leveldb: %s", err)
panic(err) panic(err)
} }
} }