mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 21:35:34 +03:00
web ui: index and basic details page, api refactoring
This commit is contained in:
@@ -2,6 +2,7 @@ package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
|
||||
"github.com/derfenix/webarchive/api/openapi"
|
||||
"github.com/derfenix/webarchive/entity"
|
||||
@@ -22,6 +23,11 @@ func PageToRestWithResults(page *entity.Page) openapi.PageWithResults {
|
||||
return res
|
||||
}(),
|
||||
Status: StatusToRest(page.Status),
|
||||
Meta: openapi.PageWithResultsMeta{
|
||||
Title: html.EscapeString(page.Meta.Title),
|
||||
Description: html.EscapeString(page.Meta.Description),
|
||||
Error: openapi.NewOptString(page.Meta.Error),
|
||||
},
|
||||
Results: func() []openapi.Result {
|
||||
results := make([]openapi.Result, len(page.Results.Results()))
|
||||
|
||||
@@ -65,6 +71,11 @@ func PageToRest(page *entity.Page) openapi.Page {
|
||||
ID: page.ID,
|
||||
URL: page.URL,
|
||||
Created: page.Created,
|
||||
Meta: openapi.PageMeta{
|
||||
Title: html.EscapeString(page.Meta.Title),
|
||||
Description: html.EscapeString(page.Meta.Description),
|
||||
Error: openapi.NewOptString(page.Meta.Error),
|
||||
},
|
||||
Formats: func() []openapi.Format {
|
||||
res := make([]openapi.Format, len(page.Formats))
|
||||
|
||||
|
||||
@@ -20,14 +20,19 @@ type Pages interface {
|
||||
GetFile(ctx context.Context, pageID, fileID uuid.UUID) (*entity.File, error)
|
||||
}
|
||||
|
||||
func NewService(sites Pages, ch chan *entity.Page) *Service {
|
||||
return &Service{pages: sites, ch: ch}
|
||||
func NewService(pages Pages, ch chan *entity.Page, processor entity.Processor) *Service {
|
||||
return &Service{
|
||||
pages: pages,
|
||||
ch: ch,
|
||||
processor: processor,
|
||||
}
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
openapi.UnimplementedHandler
|
||||
pages Pages
|
||||
ch chan *entity.Page
|
||||
pages Pages
|
||||
ch chan *entity.Page
|
||||
processor entity.Processor
|
||||
}
|
||||
|
||||
func (s *Service) GetPage(ctx context.Context, params openapi.GetPageParams) (openapi.GetPageRes, error) {
|
||||
@@ -78,6 +83,13 @@ func (s *Service) AddPage(ctx context.Context, req openapi.OptAddPageReq, params
|
||||
page := entity.NewPage(url, description, domainFormats...)
|
||||
page.Status = entity.StatusProcessing
|
||||
|
||||
meta, err := s.processor.GetMeta(ctx, page.URL)
|
||||
if err != nil {
|
||||
page.Meta.Error = err.Error()
|
||||
} else {
|
||||
page.Meta = meta
|
||||
}
|
||||
|
||||
if err := s.pages.Save(ctx, page); err != nil {
|
||||
return nil, fmt.Errorf("save page: %w", err)
|
||||
}
|
||||
|
||||
@@ -10,15 +10,19 @@ import (
|
||||
)
|
||||
|
||||
func NewUI(cfg config.UI) *UI {
|
||||
return &UI{prefix: cfg.Prefix}
|
||||
return &UI{
|
||||
prefix: cfg.Prefix,
|
||||
theme: cfg.Theme,
|
||||
}
|
||||
}
|
||||
|
||||
type UI struct {
|
||||
prefix string
|
||||
theme string
|
||||
}
|
||||
|
||||
func (u *UI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
serveRoot, err := fs.Sub(ui.StaticFiles, "static")
|
||||
serveRoot, err := fs.Sub(ui.StaticFiles, u.theme)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
@@ -27,12 +31,11 @@ func (u *UI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, u.prefix) {
|
||||
r.URL.Path = "/" + strings.TrimPrefix(r.URL.Path, u.prefix)
|
||||
}
|
||||
if !strings.HasPrefix(r.URL.Path, "/static") {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
|
||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static")
|
||||
|
||||
http.FileServer(http.FS(serveRoot)).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (u *UI) IsUIRequest(r *http.Request) bool {
|
||||
return r.URL.Path == u.prefix || strings.HasPrefix(r.URL.Path, "/static/")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user