func deleteAllFromDB(db gorm.DB, selectedType string) error { switch selectedType { case "jobs": db.DropTableIfExists(&application.Job{}) db.CreateTable(&application.Job{}) case "freelancers": db.DropTableIfExists(&application.Freelancer{}) db.CreateTable(&application.Freelancer{}) } return nil }
func ListenAndServe(addr string, auth Auth, dbType DatabaseType) { var db *gorm.DB var err error if dbType == DB_MySQL { dbConn := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)", auth.UserName, auth.Password) db, err = gorm.Open("mysql", dbConn+"/hugocms_"+auth.UserName+"?charset=utf8&parseTime=True&loc=Local") } else { db, err = gorm.Open("sqlite3", "hugocms_"+auth.UserName+".db") } if err != nil { handleError(err) } db.LogMode(true) // to reset to an empty database drop the settings table if !db.HasTable(&models.Settings{}) { for _, table := range hugocms_qor.Tables { if err := db.DropTableIfExists(table).Error; err != nil { handleError(err) } } } for _, table := range hugocms_qor.Tables { if err := db.AutoMigrate(table).Error; err != nil { handleError(err) } } // Because this is a singleton a single record must already exist in the database (there is no create through QOR admin just update) db.FirstOrCreate(&models.Settings{}) siteName := fmt.Sprintf("%s - Hugo CMS", auth.UserName) if err := setupConfig(addr, siteName, db, auth); err != nil { handleError(err) } // Add session support - used by Auth sessionLifetime := 3600 // session lifetime in seconds SessionManager, err = session.NewManager("memory", fmt.Sprintf(`{"cookieName":"gosessionid","gclifetime":%d}`, sessionLifetime)) if err != nil { handleError(err) } go SessionManager.GC() // Create Hugo's content directory if it doesnt exist // TODO read content dir from config if _, err := os.Stat("./content"); os.IsNotExist(err) { err = os.MkdirAll("./content", os.ModePerm) } mux := http.NewServeMux() mux.Handle("/", http.FileServer(http.Dir("public"))) adm := hugocms_qor.SetupAdmin() adm.MountTo("/admin", mux) adm.GetRouter().Post("/auth", func(ctx *admin.Context) { // we will only hit this on succesful login - redirect to admin dashboard w := ctx.Writer r := ctx.Request http.Redirect(w, r, "/admin", http.StatusFound) }) adm.GetRouter().Get("/logout", func(ctx *admin.Context) { w := ctx.Writer r := ctx.Request sess, err := SessionManager.SessionStart(w, r) if err != nil { handleError(err) } defer sess.SessionRelease(w) sess.Delete("User") http.Redirect(w, r, "/login", http.StatusFound) }) // NOTE: `system` is where QOR admin will upload files e.g. images - we map this to Hugo's static dir along with our other static assets // TODO read static dir from config // TODO read static assets list from config for _, path := range []string{"system", "css", "fonts", "images", "js", "login"} { mux.Handle(fmt.Sprintf("/%s/", path), http.FileServer(http.Dir("static"))) } if err := http.ListenAndServe(config.QOR.Addr, mux); err != nil { handleError(err) } // to re-generate site delete `config.json` if _, err := os.Stat("config.json"); os.IsNotExist(err) { hugocms_qor.CallSave(adm) } fmt.Printf("Listening on: %s\n", config.QOR.Addr) }
func FlushDatabase(db gorm.DB) { db.DropTableIfExists(&Question{}) db.DropTableIfExists(&Answer{}) db.DropTableIfExists(&Client{}) }
func DropOsInstallTables(db *gorm.DB, conf *config.Config) error { db.DropTableIfExists(osInstallTables...) return nil }