2025-01-07 18:42:41 -03:00
|
|
|
// Copyright 2025-present Gustavo "Guz" L. de Mello
|
|
|
|
|
// Copyright 2025-present The Lored.dev Contributors
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
package blogo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"io"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
2025-01-09 10:16:59 -03:00
|
|
|
"strings"
|
2025-01-06 20:05:34 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
|
Logger *slog.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Blogo struct {
|
2025-01-09 10:16:59 -03:00
|
|
|
files fs.FS
|
|
|
|
|
|
|
|
|
|
sources []SourcerPlugin
|
2025-01-09 10:22:00 -03:00
|
|
|
renderers []RendererPlugin
|
2025-01-06 20:05:34 -03:00
|
|
|
|
|
|
|
|
log *slog.Logger
|
|
|
|
|
panic bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(opts ...Options) *Blogo {
|
|
|
|
|
opt := Options{}
|
|
|
|
|
if len(opts) > 0 {
|
|
|
|
|
opt = opts[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opt.Logger == nil {
|
|
|
|
|
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
|
|
|
|
} else {
|
|
|
|
|
opt.Logger = opt.Logger.WithGroup("blogo")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Blogo{
|
|
|
|
|
files: nil,
|
|
|
|
|
sources: []SourcerPlugin{},
|
|
|
|
|
log: opt.Logger,
|
|
|
|
|
panic: true, // TODO
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 10:16:59 -03:00
|
|
|
func (b *Blogo) Use(p Plugin) {
|
|
|
|
|
log := b.log.With(slog.String("plugin", p.Name()))
|
|
|
|
|
|
|
|
|
|
if p, ok := p.(SourcerPlugin); ok {
|
|
|
|
|
log.Debug("Added plugin", slog.String("type", "SourcerPlugin"))
|
|
|
|
|
b.sources = append(b.sources, p)
|
|
|
|
|
}
|
2025-01-09 10:22:00 -03:00
|
|
|
if p, ok := p.(RendererPlugin); ok {
|
|
|
|
|
log.Debug("Added plugin", slog.String("type", "RenderPlugin"))
|
|
|
|
|
b.renderers = append(b.renderers, p)
|
|
|
|
|
}
|
2025-01-09 10:16:59 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
func (b *Blogo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2025-01-10 11:36:40 -03:00
|
|
|
log := b.log.With(slog.String("step", "SERVE"), slog.String("path", r.URL.Path))
|
2025-01-09 09:53:11 -03:00
|
|
|
|
|
|
|
|
log.Debug("Serving endpoint")
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
if b.files == nil {
|
2025-01-09 09:53:11 -03:00
|
|
|
log.Debug("No files in Blogo engine, initializing files")
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
err := b.Init()
|
|
|
|
|
if err != nil {
|
2025-01-09 09:53:11 -03:00
|
|
|
log.Error("Failed to initialize files")
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
err = errors.Join(errors.New("failed to initialize Blogo engine on first request"), err)
|
|
|
|
|
if b.panic {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 16:48:27 -03:00
|
|
|
path := strings.Trim(r.URL.Path, "/")
|
2025-01-09 11:06:31 -03:00
|
|
|
if path == "" || path == "/" {
|
|
|
|
|
path = "."
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 16:48:27 -03:00
|
|
|
f, err := b.files.Open(path)
|
2025-01-09 09:53:11 -03:00
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2025-01-09 09:53:11 -03:00
|
|
|
log.Error("Failed to read file", slog.String("error", err.Error()))
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
} else if err != nil {
|
2025-01-09 09:53:11 -03:00
|
|
|
log.Error("Failed to read file", slog.String("error", err.Error()))
|
|
|
|
|
|
2025-01-06 20:05:34 -03:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2025-01-09 09:53:11 -03:00
|
|
|
log.Debug("Writing response file")
|
|
|
|
|
|
2025-01-09 10:22:00 -03:00
|
|
|
log.Debug("Rendering file")
|
2025-01-06 20:05:34 -03:00
|
|
|
|
2025-01-09 10:22:00 -03:00
|
|
|
// TODO: Support for multiple renderers (conditional renderers)
|
|
|
|
|
err = b.renderers[0].Render(f, w)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("Failed to render file", slog.String("error", err.Error()))
|
2025-01-06 20:05:34 -03:00
|
|
|
|
2025-01-09 10:22:00 -03:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
|
return
|
2025-01-06 20:05:34 -03:00
|
|
|
}
|
2025-01-09 09:53:11 -03:00
|
|
|
|
2025-01-09 10:22:00 -03:00
|
|
|
log.Debug("Finished responding file")
|
2025-01-06 20:05:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Blogo) Init() error {
|
2025-01-10 11:36:40 -03:00
|
|
|
log := b.log.With(slog.String("step", "INITIALIZATION"))
|
|
|
|
|
log.Debug("Initializing blogo")
|
2025-01-06 20:05:34 -03:00
|
|
|
|
2025-01-09 10:16:59 -03:00
|
|
|
if len(b.sources) == 0 {
|
2025-01-10 11:36:40 -03:00
|
|
|
log.Debug("No SourcerPlugin found, using default one")
|
2025-01-09 10:16:59 -03:00
|
|
|
b.Use(&defaultSourcer{})
|
|
|
|
|
}
|
2025-01-09 10:22:00 -03:00
|
|
|
if len(b.renderers) == 0 {
|
2025-01-10 11:36:40 -03:00
|
|
|
log.Debug("No RendererPlugin found, using default one")
|
2025-01-09 10:22:00 -03:00
|
|
|
b.Use(&defaultRenderer{})
|
|
|
|
|
}
|
2025-01-09 10:16:59 -03:00
|
|
|
|
|
|
|
|
fs, err := b.sources[0].Source() // TOOD: Support for multiple sources (via another plugin or built-in, with prefixes or not)
|
2025-01-06 20:05:34 -03:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Join(errors.New("failed to source files"), err)
|
|
|
|
|
}
|
|
|
|
|
b.files = fs
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|