Added sub commands for doing admin stuff
This commit is contained in:
parent
329de2f953
commit
4a6b79db17
18 changed files with 531 additions and 185 deletions
47
services/services.go
Normal file
47
services/services.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"lmika.dev/lmika/weiro/config"
|
||||
"lmika.dev/lmika/weiro/providers/db"
|
||||
"lmika.dev/lmika/weiro/services/auth"
|
||||
"lmika.dev/lmika/weiro/services/posts"
|
||||
"lmika.dev/lmika/weiro/services/publisher"
|
||||
"lmika.dev/lmika/weiro/services/sites"
|
||||
)
|
||||
|
||||
type Services struct {
|
||||
DB *db.Provider
|
||||
Auth *auth.Service
|
||||
Publisher *publisher.Publisher
|
||||
PublisherQueue *publisher.Queue
|
||||
Posts *posts.Service
|
||||
Sites *sites.Service
|
||||
}
|
||||
|
||||
func New(cfg config.Config) (*Services, error) {
|
||||
dbp, err := db.New(filepath.Join(cfg.DataDir, "weiro.db"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
authSvc := auth.New(dbp)
|
||||
publisherSvc := publisher.New(dbp)
|
||||
publisherQueue := publisher.NewQueue(publisherSvc)
|
||||
postService := posts.New(dbp, publisherQueue)
|
||||
siteService := sites.New(dbp)
|
||||
|
||||
return &Services{
|
||||
DB: dbp,
|
||||
Auth: authSvc,
|
||||
Publisher: publisherSvc,
|
||||
PublisherQueue: publisherQueue,
|
||||
Posts: postService,
|
||||
Sites: siteService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Services) Close() error {
|
||||
return s.DB.Close()
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"emperror.dev/errors"
|
||||
"github.com/go-ozzo/ozzo-validation/v4"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db"
|
||||
)
|
||||
|
|
@ -90,6 +91,7 @@ func (s *Service) FirstRun(ctx context.Context, req FirstRunRequest) (newUser mo
|
|||
target := models.SitePublishTarget{
|
||||
SiteID: newSite.ID,
|
||||
Enabled: true,
|
||||
GUID: models.NewNanoID(),
|
||||
BaseURL: req.SiteURL,
|
||||
TargetType: "netlify",
|
||||
TargetRef: req.NetlifySiteID,
|
||||
|
|
@ -102,3 +104,25 @@ func (s *Service) FirstRun(ctx context.Context, req FirstRunRequest) (newUser mo
|
|||
|
||||
return newUser, newSite, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetSiteByID(ctx context.Context, siteID int64) (models.Site, error) {
|
||||
user, ok := models.GetUser(ctx)
|
||||
if !ok {
|
||||
return models.Site{}, models.UserRequiredError
|
||||
}
|
||||
|
||||
site, err := s.db.SelectSiteByID(ctx, siteID)
|
||||
if err != nil {
|
||||
return models.Site{}, err
|
||||
}
|
||||
|
||||
if site.OwnerID != user.ID {
|
||||
return models.Site{}, fiber.ErrForbidden
|
||||
}
|
||||
|
||||
return site, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListAllSitesWithOwners(ctx context.Context) ([]db.SiteWithOwner, error) {
|
||||
return s.db.SelectAllSitesWithOwners(ctx)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue