105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package lib
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/structs"
|
|
)
|
|
|
|
func Backend() {
|
|
// Создаем необходимые директории
|
|
dirs := []string{
|
|
filepath.Join(AppConfig.OutdirBackend, "db", "model"),
|
|
filepath.Join(AppConfig.OutdirBackend, "lib", "salt"),
|
|
filepath.Join(AppConfig.OutdirBackend, "structs"),
|
|
}
|
|
if len(dirs) > 0 {
|
|
for _, dir := range dirs {
|
|
if err := os.MkdirAll(dir, 0775); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
generateStaticTmpl()
|
|
generateDB()
|
|
|
|
execCommands()
|
|
}
|
|
|
|
// Генерирование структуры БД
|
|
func generateDB() {
|
|
|
|
generateModelBase()
|
|
}
|
|
|
|
func generateStaticTmpl() {
|
|
staticTmpls := []structs.TmplInOut{
|
|
{
|
|
FileIn: "tmpl/backend/lib/salt/generate-salt.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "lib", "salt", "generate-salt.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/structs/filter-request.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "structs", "filter-request.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/structs/jwt-custom-claim.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "structs", "jwt-custom-claim.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/structs/result.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "structs", "result.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/db/model/base.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "model", "base.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/db/model/base-int.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "model", "base-int.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/db/model/role.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "model", "role.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/db/model/user-role.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "model", "user-role.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/db/transaction.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "transaction.go"),
|
|
},
|
|
}
|
|
if len(staticTmpls) > 0 {
|
|
for _, staticTmpl := range staticTmpls {
|
|
if err := WriteTmplFile(staticTmpl.FileIn, staticTmpl.FileOut); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func generateModelBase() {
|
|
if err := PrepareTmplFile("tmpl/backend/db/model/user.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "db", "model", "user.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
/*if err := PrepareTmplFile("tmpl/backend/go.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "go.mod")); err != nil {
|
|
log.Fatal(err)
|
|
}*/
|
|
if err := PrepareTmplFile("tmpl/backend/db/db.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "db", "db.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if len(Project.DB.Tables) > 0 {
|
|
for _, table := range Project.DB.Tables {
|
|
if err := PrepareTmplFile("tmpl/backend/db/model/model.tmpl", table, filepath.Join(AppConfig.OutdirBackend, "db", "model", strings.ToLower(table.Name)+".go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
}
|