231 lines
7.4 KiB
Go
231 lines
7.4 KiB
Go
package lib
|
|
|
|
import (
|
|
"log"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/lib/templ"
|
|
"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"),
|
|
filepath.Join(AppConfig.OutdirBackend, "route", "static"),
|
|
filepath.Join(AppConfig.OutdirBackend, "route", "api", "auth"),
|
|
filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "role"),
|
|
filepath.Join(AppConfig.OutdirBackend, "middleware"),
|
|
}
|
|
if len(dirs) > 0 {
|
|
for _, dir := range dirs {
|
|
if err := templ.MkdirIsNotExists(dir); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
backendGenerateStructs()
|
|
generateDB()
|
|
generateBackendTmpl()
|
|
|
|
execCommands()
|
|
backendFixApi()
|
|
}
|
|
|
|
// Генерирование структуры БД
|
|
func generateDB() {
|
|
|
|
generateModelBase()
|
|
}
|
|
|
|
func generateBackendTmpl() {
|
|
staticTmpls := []structs.TmplInOut{
|
|
// root
|
|
{
|
|
FileIn: "tmpl/backend/gitignore.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, ".gitignore"),
|
|
},
|
|
|
|
// lib
|
|
{
|
|
FileIn: "tmpl/backend/lib/connect-to-ldap.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "lib", "connect-to-ldap.go"),
|
|
},
|
|
|
|
// lib/salt
|
|
{
|
|
FileIn: "tmpl/backend/lib/salt/generate-salt.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "lib", "salt", "generate-salt.go"),
|
|
},
|
|
|
|
// structs
|
|
{
|
|
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"),
|
|
},
|
|
|
|
// db
|
|
{
|
|
FileIn: "tmpl/backend/db/transaction.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "db", "transaction.go"),
|
|
},
|
|
|
|
// db/model
|
|
{
|
|
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"),
|
|
},
|
|
|
|
// routes
|
|
{
|
|
FileIn: "tmpl/backend/route/static/gitkeep.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "route", "static", ".gitkeep"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/route/static/static.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "route", "static", "static.html"),
|
|
},
|
|
// middlewares
|
|
{
|
|
FileIn: "tmpl/backend/middleware/middleware.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "middleware", "middleware.go"),
|
|
},
|
|
}
|
|
if len(staticTmpls) > 0 {
|
|
for _, staticTmpl := range staticTmpls {
|
|
if err := templ.WriteTmplFile(staticTmpl.FileIn, staticTmpl.FileOut); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
generateBackendRest()
|
|
}
|
|
|
|
func generateModelBase() {
|
|
// main
|
|
if err := templ.PrepareTmplFile("tmpl/backend/main.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "main.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := templ.PrepareTmplFile("tmpl/backend/lib/config.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "lib", "config.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// db
|
|
if err := templ.PrepareTmplIsNotExists("tmpl/backend/db/model/user.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "db", "model", "user.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := templ.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 := templ.PrepareTmplFile("tmpl/backend/db/model/model.tmpl", table, filepath.Join(AppConfig.OutdirBackend, "db", "model", strings.ToLower(table.Name)+".go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if err := templ.PrepareTmplFile("tmpl/backend/db/migrate.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "db", "migrate.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// middleware
|
|
if err := templ.PrepareTmplFile("tmpl/backend/middleware/middleware.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "middleware", "middleware.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := templ.PrepareTmplFile("tmpl/backend/middleware/in-role.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "middleware", "in-role.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// route
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/route.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "route.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// route/api
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/index.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "index.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// route/api/auth
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/auth/index.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "auth", "index.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// route/api/user
|
|
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/index.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "index.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/ldap-search.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "ldap-search.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/list-users.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "list-users.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/lock.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "lock.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/unlock.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "unlock.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/delete.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "delete.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
staticTmpls := []structs.TmplInOut{
|
|
{
|
|
FileIn: "tmpl/backend/route/api/user/get.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "get.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/route/api/user/post.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "post.go"),
|
|
},
|
|
{
|
|
FileIn: "tmpl/backend/route/api/user/put.tmpl",
|
|
FileOut: filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "put.go"),
|
|
},
|
|
}
|
|
for _, item := range staticTmpls {
|
|
if err := templ.PrepareTmplIsNotExists(item.FileIn, Project, item.FileOut); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// route/api/user/role
|
|
if err := templ.PrepareTmplFile("tmpl/backend/route/api/user/role/index.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "route", "api", "user", "role", "index.go")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|