mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 01:37:52 +02:00
create frontend handler (#129)
This commit is contained in:
parent
81e5f7e469
commit
c9db95fe03
@ -31,6 +31,7 @@ func Run(inCfg *Config) error {
|
|||||||
api.AccountLoginHandler = account.LoginHandlerFunc(loginHandler)
|
api.AccountLoginHandler = account.LoginHandlerFunc(loginHandler)
|
||||||
api.AccountRegisterHandler = newRegisterHandler()
|
api.AccountRegisterHandler = newRegisterHandler()
|
||||||
api.AccountVerifyHandler = newVerifyHandler()
|
api.AccountVerifyHandler = newVerifyHandler()
|
||||||
|
api.AdminCreateFrontendHandler = newCreateFrontendHandler()
|
||||||
api.EnvironmentEnableHandler = newEnableHandler()
|
api.EnvironmentEnableHandler = newEnableHandler()
|
||||||
api.EnvironmentDisableHandler = newDisableHandler()
|
api.EnvironmentDisableHandler = newDisableHandler()
|
||||||
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
|
api.MetadataOverviewHandler = metadata.OverviewHandlerFunc(overviewHandler)
|
||||||
|
56
controller/create_frontend.go
Normal file
56
controller/create_frontend.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-openapi/runtime/middleware"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/admin"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type createFrontendHandler struct{}
|
||||||
|
|
||||||
|
func newCreateFrontendHandler() *createFrontendHandler {
|
||||||
|
return &createFrontendHandler{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, principal *rest_model_zrok.Principal) middleware.Responder {
|
||||||
|
if !principal.Admin {
|
||||||
|
logrus.Errorf("invalid admin principal")
|
||||||
|
return admin.NewCreateFrontendUnauthorized()
|
||||||
|
}
|
||||||
|
|
||||||
|
tx, err := str.Begin()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("error starting transaction: %v", err)
|
||||||
|
return admin.NewCreateFrontendInternalServerError()
|
||||||
|
}
|
||||||
|
defer func() { _ = tx.Rollback() }()
|
||||||
|
|
||||||
|
feToken, err := createToken()
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("error creating frontend token: %v", err)
|
||||||
|
return admin.NewCreateFrontendInternalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
fe := &store.Frontend{
|
||||||
|
Token: feToken,
|
||||||
|
ZId: params.Body.ZID,
|
||||||
|
PublicName: ¶ms.Body.PublicName,
|
||||||
|
UrlTemplate: ¶ms.Body.URLTemplate,
|
||||||
|
Reserved: true,
|
||||||
|
}
|
||||||
|
if _, err := str.CreateGlobalFrontend(fe, tx); err != nil {
|
||||||
|
logrus.Errorf("error creating frontend record: %v", err)
|
||||||
|
return admin.NewCreateFrontendInternalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
logrus.Errorf("error committing frontend record: %v", err)
|
||||||
|
return admin.NewCreateFrontendInternalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Infof("created global frontend '%v' with public name '%v'", fe.Token, fe.PublicName)
|
||||||
|
|
||||||
|
return admin.NewCreateFrontendCreated().WithPayload(&rest_model_zrok.CreateFrontendResponse{Token: feToken})
|
||||||
|
}
|
@ -90,9 +90,9 @@ func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_
|
|||||||
}, tx)
|
}, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error creating service record: %v", err)
|
logrus.Errorf("error creating service record: %v", err)
|
||||||
_ = tx.Rollback()
|
|
||||||
return service.NewShareInternalServerError()
|
return service.NewShareInternalServerError()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Commit(); err != nil {
|
if err := tx.Commit(); err != nil {
|
||||||
logrus.Errorf("error committing service record: %v", err)
|
logrus.Errorf("error committing service record: %v", err)
|
||||||
return service.NewShareInternalServerError()
|
return service.NewShareInternalServerError()
|
||||||
|
@ -16,17 +16,29 @@ type Frontend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (str *Store) CreateFrontend(envId int, f *Frontend, tx *sqlx.Tx) (int, error) {
|
func (str *Store) CreateFrontend(envId int, f *Frontend, tx *sqlx.Tx) (int, error) {
|
||||||
stmt, err := tx.Prepare("insert into frontends (environment_id, token, z_id, public_name, reserved) values ($1, $2, $3, $4, $5) returning id")
|
stmt, err := tx.Prepare("insert into frontends (environment_id, token, z_id, public_name, url_template, reserved) values ($1, $2, $3, $4, $5, $6) returning id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(err, "error preparing frontends insert statement")
|
return 0, errors.Wrap(err, "error preparing frontends insert statement")
|
||||||
}
|
}
|
||||||
var id int
|
var id int
|
||||||
if err := stmt.QueryRow(envId, f.Token, f.ZId, f.PublicName, f.Reserved).Scan(&id); err != nil {
|
if err := stmt.QueryRow(envId, f.Token, f.ZId, f.PublicName, f.UrlTemplate, f.Reserved).Scan(&id); err != nil {
|
||||||
return 0, errors.Wrap(err, "error executing frontends insert statement")
|
return 0, errors.Wrap(err, "error executing frontends insert statement")
|
||||||
}
|
}
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (str *Store) CreateGlobalFrontend(f *Frontend, tx *sqlx.Tx) (int, error) {
|
||||||
|
stmt, err := tx.Prepare("insert into frontends (token, z_id, public_name, url_template, reserved) values ($1, $2, $3, $4, $5) returning id")
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error preparing global frontends insert statement")
|
||||||
|
}
|
||||||
|
var id int
|
||||||
|
if err := stmt.QueryRow(f.Token, f.ZId, f.PublicName, f.UrlTemplate, f.Reserved).Scan(&id); err != nil {
|
||||||
|
return 0, errors.Wrap(err, "error executing global frontends insert statement")
|
||||||
|
}
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (str *Store) GetFrontend(id int, tx *sqlx.Tx) (*Frontend, error) {
|
func (str *Store) GetFrontend(id int, tx *sqlx.Tx) (*Frontend, error) {
|
||||||
i := &Frontend{}
|
i := &Frontend{}
|
||||||
if err := tx.QueryRowx("select * from frontends where id = $1", id).StructScan(i); err != nil {
|
if err := tx.QueryRowx("select * from frontends where id = $1", id).StructScan(i); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user