Генерирование роутов для учеток пользователей

This commit is contained in:
Ymnuk 2023-07-19 12:05:56 +03:00
parent 550da898b4
commit a163388557
23 changed files with 163 additions and 21 deletions

View File

@ -11,18 +11,22 @@ licenseUrl: http://www.apache.org/licenses/LICENSE-2.0.html
host: example.com
basePath: /api
db:
sqlite: true
tables:
- name: test___test_1
pk: uuid
fields:
- name: f1
recursive: true
- name: test2
pk: int
fields:
- name: f1
- name: f2
fks:
- name: test1
- name: test___test_1
tableName: test___test_1
backend:
config:
- name: nominatim
@ -31,3 +35,5 @@ backend:
- name: tile_server
type: string
help: Tile server for download tiles map
frontend:

View File

@ -127,7 +127,7 @@ func generateModelBase() {
if err := PrepareTmplFile("tmpl/backend/main.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "main.go")); err != nil {
log.Fatal(err)
}
if err := PrepareTmplFile("tmpl/backend/lib/config.tmpl", Project.Backend, filepath.Join(AppConfig.OutdirBackend, "lib", "config.go")); err != nil {
if err := PrepareTmplFile("tmpl/backend/lib/config.tmpl", Project, filepath.Join(AppConfig.OutdirBackend, "lib", "config.go")); err != nil {
log.Fatal(err)
}

View File

@ -19,6 +19,9 @@ func execCommands() {
log.Fatal(err)
} else {
strs := strings.Split(string(buff), "\n")
if Project.DB.SQLite {
strs = append(strs, "gorm.io/driver/sqlite")
}
if len(strs) > 0 {
for _, str := range strs {
cmd := exec.Command("go", "get", str)

View File

@ -40,7 +40,7 @@ func PrepareMetadata(project *structs.Project) {
}
if project.DB.Tables[i].Recursive {
project.DB.Tables[i].FkFields = append(project.DB.Tables[i].FkFields, structs.Field{
Name: "parent_id",
Name: "parent",
Description: "Recursive foreign key for self table",
})
switch project.DB.Tables[i].Pk {
@ -61,8 +61,13 @@ func PrepareMetadata(project *structs.Project) {
}
if project.DB.Tables[i].FKs[j].TableID == uuid.Nil && project.DB.Tables[i].FKs[j].TableName != "" {
for k := range project.DB.Tables {
if project.DB.Tables[i].Name == project.DB.Tables[i].FKs[j].TableName {
if project.DB.Tables[k].Name == project.DB.Tables[i].FKs[j].TableName {
project.DB.Tables[i].FKs[j].TableID = project.DB.Tables[k].ID
project.DB.Tables[i].FKs[j].Type = project.DB.Tables[k].Pk
/*project.DB.Tables[i].FkFields = append(project.DB.Tables[i].FkFields, structs.Field{
Name: fmt.Sprintf("%s", project.DB.Tables[k].Name),
Type: project.DB.Tables[k].Pk,
})*/
}
}
}
@ -70,6 +75,7 @@ func PrepareMetadata(project *structs.Project) {
for k := range project.DB.Tables {
if project.DB.Tables[k].ID == project.DB.Tables[i].FKs[j].TableID {
project.DB.Tables[i].FKs[j].TableName = project.DB.Tables[k].Name
project.DB.Tables[k].Children = append(project.DB.Tables[k].Children, project.DB.Tables[i].Name)
}
}
}
@ -77,8 +83,9 @@ func PrepareMetadata(project *structs.Project) {
log.Fatalf("Error foreign key for '%s' table", project.DB.Tables[i].FKs[j].TableID.String())
}
project.DB.Tables[i].FkFields = append(project.DB.Tables[i].FkFields, structs.Field{
Name: fmt.Sprintf("%s_id", strings.ToLower(project.DB.Tables[i].FKs[j].TableName)),
Description: fmt.Sprintf("Foreign key for \"%s\" table", project.DB.Tables[i].FKs[j].TableName),
Name: fmt.Sprintf("%s", strings.ToLower(project.DB.Tables[i].FKs[j].TableName)),
Type: project.DB.Tables[i].Pk,
Description: fmt.Sprintf("Foreign key for \\\"%s\\\" table", project.DB.Tables[i].FKs[j].TableName),
})
}
}

View File

@ -100,6 +100,8 @@ func fieldType(field *structs.Field) string {
return "*int"
case "bigint":
return "*int64"
case "uuid":
return "*uuid.UUID"
default:
log.Fatalf("Unknow format %s", field.Type)
}
@ -124,6 +126,8 @@ func fieldTypeDB(field *structs.Field) string {
return "INT"
case "bigint":
return "BIGINT"
case "uuid":
return "UUID"
default:
log.Fatalf("Unknow format %s", field.Type)
}

View File

@ -13,6 +13,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
{{ if .DB.SQLite }}"gorm.io/driver/sqlite"{{ end }}
"gorm.io/gorm/logger"
)
@ -53,6 +54,14 @@ func DBConnect() {
if err != nil {
log.Fatal(err)
}
{{ if .DB.SQLite }}
case "sqlite":
dsn := lib.AppConfig.DbPath
DB, err = gorm.Open(sqlite.Open(dsn), &conf)
if err != nil {
log.Fatal(err)
}
{{ end }}
default:
log.Fatal("Server DB not set")
}

View File

@ -2,16 +2,11 @@
package model
import (
"time"
"gorm.io/gorm"
)
import "time"
type BaseInt struct {
ID uint64 `gorm:"type:autoIncrement;primaryKey" json:"id,omitempty"`
ID uint64 `gorm:"primaryKey" json:"id,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
DeletedAt gorm.DeletedAt `sql:"index" json:"deletedAt"`
// DeletedAt *time.Time `sql:"index" json:"deletedAt"`
}

View File

@ -13,7 +13,7 @@ type Base struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;" json:"id,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
DeletedAt gorm.DeletedAt `sql:"index" json:"deletedAt"`
// DeletedAt *time.Time `sql:"index" json:"deletedAt"`
}
// BeforeCreate will set a UUID rather than numeric ID.

View File

@ -3,20 +3,30 @@
package model
import (
"time"
// "time"
uuid "github.com/satori/go.uuid"
)
// {{ fieldNamePrepare .Name }} {{ .Description}}
type {{ fieldNamePrepare .Name }} struct {
{{ if eq .Pk "uuid" }}
Base
{{ else }}
BaseInt
{{ end }}
{{ range $index, $field := .Fields }}
{{ fieldName $field }} {{ fieldType $field }} {{ fieldDescript $field }}
{{ end }}
{{ range $index, $field := .Children }}
{{ fieldNamePrepare $field }} []{{ fieldNamePrepare $field }}
{{ end }}
{{ range $index, $field := .FkFields }}
{{ fieldName $field }}ID {{ fieldType $field }} {{ fieldDescript $field }}
{{ fieldName $field }} *{{ if eq $field.Name "Parent" }}{{ fieldNamePrepare $.Name }}{{ else }}{{ fieldName $field }}{{ end }} {{ fieldDescript $field }}
{{ end }}
}
{{ $varNameField := fieldNamePrepare .Name }}

View File

@ -1,6 +1,6 @@
package model
// Роли пользователей
// Role Роли пользователей
type Role struct {
Base
Name string `gorm:"type:varchar(20);comment:Название роли" json:"name"`

View File

@ -2,7 +2,7 @@ package model
import uuid "github.com/satori/go.uuid"
// Связь пользователя и роли
// UserRole Связь пользователя и роли
type UserRole struct {
Base
UserID uuid.UUID `gorm:"column:id_user"`

View File

@ -10,6 +10,7 @@ import (
"gorm.io/gorm"
)
// User Пользователи
type User struct {
Base
Login string `json:"login"`

View File

@ -3,8 +3,11 @@ package lib
var AppConfig struct {
Env string `arg:"-e,env:ENV" default:"prod" help:"Environment for application. dev or prod. Default prod"`
MigrateDB bool `arg:"-m,--migrate,env:DB_MIGRATE" default:"false" help:"Run migrate Database"`
DBInstance string `arg:"--db-instance,env:DB_INSTANCE" default:"postgres" help:"postgres (PostgreSQL) or mysql (MySQL/MariaDB)"`
DBInstance string `arg:"--db-instance,env:DB_INSTANCE" default:"{{ if .DB.SQLite }}sqlite{{ else }}postgres{{ end }}" help:"postgres (PostgreSQL) or mysql (MySQL/MariaDB){{ if .DB.SQLite }} or sqlite (SQLite3){{ end }}"`
DbName string `arg:"--db-name,env:DB_NAME" default:"dbname" help:"Database name for connect"`
{{ if .DB.SQLite }}
DbPath string `arg:"--db-path,env:DB_PATH" default:"./{{ .Name }}.db" help:"Path to database for SQLite3"`
{{ end }}
DbAddress string `arg:"--db-host,env:DB_HOST" default:"localhost" help:"Database host for connect"`
DbPort string `arg:"--db-port,env:DB_PORT" default:"5432" help:"Database port for connect"`
DbLogin string `arg:"--db-login,env:DB_LOGIN" default:"db" help:"Database login for connect"`
@ -17,7 +20,7 @@ var AppConfig struct {
LdapPassword string `arg:"--ldap-pwd,env:LDAP_PWD" help:"Ldap password for credential"`
LdapSearchBase string `arg:"--ldap-search-base,env:LDAP_SEARCH_BASE" help:"Ldap search base for search users"`
{{ range $index, $field := .Config }}
{{ range $index, $field := .Backend.Config }}
{{ configParamName $field }} {{ configParamType $field }} {{ configParamTag $field }}
{{ end }}
}

View File

@ -39,6 +39,11 @@ var e *echo.Echo
// @host {{ .Host }}
// @BasePath {{ .BasePath }}
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
func main() {
arg.MustParse(&lib.AppConfig)

View File

@ -20,6 +20,18 @@ func Init(c *echo.Group) {
c.POST("/login", login)
}
// Authenticate auth
// @Summary Authenticate in system
// @Description Get authentication data
// @Tags users
// @Accept json
// @Produce json
// @Param body body model.User true "query params"
// @Success 200 {object} structs.Result
// @Failure 400 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/auth [post]
func login(c echo.Context) error {
var err error
var userForm model.User

View File

@ -13,6 +13,20 @@ import (
uuid "github.com/satori/go.uuid"
)
// DeleteUser deleteUser
// @Summary Удаление пользователя
// @Description Удаление информации о пользователе
// @Tags users
// @Accept json
// @Produce json
// @Param id path string true "Идентификатор пользователя"
// @Success 200 {object} structs.Result
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user/{id} [delete]
// @Security BearerAuth
func delete(c echo.Context) error {
user := c.Get("user").(*jwt.Token)

View File

@ -11,6 +11,20 @@ import (
uuid "github.com/satori/go.uuid"
)
// GetUser getUser
// @Summary Получить пользователя
// @Description Получения информации о пользователе по его id
// @Tags users
// @Accept json
// @Produce json
// @Param id path string true "Идентификатор пользователя"
// @Success 200 {object} model.User
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user/{id} [get]
// @Security BearerAuth
func get(c echo.Context) error {
id := uuid.FromStringOrNil(c.Param("id"))

View File

@ -12,6 +12,19 @@ import (
"github.com/labstack/echo/v4"
)
// ListUsers listUsers
// @Summary Получить пользователя
// @Description Возвращает список пользователей
// @Tags users
// @Accept json
// @Produce json
// @Success 200 {object} []model.User
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user [get]
// @Security BearerAuth
func listUsers(c echo.Context) error {
user := c.Get("user").(*jwt.Token)

View File

@ -14,6 +14,20 @@ import (
"github.com/labstack/echo/v4"
)
// CreateUser createUser
// @Summary Создать пользователя
// @Description Добавление нового пользователя в систему
// @Tags users
// @Accept json
// @Produce json
// @Param body body model.User true "Описание нового пользователя"
// @Success 200 {object} model.User
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user [post]
// @Security BearerAuth
func post(c echo.Context) error {
user := c.Get("user").(*jwt.Token)

View File

@ -15,6 +15,21 @@ import (
uuid "github.com/satori/go.uuid"
)
// UpdateUser updateUser
// @Summary Обновление пользователя
// @Description Обновление информации о пользователе, а так же смена пароля при необходимости
// @Tags users
// @Accept json
// @Produce json
// @Param id path string true "Идентификатор пользователя"
// @Param body body model.User true "Описание существующего пользователя"
// @Success 200 {object} model.User
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user/{id} [post]
// @Security BearerAuth
func put(c echo.Context) error {
user := c.Get("user").(*jwt.Token)

View File

@ -16,6 +16,19 @@ func Init(c *echo.Group) {
}
// ListRoles listRoles
// @Summary Список ролей для пользователей в системе
// @Description Получить список пролей для отображения и назначения пользователям
// @Tags users
// @Accept json
// @Produce json
// @Success 200 {object} structs.Result
// @Failure 400 {object} structs.Result
// @Failure 401 {object} structs.Result
// @Failure 404 {object} structs.Result
// @Failure 500 {object} structs.Result
// @Router /api/user/roles [get]
// @Security BearerAuth
func listRoles(c echo.Context) error {
var err error
tx := db.BeginTransation()

View File

@ -42,6 +42,7 @@ func main() {
} else {
log.Fatal("Ошибка открытия файла")
}
lib.PrepareMetadata(lib.Project)
lib.Generate()
}

View File

@ -3,6 +3,7 @@ package structs
import uuid "github.com/satori/go.uuid"
type DB struct {
SQLite bool `yaml:"sqlite,omitempty"`
Tables []Table `yaml:"tables,omitempty"`
Description string `yaml:"description,omitempty" json:"nescription,omitempty"`
}
@ -18,6 +19,7 @@ type Table struct {
Recursive bool `yaml:"recursive,omitempty" json:"recursive,omitempty"` // Рекурсивная таблица
FKs []FK `yaml:"fks,omitempty" json:"fks,omitempty"` // Внешние ключи
FkFields []Field `yaml:"-" json:"-"` // Поля с описанием внешних ключей
Children []string `yaml:"-" json:"-"` // Дочерние (подчиненные) таблицы
}
// Описание поля
@ -34,6 +36,7 @@ type Field struct {
type FK struct {
ID uuid.UUID `yaml:"id,omitempty" json:"id,omitempty"` // Идентификатор внешнего ключа
Name string `yaml:"name,omitempty" json:"name,omitempty"` // Название ключа
Type string `yaml:"-"` // Тип первичного ключа
TableID uuid.UUID `yaml:"idTable,omitempty" json:"idTable,omitempty"` // Идентификатор таблицы, на которую ссылаемся
TableName string `yaml:"tableName,omitempty" json:"tableName,omitempty"` // Название таблицы, на которую ссылаемся
Description string `yaml:"description,omitempty" json:"description,omitempty"` // Описание внешнего ключа