mirror of
https://github.com/documize/community.git
synced 2025-07-24 15:49:44 +02:00
removed old API
This commit is contained in:
parent
e284a46f86
commit
562872a4a8
105 changed files with 337 additions and 14496 deletions
|
@ -20,7 +20,6 @@ import (
|
|||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/server/web"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// sql variantsa
|
||||
|
@ -30,14 +29,9 @@ const sqlVariantMariaDB string = "MariaDB"
|
|||
|
||||
var dbCheckOK bool // default false
|
||||
|
||||
// dbPtr is a pointer to the central connection to the database, used by all database requests.
|
||||
var dbPtr *sqlx.DB
|
||||
|
||||
// Check that the database is configured correctly and that all the required tables exist.
|
||||
// It must be the first function called in this package.
|
||||
func Check(runtime *env.Runtime) bool {
|
||||
dbPtr = runtime.Db
|
||||
|
||||
runtime.Log.Info("Database checks: started")
|
||||
|
||||
csBits := strings.Split(runtime.Flags.DBConn, "/")
|
||||
|
|
|
@ -18,63 +18,34 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/api"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/log"
|
||||
"github.com/documize/community/core/secrets"
|
||||
"github.com/documize/community/core/stringutil"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/server/web"
|
||||
)
|
||||
|
||||
// runSQL creates a transaction per call
|
||||
func runSQL(sql string) (id uint64, err error) {
|
||||
|
||||
if strings.TrimSpace(sql) == "" {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
|
||||
if err != nil {
|
||||
log.Error("runSql - failed to get transaction", err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := tx.Exec(sql)
|
||||
|
||||
if err != nil {
|
||||
log.IfErr(tx.Rollback())
|
||||
log.Error("runSql - unable to run sql", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
log.Error("runSql - unable to commit sql", err)
|
||||
return
|
||||
}
|
||||
|
||||
tempID, _ := result.LastInsertId()
|
||||
id = uint64(tempID)
|
||||
|
||||
return
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Create the tables in a blank database
|
||||
func Create(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
target := "/setup"
|
||||
status := http.StatusBadRequest
|
||||
|
||||
if api.Runtime.Flags.SiteMode == env.SiteModeNormal {
|
||||
if h.Runtime.Flags.SiteMode == env.SiteModeNormal {
|
||||
target = "/"
|
||||
status = http.StatusOK
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", target, nil)
|
||||
|
||||
if err != nil {
|
||||
log.Error("database.Create()'s error in defer ", err)
|
||||
h.Runtime.Log.Error("database.Create()'s error in defer ", err)
|
||||
}
|
||||
|
||||
http.Redirect(w, req, target, status)
|
||||
|
@ -82,20 +53,20 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
log.Error("database.Create()'s r.ParseForm()", err)
|
||||
h.Runtime.Log.Error("database.Create()'s r.ParseForm()", err)
|
||||
return
|
||||
}
|
||||
|
||||
dbname := r.Form.Get("dbname")
|
||||
dbhash := r.Form.Get("dbhash")
|
||||
|
||||
fmt.Println(dbname)
|
||||
fmt.Println(dbhash)
|
||||
fmt.Println(web.SiteInfo.DBname)
|
||||
fmt.Println(web.SiteInfo.DBhash)
|
||||
h.Runtime.Log.Info(dbname)
|
||||
h.Runtime.Log.Info(dbhash)
|
||||
h.Runtime.Log.Info(web.SiteInfo.DBname)
|
||||
h.Runtime.Log.Info(web.SiteInfo.DBhash)
|
||||
|
||||
if dbname != web.SiteInfo.DBname || dbhash != web.SiteInfo.DBhash {
|
||||
log.Error("database.Create()'s security credentials error ", errors.New("bad db name or validation code"))
|
||||
h.Runtime.Log.Error("database.Create()'s security credentials error ", errors.New("bad db name or validation code"))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -118,23 +89,22 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
|||
details.Password == "" ||
|
||||
details.Firstname == "" ||
|
||||
details.Lastname == "" {
|
||||
log.Error("database.Create() error ",
|
||||
errors.New("required field in database set-up form blank"))
|
||||
h.Runtime.Log.Error("database.Create() error ", errors.New("required field in database set-up form blank"))
|
||||
return
|
||||
}
|
||||
|
||||
if err = Migrate(api.Runtime, false /* no tables exist yet */); err != nil {
|
||||
log.Error("database.Create()", err)
|
||||
if err = Migrate(h.Runtime, false /* no tables exist yet */); err != nil {
|
||||
h.Runtime.Log.Error("database.Create()", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = setupAccount(details, secrets.GenerateSalt())
|
||||
err = setupAccount(h.Runtime, details, secrets.GenerateSalt())
|
||||
if err != nil {
|
||||
log.Error("database.Create()", err)
|
||||
h.Runtime.Log.Error("database.Create()", err)
|
||||
return
|
||||
}
|
||||
|
||||
api.Runtime.Flags.SiteMode = env.SiteModeNormal
|
||||
h.Runtime.Flags.SiteMode = env.SiteModeNormal
|
||||
}
|
||||
|
||||
// The result of completing the onboarding process.
|
||||
|
@ -152,7 +122,7 @@ type onboardRequest struct {
|
|||
|
||||
// setupAccount prepares the database for a newly onboard customer.
|
||||
// Once done, they can then login and use Documize.
|
||||
func setupAccount(completion onboardRequest, serial string) (err error) {
|
||||
func setupAccount(rt *env.Runtime, completion onboardRequest, serial string) (err error) {
|
||||
//accountTitle := "This is where you will find documentation for your all projects. You can customize this message from the settings screen."
|
||||
salt := secrets.GenerateSalt()
|
||||
password := secrets.GeneratePassword(completion.Password, salt)
|
||||
|
@ -162,10 +132,10 @@ func setupAccount(completion onboardRequest, serial string) (err error) {
|
|||
|
||||
sql := fmt.Sprintf("insert into organization (refid, company, title, message, domain, email, serial) values (\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\")",
|
||||
orgID, completion.Company, completion.CompanyLong, completion.Message, completion.URL, completion.Email, serial)
|
||||
_, err = runSQL(sql)
|
||||
_, err = runSQL(rt, sql)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Failed to insert into organization", err)
|
||||
rt.Log.Error("Failed to insert into organization", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -173,39 +143,70 @@ func setupAccount(completion onboardRequest, serial string) (err error) {
|
|||
|
||||
sql = fmt.Sprintf("insert into user (refid, firstname, lastname, email, initials, salt, password, global) values (\"%s\",\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", 1)",
|
||||
userID, completion.Firstname, completion.Lastname, completion.Email, stringutil.MakeInitials(completion.Firstname, completion.Lastname), salt, password)
|
||||
_, err = runSQL(sql)
|
||||
_, err = runSQL(rt, sql)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Failed with error", err)
|
||||
rt.Log.Error("Failed with error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Link user to organization.
|
||||
accountID := uniqueid.Generate()
|
||||
sql = fmt.Sprintf("insert into account (refid, userid, orgid, admin, editor) values (\"%s\", \"%s\", \"%s\",1, 1)", accountID, userID, orgID)
|
||||
_, err = runSQL(sql)
|
||||
_, err = runSQL(rt, sql)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Failed with error", err)
|
||||
rt.Log.Error("Failed with error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Set up default labels for main collection.
|
||||
labelID := uniqueid.Generate()
|
||||
sql = fmt.Sprintf("insert into label (refid, orgid, label, type, userid) values (\"%s\", \"%s\", \"My Project\", 2, \"%s\")", labelID, orgID, userID)
|
||||
_, err = runSQL(sql)
|
||||
_, err = runSQL(rt, sql)
|
||||
|
||||
if err != nil {
|
||||
log.Error("insert into label failed", err)
|
||||
rt.Log.Error("insert into label failed", err)
|
||||
}
|
||||
|
||||
labelRoleID := uniqueid.Generate()
|
||||
sql = fmt.Sprintf("insert into labelrole (refid, labelid, orgid, userid, canview, canedit) values (\"%s\", \"%s\", \"%s\", \"%s\", 1, 1)", labelRoleID, labelID, orgID, userID)
|
||||
_, err = runSQL(sql)
|
||||
_, err = runSQL(rt, sql)
|
||||
|
||||
if err != nil {
|
||||
log.Error("insert into labelrole failed", err)
|
||||
rt.Log.Error("insert into labelrole failed", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// runSQL creates a transaction per call
|
||||
func runSQL(rt *env.Runtime, sql string) (id uint64, err error) {
|
||||
if strings.TrimSpace(sql) == "" {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
tx, err := rt.Db.Beginx()
|
||||
if err != nil {
|
||||
rt.Log.Error("runSql - failed to get transaction", err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := tx.Exec(sql)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
rt.Log.Error("runSql - unable to run sql", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
rt.Log.Error("runSql - unable to commit sql", err)
|
||||
return
|
||||
}
|
||||
|
||||
tempID, _ := result.LastInsertId()
|
||||
id = uint64(tempID)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ func migrations(lastMigration string) (migrationsT, error) {
|
|||
}
|
||||
|
||||
// migrate the database as required, by applying the migrations.
|
||||
func (m migrationsT) migrate(runtime env.Runtime, tx *sqlx.Tx) error {
|
||||
func (m migrationsT) migrate(runtime *env.Runtime, tx *sqlx.Tx) error {
|
||||
for _, v := range m {
|
||||
runtime.Log.Info("Processing migration file: " + v)
|
||||
|
||||
|
@ -96,7 +96,7 @@ func (m migrationsT) migrate(runtime env.Runtime, tx *sqlx.Tx) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func lockDB(runtime env.Runtime) (bool, error) {
|
||||
func lockDB(runtime *env.Runtime) (bool, error) {
|
||||
b := make([]byte, 2)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
|
@ -105,7 +105,7 @@ func lockDB(runtime env.Runtime) (bool, error) {
|
|||
wait := ((time.Duration(b[0]) << 8) | time.Duration(b[1])) * time.Millisecond / 10 // up to 6.5 secs wait
|
||||
time.Sleep(wait)
|
||||
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
tx, err := runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -138,8 +138,8 @@ func lockDB(runtime env.Runtime) (bool, error) {
|
|||
return true, err // success!
|
||||
}
|
||||
|
||||
func unlockDB() error {
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
func unlockDB(rt *env.Runtime) error {
|
||||
tx, err := rt.Db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -150,9 +150,9 @@ func unlockDB() error {
|
|||
return tx.Commit()
|
||||
}
|
||||
|
||||
func migrateEnd(runtime env.Runtime, tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
func migrateEnd(runtime *env.Runtime, tx *sqlx.Tx, err error, amLeader bool) error {
|
||||
if amLeader {
|
||||
defer func() { unlockDB() }()
|
||||
defer func() { unlockDB(runtime) }()
|
||||
if tx != nil {
|
||||
if err == nil {
|
||||
tx.Commit()
|
||||
|
@ -188,7 +188,7 @@ func getLastMigration(tx *sqlx.Tx) (lastMigration string, err error) {
|
|||
}
|
||||
|
||||
// Migrate the database as required, consolidated action.
|
||||
func Migrate(runtime env.Runtime, ConfigTableExists bool) error {
|
||||
func Migrate(runtime *env.Runtime, ConfigTableExists bool) error {
|
||||
amLeader := false
|
||||
|
||||
if ConfigTableExists {
|
||||
|
@ -201,7 +201,7 @@ func Migrate(runtime env.Runtime, ConfigTableExists bool) error {
|
|||
amLeader = true // what else can you do?
|
||||
}
|
||||
|
||||
tx, err := (*dbPtr).Beginx()
|
||||
tx, err := runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
|
@ -236,8 +236,8 @@ func Migrate(runtime env.Runtime, ConfigTableExists bool) error {
|
|||
for targetMigration != lastMigration {
|
||||
time.Sleep(time.Second)
|
||||
runtime.Log.Info("Waiting for database migration completion")
|
||||
tx.Rollback() // ignore error
|
||||
tx, err := (*dbPtr).Beginx() // need this in order to see the changed situation since last tx
|
||||
tx.Rollback() // ignore error
|
||||
tx, err := runtime.Db.Beginx() // need this in order to see the changed situation since last tx
|
||||
if err != nil {
|
||||
return migrateEnd(runtime, tx, err, amLeader)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue