updated store.Services and friends (#103)

This commit is contained in:
Michael Quigley 2022-11-22 11:14:58 -05:00
parent cbb9ec833f
commit 6184e0e450
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 30 additions and 21 deletions

View File

@ -45,10 +45,18 @@ func overviewHandler(_ metadata.OverviewParams, principal *rest_model_zrok.Princ
return metadata.NewOverviewInternalServerError() return metadata.NewOverviewInternalServerError()
} }
for _, svc := range svcs { for _, svc := range svcs {
fe := ""
if svc.FrontendEndpoint != nil {
fe = *svc.FrontendEndpoint
}
be := ""
if svc.BackendProxyEndpoint != nil {
be = *svc.BackendProxyEndpoint
}
es.Services = append(es.Services, &rest_model_zrok.Service{ es.Services = append(es.Services, &rest_model_zrok.Service{
CreatedAt: svc.CreatedAt.UnixMilli(), CreatedAt: svc.CreatedAt.UnixMilli(),
Frontend: svc.Frontend, Frontend: fe,
Backend: svc.Backend, Backend: be,
UpdatedAt: svc.UpdatedAt.UnixMilli(), UpdatedAt: svc.UpdatedAt.UnixMilli(),
ZID: svc.ZId, ZID: svc.ZId,
Name: svc.Name, Name: svc.Name,

View File

@ -95,10 +95,10 @@ func (h *sharePublicHandler) Handle(params service.ShareParams, principal *rest_
sid, err := str.CreateService(envId, &store.Service{ sid, err := str.CreateService(envId, &store.Service{
ZId: svcZId, ZId: svcZId,
Name: svcName, Name: svcName,
Frontend: frontendUrl,
Backend: params.Body.BackendProxyEndpoint,
ShareMode: "public", ShareMode: "public",
BackendMode: "proxy", BackendMode: "proxy",
FrontendEndpoint: &frontendUrl,
BackendProxyEndpoint: &params.Body.BackendProxyEndpoint,
}, tx) }, tx)
if err != nil { if err != nil {
logrus.Errorf("error creating service record: %v", err) logrus.Errorf("error creating service record: %v", err)

View File

@ -10,19 +10,20 @@ type Service struct {
EnvironmentId int EnvironmentId int
ZId string ZId string
Name string Name string
Frontend string
Backend string
ShareMode string ShareMode string
BackendMode string BackendMode string
FrontendSelection *string
FrontendEndpoint *string
BackendProxyEndpoint *string
} }
func (self *Store) CreateService(envId int, svc *Service, tx *sqlx.Tx) (int, error) { func (self *Store) CreateService(envId int, svc *Service, tx *sqlx.Tx) (int, error) {
stmt, err := tx.Prepare("insert into services (environment_id, z_id, name, frontend, backend, share_mode, backend_mode) values ($1, $2, $3, $4, $5, $6, $7) returning id") stmt, err := tx.Prepare("insert into services (environment_id, z_id, name, share_mode, backend_mode, frontend_selection, frontend_endpoint, backend_proxy_endpoint) values ($1, $2, $3, $4, $5, $6, $7, $8) returning id")
if err != nil { if err != nil {
return 0, errors.Wrap(err, "error preparing services insert statement") return 0, errors.Wrap(err, "error preparing services insert statement")
} }
var id int var id int
if err := stmt.QueryRow(envId, svc.ZId, svc.Name, svc.Frontend, svc.Backend, svc.ShareMode, svc.BackendMode).Scan(&id); err != nil { if err := stmt.QueryRow(envId, svc.ZId, svc.Name, svc.ShareMode, svc.BackendMode, svc.FrontendSelection, svc.FrontendEndpoint, svc.BackendProxyEndpoint).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing services insert statement") return 0, errors.Wrap(err, "error executing services insert statement")
} }
return id, nil return id, nil
@ -69,12 +70,12 @@ func (self *Store) FindServicesForEnvironment(envId int, tx *sqlx.Tx) ([]*Servic
} }
func (self *Store) UpdateService(svc *Service, tx *sqlx.Tx) error { func (self *Store) UpdateService(svc *Service, tx *sqlx.Tx) error {
sql := "update services set z_id = $1, name = $2, frontend = $3, backend = $4, share_mode = $5, backend_mode = $6, updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now') where id = $7" sql := "update services set z_id = $1, name = $2, share_mode = $3, backend_mode = $4, frontend_selection = $5, frontend_endpoint = $6, backend_proxy_endpoint = $7, updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now') where id = $8"
stmt, err := tx.Prepare(sql) stmt, err := tx.Prepare(sql)
if err != nil { if err != nil {
return errors.Wrap(err, "error preparing services update statement") return errors.Wrap(err, "error preparing services update statement")
} }
_, err = stmt.Exec(svc.ZId, svc.Name, svc.Frontend, svc.Backend, svc.ShareMode, svc.BackendMode, svc.Id) _, err = stmt.Exec(svc.ZId, svc.Name, svc.ShareMode, svc.BackendMode, svc.FrontendSelection, svc.FrontendEndpoint, svc.BackendProxyEndpoint, svc.Id)
if err != nil { if err != nil {
return errors.Wrap(err, "error executing services update statement") return errors.Wrap(err, "error executing services update statement")
} }

View File

@ -21,8 +21,8 @@ create table services (
constraint chk_z_id check (z_id <> ''), constraint chk_z_id check (z_id <> ''),
constraint chk_name check (name <> ''), constraint chk_name check (name <> ''),
constraint chk_share_mode check (share_mode == 'public' || share_mode == 'private'), constraint chk_share_mode check (share_mode == 'public' or share_mode == 'private'),
constraint chk_backend_mode check (backend_mode == 'proxy' || backend_mode == 'web' || backend_mode == 'dav') constraint chk_backend_mode check (backend_mode == 'proxy' or backend_mode == 'web' or backend_mode == 'dav')
); );
insert into services (id, environment_id, z_id, name, share_mode, backend_mode, frontend_selection, frontend_endpoint, backend_proxy_endpoint, created_at, updated_at) insert into services (id, environment_id, z_id, name, share_mode, backend_mode, frontend_selection, frontend_endpoint, backend_proxy_endpoint, created_at, updated_at)