diff --git a/config/config.go b/config/config.go index c5a07be..484b507 100644 --- a/config/config.go +++ b/config/config.go @@ -17,7 +17,7 @@ func New() *Config { return &Config{ Server: ServerConfig{ Port: getEnv("PORT", "8080"), - ConnectionString: getEnv("CONNECTION_STRING", "test.db"), + ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_users password=password dbname=astra_users_db port=5432 sslmode=disable TimeZone=Europe/Moscow"), }, } } diff --git a/deploy/templates/deployment.yaml b/deploy/templates/deployment.yaml index 7eb0aba..06799b8 100644 --- a/deploy/templates/deployment.yaml +++ b/deploy/templates/deployment.yaml @@ -28,7 +28,7 @@ spec: - name: PORT value: {{ .Values.app.server.port | quote }} - name: CONNECTION_STRING - value: /app/test.db + value: {{ .Values.app.db.connection_string }} {{- .Values.envs | toYaml | nindent 12 }} image: {{ .Values.app.image }}:{{ .Chart.AppVersion }} diff --git a/deploy/values.template.yaml b/deploy/values.template.yaml index 0adf226..aee6d6b 100644 --- a/deploy/values.template.yaml +++ b/deploy/values.template.yaml @@ -1,6 +1,8 @@ app: fullName: ${CI_PROJECT_TITLE} image: docker-registry.dopcore.com/${CI_PROJECT_PATH} + db: + connection_string: ${CONNECTION_STRING} server: port: ${SERVICE_PORT} diff --git a/dev/docker-compose.yaml b/dev/docker-compose.yaml new file mode 100644 index 0000000..59c5c7b --- /dev/null +++ b/dev/docker-compose.yaml @@ -0,0 +1,10 @@ +version: "3.9" +services: + astra_users_db: + image: postgres:14.8 + environment: + POSTGRES_DB: "astra_users_db" + POSTGRES_USER: "astra_users" + POSTGRES_PASSWORD: "password" + ports: + - "5432:5432" \ No newline at end of file diff --git a/go.mod b/go.mod index 1c9cfd1..d0814ae 100644 --- a/go.mod +++ b/go.mod @@ -2,22 +2,27 @@ module astra-users go 1.21rc1 +require ( + github.com/labstack/echo/v4 v4.10.2 + gorm.io/driver/postgres v1.5.2 + gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 +) + require ( github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.3.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/labstack/echo/v4 v4.10.2 // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect - github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - golang.org/x/crypto v0.6.0 // indirect - golang.org/x/net v0.7.0 // indirect - golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.7.0 // indirect + golang.org/x/crypto v0.11.0 // indirect + golang.org/x/net v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/text v0.11.0 // indirect golang.org/x/time v0.3.0 // indirect - gorm.io/driver/sqlite v1.5.2 // indirect - gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 // indirect ) diff --git a/go.sum b/go.sum index 2aa2d89..cb25674 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,14 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= +github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= @@ -17,36 +24,38 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= -github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= -golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= -golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= -golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= -golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= +golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50= +golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc= -gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4= -gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64= -gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= +gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8= gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ= gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= diff --git a/handler/organization.go b/handler/organization.go index 3f62dc4..0cab87c 100644 --- a/handler/organization.go +++ b/handler/organization.go @@ -2,24 +2,27 @@ package handler import ( "astra-users/model" + "astra-users/pkg/utils" "github.com/labstack/echo/v4" "net/http" ) func (h *Handler) CreateOrganization(c echo.Context) (err error) { - organization := new(model.Organization) + organization := new(model.OrganizationApi) if err = c.Bind(organization); err != nil { return c.NoContent(http.StatusBadRequest) } - tx := h.DB.Create(organization) + newOrg, err := utils.TypeConverter[model.Organization](organization) + + tx := h.DB.Create(&newOrg) if tx.RowsAffected == 0 { return c.NoContent(http.StatusBadGateway) } - return c.JSON(http.StatusCreated, organization) + return c.JSON(http.StatusCreated, newOrg) } func (h *Handler) GetOrganizations(c echo.Context) error { diff --git a/handler/position.go b/handler/position.go index bf2be54..68981ed 100644 --- a/handler/position.go +++ b/handler/position.go @@ -2,6 +2,7 @@ package handler import ( "astra-users/model" + "astra-users/pkg/utils" "errors" "github.com/labstack/echo/v4" "net/http" @@ -37,17 +38,19 @@ func (h *Handler) GetPosition(id int) (*model.Position, error) { } func (h *Handler) CreatePosition(c echo.Context) (err error) { - position := new(model.Position) + position := new(model.PositionApi) if err = c.Bind(position); err != nil { return c.NoContent(http.StatusBadRequest) } - tx := h.DB.Create(position) + newPosition, err := utils.TypeConverter[model.Position](position) + + tx := h.DB.Create(&newPosition) if tx.RowsAffected == 0 { return c.NoContent(http.StatusBadGateway) } - return c.JSON(http.StatusCreated, position) + return c.JSON(http.StatusCreated, newPosition) } diff --git a/handler/users.go b/handler/users.go index e3e62e7..1c93992 100644 --- a/handler/users.go +++ b/handler/users.go @@ -129,7 +129,7 @@ func (h *Handler) DeleteUser(c echo.Context) (err error) { } func (h *Handler) CreateUser(c echo.Context) (err error) { - var user = new(model.User) + user := new(model.APIUser) if err = c.Bind(user); err != nil { return c.NoContent(http.StatusBadRequest) @@ -137,13 +137,17 @@ func (h *Handler) CreateUser(c echo.Context) (err error) { user.Password = fmt.Sprintf("%x", sha256.Sum256([]byte(user.Password))) - tx := h.DB.Create(user) + newUser, err := utils.TypeConverter[model.User](&user) + + tx := h.DB.Create(&newUser) if tx.RowsAffected == 0 { return c.NoContent(http.StatusBadGateway) } - user.Password = "" + newUser.Password = "" - return c.JSON(http.StatusCreated, user) + h.DB.Find(&newUser.Position, newUser.PositionID) + + return c.JSON(http.StatusCreated, newUser) } diff --git a/main.go b/main.go index 51a8cb2..996adc4 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "astra-users/mw" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "gorm.io/driver/sqlite" + "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/logger" "log" @@ -27,7 +27,7 @@ func main() { }, ) - db, err := gorm.Open(sqlite.Open(config.Env.Server.ConnectionString), &gorm.Config{ + db, err := gorm.Open(postgres.Open(config.Env.Server.ConnectionString), &gorm.Config{ Logger: newLogger, }) if err != nil { diff --git a/model/organization.go b/model/organization.go index 61c4231..9f3b9f9 100644 --- a/model/organization.go +++ b/model/organization.go @@ -8,3 +8,8 @@ type Organization struct { ParentID uint `json:"parent_id"` Positions []Position `json:"positions,omitempty"` } + +type OrganizationApi struct { + Name string `json:"name" gorm:"unique;index"` + ParentID uint `json:"parent_id"` +} diff --git a/model/position.go b/model/position.go index bc79ca0..248ee0b 100644 --- a/model/position.go +++ b/model/position.go @@ -9,3 +9,9 @@ type Position struct { OrganizationID uint `json:"organization_id" gorm:"index;not null"` Users []User `json:"users,omitempty"` } + +type PositionApi struct { + Name string `json:"name" gorm:"unique;index"` + RightMask int `json:"right_mask"` + OrganizationID uint `json:"organization_id" gorm:"index;not null"` +} diff --git a/model/user.go b/model/user.go index de60721..77ad261 100644 --- a/model/user.go +++ b/model/user.go @@ -5,19 +5,16 @@ import "gorm.io/gorm" type ( User struct { gorm.Model - UserName string `json:"user_name" gorm:"unique;index"` - Password string `json:"password,omitempty"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - Role string `json:"role" gorm:"default:employee"` - PositionID uint `json:"-"` - Position Position `gorm:"-:migration"` + APIUser + Position Position `gorm:"-:migration"` } ) type APIUser struct { - UserName string `json:"user_name" gorm:"unique;index"` - Password string `json:"password,omitempty"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` + UserName string `json:"user_name" gorm:"unique;index"` + Password string `json:"password,omitempty"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Role string `json:"role" gorm:"default:employee"` + PositionID uint `json:"position_id,omitempty"` } diff --git a/pkg/utils/type-coverter.go b/pkg/utils/type-coverter.go new file mode 100644 index 0000000..3879896 --- /dev/null +++ b/pkg/utils/type-coverter.go @@ -0,0 +1,16 @@ +package utils + +import "encoding/json" + +func TypeConverter[R any](data any) (*R, error) { + var result R + b, err := json.Marshal(&data) + if err != nil { + return nil, err + } + err = json.Unmarshal(b, &result) + if err != nil { + return nil, err + } + return &result, err +} diff --git a/test.db b/test.db deleted file mode 100644 index 484e84e..0000000 Binary files a/test.db and /dev/null differ