add opts config for engine and reorganized files
This commit is contained in:
parent
5f0dee29e2
commit
dc48e215ec
128
ron.go
128
ron.go
@ -3,21 +3,86 @@ package ron
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Data map[string]any
|
||||
type (
|
||||
EngineOptions func(*Engine)
|
||||
|
||||
type Context struct {
|
||||
C context.Context
|
||||
W http.ResponseWriter
|
||||
R *http.Request
|
||||
E *Engine
|
||||
Data map[string]any
|
||||
|
||||
Context struct {
|
||||
C context.Context
|
||||
W http.ResponseWriter
|
||||
R *http.Request
|
||||
E *Engine
|
||||
}
|
||||
|
||||
Engine struct {
|
||||
mux *http.ServeMux
|
||||
LogLevel slog.Level
|
||||
Renderer *Render
|
||||
}
|
||||
)
|
||||
|
||||
func DefaultEngine() *Engine {
|
||||
return &Engine{
|
||||
mux: http.NewServeMux(),
|
||||
LogLevel: slog.LevelInfo,
|
||||
}
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
mux *http.ServeMux
|
||||
Renderer *Render
|
||||
func New(opts ...EngineOptions) *Engine {
|
||||
config := DefaultEngine()
|
||||
return config.apply(opts...)
|
||||
|
||||
}
|
||||
|
||||
func (e *Engine) apply(opts ...EngineOptions) *Engine {
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(e)
|
||||
}
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
e.handleRequest(w, r)
|
||||
}
|
||||
|
||||
func (e *Engine) Run(addr string) error {
|
||||
return http.ListenAndServe(addr, e)
|
||||
}
|
||||
|
||||
func (e *Engine) handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
e.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (e *Engine) GET(path string, handler func(*Context)) {
|
||||
e.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
handler(&Context{W: w, R: r, E: e})
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Engine) POST(path string, handler func(*Context)) {
|
||||
e.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
handler(&Context{W: w, R: r, E: e})
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Context) JSON(code int, data any) {
|
||||
@ -40,41 +105,18 @@ func (c *Context) HTML(code int, name string, data Data) {
|
||||
}
|
||||
}
|
||||
|
||||
func New() *Engine {
|
||||
engine := &Engine{
|
||||
mux: http.NewServeMux(),
|
||||
func newLogger(level slog.Level) {
|
||||
now := time.Now().Format("2006-01-02")
|
||||
if _, err := os.Stat("logs"); os.IsNotExist(err) {
|
||||
os.Mkdir("logs", 0755)
|
||||
}
|
||||
return engine
|
||||
}
|
||||
f, _ := os.OpenFile(fmt.Sprintf("logs/log%s.log", now), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
mw := io.MultiWriter(os.Stdout, f)
|
||||
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
engine.handleRequest(w, r)
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(mw, &slog.HandlerOptions{
|
||||
AddSource: true,
|
||||
Level: level,
|
||||
}))
|
||||
|
||||
func (engine *Engine) Run(addr string) error {
|
||||
return http.ListenAndServe(addr, engine)
|
||||
}
|
||||
|
||||
func (engine *Engine) handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
engine.mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (engine *Engine) GET(path string, handler func(*Context)) {
|
||||
engine.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
handler(&Context{W: w, R: r, E: engine})
|
||||
})
|
||||
}
|
||||
|
||||
func (engine *Engine) POST(path string, handler func(*Context)) {
|
||||
engine.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
handler(&Context{W: w, R: r, E: engine})
|
||||
})
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
|
||||
@ -17,8 +17,8 @@ type (
|
||||
Data Data
|
||||
}
|
||||
|
||||
OptionFunc func(*Render)
|
||||
Render struct {
|
||||
RenderOptions func(*Render)
|
||||
Render struct {
|
||||
EnableCache bool
|
||||
TemplatesPath string
|
||||
Functions template.FuncMap
|
||||
@ -35,12 +35,12 @@ func DefaultHTMLRender() *Render {
|
||||
}
|
||||
}
|
||||
|
||||
func NewHTMLRender(opts ...OptionFunc) *Render {
|
||||
func NewHTMLRender(opts ...RenderOptions) *Render {
|
||||
config := DefaultHTMLRender()
|
||||
return config.apply(opts...)
|
||||
}
|
||||
|
||||
func (re *Render) apply(opts ...OptionFunc) *Render {
|
||||
func (re *Render) apply(opts ...RenderOptions) *Render {
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(re)
|
||||
|
||||
@ -33,9 +33,9 @@ func Test_HTMLRender(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg OptionFunc
|
||||
arg RenderOptions
|
||||
}{
|
||||
{"Empty OptionFunc", OptionFunc(func(r *Render) {})},
|
||||
{"Empty OptionFunc", RenderOptions(func(r *Render) {})},
|
||||
{"Nil", nil},
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user