alist/bootstrap/data.go

59 lines
1.1 KiB
Go
Raw Normal View History

2022-06-25 14:05:02 +00:00
package bootstrap
import (
2022-06-26 11:09:28 +00:00
"github.com/alist-org/alist/v3/cmd/args"
2022-06-25 14:05:02 +00:00
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils/random"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func InitData() {
initUser()
}
func initUser() {
admin, err := db.GetAdmin()
2022-06-26 11:09:28 +00:00
adminPassword := random.String(8)
if args.Dev {
adminPassword = "admin"
}
2022-06-25 14:05:02 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
admin = &model.User{
Username: "admin",
2022-06-26 11:09:28 +00:00
Password: adminPassword,
2022-06-25 14:05:02 +00:00
Role: model.ADMIN,
BasePath: "/",
2022-06-26 08:39:02 +00:00
Webdav: true,
2022-06-25 14:05:02 +00:00
}
if err := db.CreateUser(admin); err != nil {
panic(err)
}
} else {
panic(err)
}
}
guest, err := db.GetGuest()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
guest = &model.User{
Username: "guest",
2022-06-26 08:39:02 +00:00
Password: "guest",
ReadOnly: true,
Webdav: true,
2022-06-25 14:05:02 +00:00
Role: model.GUEST,
BasePath: "/",
}
if err := db.CreateUser(guest); err != nil {
panic(err)
}
} else {
panic(err)
}
}
log.Infof("admin password: %+v", admin.Password)
}