From 6007918367790537cef1f397ab2d86d445f8981f Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Mon, 30 Jan 2023 23:24:22 +0100 Subject: [PATCH] Add more app methods accessible by apps, add requests library for plugins --- cache.go | 3 + docs/plugins.md | 1 + pkgs/plugintypes/goblog.go | 5 + .../github_com-carlmjohnson-requests.go | 103 ++++++++++++++++++ .../go_goblog_app-app-pkgs-plugintypes.go | 14 ++- pkgs/yaegiwrappers/wrappers.go | 1 + plugins.go | 9 ++ 7 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 pkgs/yaegiwrappers/github_com-carlmjohnson-requests.go diff --git a/cache.go b/cache.go index 98d1259..00b275f 100644 --- a/cache.go +++ b/cache.go @@ -234,6 +234,9 @@ func (c *cache) getCache(key string, next http.Handler, r *http.Request) *cacheI } func (c *cache) purge() { + if c == nil { + return + } c.c.Clear() } diff --git a/docs/plugins.md b/docs/plugins.md index 90c9d36..253f599 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -85,6 +85,7 @@ GoBlog modules: Third-party modules - `github.com/PuerkitoBio/goquery` (Can be used to *manipulate* HTML in a jquery-like way) +- `github.com/carlmjohnson/requests` (Can be used to do HTTP requests more easily) ## Plugins diff --git a/pkgs/plugintypes/goblog.go b/pkgs/plugintypes/goblog.go index ef97fec..7fcc6c4 100644 --- a/pkgs/plugintypes/goblog.go +++ b/pkgs/plugintypes/goblog.go @@ -3,6 +3,7 @@ package plugintypes import ( "context" "database/sql" + "net/http" ) // App is used to access GoBlog's app instance. @@ -11,6 +12,10 @@ type App interface { GetDatabase() Database // Get a post from the database or an error when there is no post for the given path GetPost(path string) (Post, error) + // Purge the rendering cache + PurgeCache() + // Get the HTTP client used by GoBlog + GetHTTPClient() *http.Client } // Database is used to provide access to GoBlog's database. diff --git a/pkgs/yaegiwrappers/github_com-carlmjohnson-requests.go b/pkgs/yaegiwrappers/github_com-carlmjohnson-requests.go new file mode 100644 index 0000000..5b544f0 --- /dev/null +++ b/pkgs/yaegiwrappers/github_com-carlmjohnson-requests.go @@ -0,0 +1,103 @@ +// Code generated by 'yaegi extract github.com/carlmjohnson/requests'. DO NOT EDIT. + +// MIT License +// +// Copyright (c) 2020 - 2023 Jan-Lukas Else +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package yaegiwrappers + +import ( + "github.com/carlmjohnson/requests" + "net/http" + "reflect" +) + +func init() { + Symbols["github.com/carlmjohnson/requests/requests"] = map[string]reflect.Value{ + // function, constant and variable definitions + "BodyBytes": reflect.ValueOf(requests.BodyBytes), + "BodyFile": reflect.ValueOf(requests.BodyFile), + "BodyForm": reflect.ValueOf(requests.BodyForm), + "BodyJSON": reflect.ValueOf(requests.BodyJSON), + "BodyReader": reflect.ValueOf(requests.BodyReader), + "BodyWriter": reflect.ValueOf(requests.BodyWriter), + "Caching": reflect.ValueOf(requests.Caching), + "ChainHandlers": reflect.ValueOf(requests.ChainHandlers), + "CheckContentType": reflect.ValueOf(requests.CheckContentType), + "CheckPeek": reflect.ValueOf(requests.CheckPeek), + "CheckStatus": reflect.ValueOf(requests.CheckStatus), + "CopyHeaders": reflect.ValueOf(requests.CopyHeaders), + "DefaultValidator": reflect.ValueOf(&requests.DefaultValidator).Elem(), + "ErrHandler": reflect.ValueOf(requests.ErrHandler), + "ErrInvalidHandled": reflect.ValueOf(&requests.ErrInvalidHandled).Elem(), + "ErrRequest": reflect.ValueOf(requests.ErrRequest), + "ErrTransport": reflect.ValueOf(requests.ErrTransport), + "ErrURL": reflect.ValueOf(requests.ErrURL), + "ErrValidator": reflect.ValueOf(requests.ErrValidator), + "ErrorJSON": reflect.ValueOf(requests.ErrorJSON), + "GzipConfig": reflect.ValueOf(requests.GzipConfig), + "HasStatusErr": reflect.ValueOf(requests.HasStatusErr), + "MaxFollow": reflect.ValueOf(requests.MaxFollow), + "NewCookieJar": reflect.ValueOf(requests.NewCookieJar), + "NoFollow": reflect.ValueOf(&requests.NoFollow).Elem(), + "PermitURLTransport": reflect.ValueOf(requests.PermitURLTransport), + "Record": reflect.ValueOf(requests.Record), + "Replay": reflect.ValueOf(requests.Replay), + "ReplayFS": reflect.ValueOf(requests.ReplayFS), + "ReplayString": reflect.ValueOf(requests.ReplayString), + "ToBufioReader": reflect.ValueOf(requests.ToBufioReader), + "ToBufioScanner": reflect.ValueOf(requests.ToBufioScanner), + "ToBytesBuffer": reflect.ValueOf(requests.ToBytesBuffer), + "ToFile": reflect.ValueOf(requests.ToFile), + "ToHTML": reflect.ValueOf(requests.ToHTML), + "ToHeaders": reflect.ValueOf(&requests.ToHeaders).Elem(), + "ToJSON": reflect.ValueOf(requests.ToJSON), + "ToString": reflect.ValueOf(requests.ToString), + "ToWriter": reflect.ValueOf(requests.ToWriter), + "URL": reflect.ValueOf(requests.URL), + "UserAgentTransport": reflect.ValueOf(requests.UserAgentTransport), + "ValidatorHandler": reflect.ValueOf(requests.ValidatorHandler), + + // type definitions + "BodyGetter": reflect.ValueOf((*requests.BodyGetter)(nil)), + "Builder": reflect.ValueOf((*requests.Builder)(nil)), + "CheckRedirectPolicy": reflect.ValueOf((*requests.CheckRedirectPolicy)(nil)), + "Config": reflect.ValueOf((*requests.Config)(nil)), + "ErrorKind": reflect.ValueOf((*requests.ErrorKind)(nil)), + "ResponseError": reflect.ValueOf((*requests.ResponseError)(nil)), + "ResponseHandler": reflect.ValueOf((*requests.ResponseHandler)(nil)), + "RoundTripFunc": reflect.ValueOf((*requests.RoundTripFunc)(nil)), + "Transport": reflect.ValueOf((*requests.Transport)(nil)), + + // interface wrapper definitions + "_Transport": reflect.ValueOf((*_github_com_carlmjohnson_requests_Transport)(nil)), + } +} + +// _github_com_carlmjohnson_requests_Transport is an interface wrapper for Transport type +type _github_com_carlmjohnson_requests_Transport struct { + IValue interface{} + WRoundTrip func(a0 *http.Request) (*http.Response, error) +} + +func (W _github_com_carlmjohnson_requests_Transport) RoundTrip(a0 *http.Request) (*http.Response, error) { + return W.WRoundTrip(a0) +} diff --git a/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go b/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go index 928c5e1..d48c684 100644 --- a/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go +++ b/pkgs/yaegiwrappers/go_goblog_app-app-pkgs-plugintypes.go @@ -61,17 +61,25 @@ func init() { // _go_goblog_app_app_pkgs_plugintypes_App is an interface wrapper for App type type _go_goblog_app_app_pkgs_plugintypes_App struct { - IValue interface{} - WGetDatabase func() plugintypes.Database - WGetPost func(path string) (plugintypes.Post, error) + IValue interface{} + WGetDatabase func() plugintypes.Database + WGetHTTPClient func() *http.Client + WGetPost func(path string) (plugintypes.Post, error) + WPurgeCache func() } func (W _go_goblog_app_app_pkgs_plugintypes_App) GetDatabase() plugintypes.Database { return W.WGetDatabase() } +func (W _go_goblog_app_app_pkgs_plugintypes_App) GetHTTPClient() *http.Client { + return W.WGetHTTPClient() +} func (W _go_goblog_app_app_pkgs_plugintypes_App) GetPost(path string) (plugintypes.Post, error) { return W.WGetPost(path) } +func (W _go_goblog_app_app_pkgs_plugintypes_App) PurgeCache() { + W.WPurgeCache() +} // _go_goblog_app_app_pkgs_plugintypes_Database is an interface wrapper for Database type type _go_goblog_app_app_pkgs_plugintypes_Database struct { diff --git a/pkgs/yaegiwrappers/wrappers.go b/pkgs/yaegiwrappers/wrappers.go index 861c0a2..6dd68f2 100644 --- a/pkgs/yaegiwrappers/wrappers.go +++ b/pkgs/yaegiwrappers/wrappers.go @@ -15,3 +15,4 @@ var ( // Dependencies //go:generate yaegi extract -license ../../LICENSE -name yaegiwrappers github.com/PuerkitoBio/goquery +//go:generate yaegi extract -license ../../LICENSE -name yaegiwrappers github.com/carlmjohnson/requests diff --git a/plugins.go b/plugins.go index 070d08f..f451a7e 100644 --- a/plugins.go +++ b/plugins.go @@ -3,6 +3,7 @@ package main import ( "embed" "io/fs" + "net/http" "reflect" "go.goblog.app/app/pkgs/plugins" @@ -78,6 +79,14 @@ func (a *goBlog) GetPost(path string) (plugintypes.Post, error) { return a.getPost(path) } +func (a *goBlog) PurgeCache() { + a.cache.purge() +} + +func (a *goBlog) GetHTTPClient() *http.Client { + return a.httpClient +} + func (p *post) GetPath() string { return p.Path }