A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
refactor(plugin): Directory movement
LinkinStar1802e6fa2e14
diff --git a/internal/controller/connector_controller.go b/internal/controller/connector_controller.goindex df67b3f5..a736760c 100644--- a/internal/controller/connector_controller.go+++ b/internal/controller/connector_controller.go@@ -6,12 +6,11 @@ import ("github.com/answerdev/answer/internal/base/handler""github.com/answerdev/answer/internal/base/middleware"- "github.com/answerdev/answer/internal/plugin"- _ "github.com/answerdev/answer/internal/plugin/connector""github.com/answerdev/answer/internal/schema""github.com/answerdev/answer/internal/service/export""github.com/answerdev/answer/internal/service/siteinfo_common""github.com/answerdev/answer/internal/service/user_external_login"+ "github.com/answerdev/answer/plugin""github.com/gin-gonic/gin""github.com/segmentfault/pacman/log")diff --git a/internal/controller_admin/plugin_controller.go b/internal/controller_admin/plugin_controller.goindex d22d11d8..e638d442 100644--- a/internal/controller_admin/plugin_controller.go+++ b/internal/controller_admin/plugin_controller.go@@ -4,9 +4,9 @@ import ("encoding/json""github.com/answerdev/answer/internal/base/handler"- "github.com/answerdev/answer/internal/plugin""github.com/answerdev/answer/internal/schema""github.com/answerdev/answer/internal/service/plugin_common"+ "github.com/answerdev/answer/plugin""github.com/gin-gonic/gin")diff --git a/internal/plugin/connector/google.go b/internal/plugin/connector/google.godeleted file mode 100644index 7dd2eae6..00000000--- a/internal/plugin/connector/google.go+++ /dev/null@@ -1,124 +0,0 @@-package connector--import (- "context"- "encoding/json"- "fmt"- "io"- "net/http"- "os"- "time"-- "github.com/answerdev/answer/internal/plugin"- "github.com/go-resty/resty/v2"- "golang.org/x/oauth2"- oauth2Google "golang.org/x/oauth2/google"-)--type Google struct {- ClientID string- ClientSecret string-}--func init() {- plugin.Register(&Google{- ClientID: os.Getenv("GOOGLE_CLIENT_ID"),- ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),- })-}--func (g *Google) Info() plugin.Info {- return plugin.Info{- Name: "google connector",- SlugName: "google_connector",- Description: "google connector plugin",- Version: "0.0.1",- }-}--func (g *Google) ConnectorLogo() []byte {- response, err := resty.New().R().Get("https://cdn-icons-png.flaticon.com/32/300/300221.png")- if err != nil {- return nil- }- return response.Body()-}--func (g *Google) ConnectorLogoContentType() string {- return "image/png"-}--func (g *Google) ConnectorName() string {- return "Google"-}--func (g *Google) ConnectorSlugName() string {- return "google"-}--func (g *Google) ConnectorSender(ctx *plugin.GinContext) {- oauth2Config := &oauth2.Config{- ClientID: g.ClientID,- ClientSecret: g.ClientSecret,- Endpoint: oauth2Google.Endpoint,- RedirectURL: "http://127.0.0.1:8080/answer/api/v1/oauth/redirect/google", // TODO: Pass by parameter- Scopes: []string{- "https://www.googleapis.com/auth/userinfo.email",- "https://www.googleapis.com/auth/userinfo.profile",- "openid",- },- }- ctx.Redirect(http.StatusFound, oauth2Config.AuthCodeURL("state"))-}--type GoogleAuthUserInfo struct {- Sub string `json:"sub"`- Name string `json:"name"`- GivenName string `json:"given_name"`- FamilyName string `json:"family_name"`- Profile string `json:"profile"`- Picture string `json:"picture"`- Email string `json:"email"`- EmailVerified bool `json:"email_verified"`- Gender string `json:"gender"`-}--func (g *Google) ConnectorReceiver(ctx *plugin.GinContext) {- code := ctx.Query("code")- oauth2Config := &oauth2.Config{- ClientID: g.ClientID,- ClientSecret: g.ClientSecret,- Endpoint: oauth2Google.Endpoint,- }-- token, err := oauth2Config.Exchange(context.Background(), code)- if err != nil {- ctx.Redirect(http.StatusFound, "/50x")- return- }-- client := oauth2Config.Client(context.TODO(), token)- client.Timeout = 60 * time.Second- userinfo, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")- if err != nil {- ctx.Redirect(http.StatusFound, "/50x")- return- }- defer userinfo.Body.Close()- data, _ := io.ReadAll(userinfo.Body)-- userInfo := &GoogleAuthUserInfo{}- if err = json.Unmarshal(data, userInfo); err != nil {- ctx.Redirect(http.StatusFound, "/50x")- return- }-- fmt.Printf("user info is :%+v", userInfo)-- // TODO- // If user email exists, try to login this user.- // If user email not exists, try to register this user.-- ctx.Redirect(http.StatusFound, "/login-success?access_token=token")- return-}diff --git a/internal/plugin/sample/filter_sample.go b/internal/plugin/sample/filter_sample.godeleted file mode 100644index e50218dd..00000000--- a/internal/plugin/sample/filter_sample.go+++ /dev/null@@ -1,30 +0,0 @@-package sample--import (- "fmt"- "strings"-- "github.com/answerdev/answer/internal/plugin"-)--type FilterSample struct {-}--func (s *FilterSample) Info() plugin.Info {- return plugin.Info{- Name: "filter sample",- Description: "filter sample plugin",- Version: "0.0.1",- }-}--func (s *FilterSample) FilterText(data string) (err error) {- if strings.Contains(data, "violent") {- return fmt.Errorf("bloody and violent words cannot appear in this website")- }- return nil-}--func init() {- plugin.Register(&FilterSample{})-}diff --git a/internal/plugin/sample/filter_sample_test.go b/internal/plugin/sample/filter_sample_test.godeleted file mode 100644index ba72cb20..00000000--- a/internal/plugin/sample/filter_sample_test.go+++ /dev/null@@ -1,18 +0,0 @@-package sample--import (- "fmt"- "testing"-- "github.com/answerdev/answer/internal/plugin"- "github.com/stretchr/testify/assert"-)--func TestFilterSample_FilterText(t *testing.T) {- // try to call filter plugin for filter text that are not allowed- err := plugin.CallFilter(func(fn plugin.Filter) error {- return fn.FilterText("bloody and violent words")- })- assert.Error(t, err)- fmt.Println(err)-}diff --git a/internal/plugin/sample/sample.go b/internal/plugin/sample/sample.godeleted file mode 100644index 42e2b7e9..00000000--- a/internal/plugin/sample/sample.go+++ /dev/null@@ -1,18 +0,0 @@-package sample--import "github.com/answerdev/answer/internal/plugin"--type Sample struct {-}--func (s *Sample) Info() plugin.Info {- return plugin.Info{- Name: "sample",- Description: "sample plugin",- Version: "0.0.1",- }-}--func init() {- plugin.Register(&Sample{})-}diff --git a/internal/router/plugin_api_router.go b/internal/router/plugin_api_router.goindex cfc975f1..5d55863b 100644--- a/internal/router/plugin_api_router.go+++ b/internal/router/plugin_api_router.go@@ -2,7 +2,7 @@ package routerimport ("github.com/answerdev/answer/internal/controller"- "github.com/answerdev/answer/internal/plugin"+ "github.com/answerdev/answer/plugin""github.com/gin-gonic/gin")diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.goindex d878f98a..c8f7d799 100644--- a/internal/service/plugin_common/plugin_common_service.go+++ b/internal/service/plugin_common/plugin_common_service.go@@ -7,9 +7,9 @@ import ("github.com/answerdev/answer/internal/base/constant""github.com/answerdev/answer/internal/base/reason""github.com/answerdev/answer/internal/entity"- "github.com/answerdev/answer/internal/plugin""github.com/answerdev/answer/internal/schema""github.com/answerdev/answer/internal/service/config"+ "github.com/answerdev/answer/plugin""github.com/segmentfault/pacman/errors""github.com/segmentfault/pacman/log")diff --git a/pkg/checker/reserved_username.go b/pkg/checker/reserved_username.goindex 91b5f9ac..01d31997 100644--- a/pkg/checker/reserved_username.go+++ b/pkg/checker/reserved_username.go@@ -4,7 +4,6 @@ import ("encoding/json""github.com/answerdev/answer/configs"- "github.com/segmentfault/pacman/log")var (@@ -14,7 +13,6 @@ var (func init() {var usernames []string_ = json.Unmarshal(configs.ReservedUsernames, &usernames)- log.Debugf("get reserved usernames %d", len(usernames))for _, username := range usernames {reservedUsernameMapping[username] = true}diff --git a/internal/plugin/base.go b/plugin/base.gosimilarity index 100%rename from internal/plugin/base.gorename to plugin/base.godiff --git a/internal/plugin/config.go b/plugin/config.gosimilarity index 100%rename from internal/plugin/config.gorename to plugin/config.godiff --git a/internal/plugin/connector.go b/plugin/connector.gosimilarity index 100%rename from internal/plugin/connector.gorename to plugin/connector.godiff --git a/internal/plugin/connector/github.go b/plugin/connector/github.gosimilarity index 98%rename from internal/plugin/connector/github.gorename to plugin/connector/github.goindex 3e79cbdf..863b1205 100644--- a/internal/plugin/connector/github.go+++ b/plugin/connector/github.go@@ -6,7 +6,7 @@ import ("fmt""os"- "github.com/answerdev/answer/internal/plugin"+ "github.com/answerdev/answer/plugin""github.com/google/go-github/v48/github""golang.org/x/oauth2"oauth2GitHub "golang.org/x/oauth2/github"diff --git a/plugin/connector/google.go b/plugin/connector/google.gonew file mode 100644index 00000000..d0510ac4--- /dev/null+++ b/plugin/connector/google.go@@ -0,0 +1,118 @@+package connector++import (+ "context"+ "encoding/json"+ "fmt"+ "io"+ "os"+ "time"++ "github.com/answerdev/answer/plugin"+ "golang.org/x/oauth2"+ oauth2Google "golang.org/x/oauth2/google"+)++type Google struct {+ Config *GoogleConfig+}++type GoogleConfig struct {+ ClientID string `json:"client_id"`+ ClientSecret string `json:"client_secret"`+}++type GoogleAuthUserInfo struct {+ ID string `json:"id"`+ Sub string `json:"sub"`+ Name string `json:"name"`+ GivenName string `json:"given_name"`+ FamilyName string `json:"family_name"`+ Profile string `json:"profile"`+ Picture string `json:"picture"`+ Email string `json:"email"`+ EmailVerified bool `json:"email_verified"`+ Gender string `json:"gender"`+}++func init() {+ plugin.Register(&Google{+ Config: &GoogleConfig{+ ClientID: os.Getenv("GOOGLE_CLIENT_ID"),+ ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),+ },+ })+}++func (g *Google) Info() plugin.Info {+ return plugin.Info{+ Name: "google connector",+ SlugName: "google_connector",+ Description: "google connector plugin",+ Version: "0.0.1",+ }+}++func (g *Google) ConnectorLogoSVG() string {+ //TODO implement me+ panic("implement me")+}++func (g *Google) ConnectorSender(ctx *plugin.GinContext, receiverURL string) (redirectURL string) {+ oauth2Config := &oauth2.Config{+ ClientID: g.Config.ClientID,+ ClientSecret: g.Config.ClientSecret,+ Endpoint: oauth2Google.Endpoint,+ RedirectURL: receiverURL,+ Scopes: []string{+ "https://www.googleapis.com/auth/userinfo.email",+ "https://www.googleapis.com/auth/userinfo.profile",+ "openid",+ },+ }+ return oauth2Config.AuthCodeURL("state")+}++func (g *Google) ConnectorReceiver(ctx *plugin.GinContext) (userInfo plugin.ExternalLoginUserInfo, err error) {+ code := ctx.Query("code")+ oauth2Config := &oauth2.Config{+ ClientID: g.Config.ClientID,+ ClientSecret: g.Config.ClientSecret,+ Endpoint: oauth2Google.Endpoint,+ }++ token, err := oauth2Config.Exchange(context.Background(), code)+ if err != nil {+ return userInfo, err+ }++ client := oauth2Config.Client(context.TODO(), token)+ client.Timeout = 15 * time.Second+ response, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")+ if err != nil {+ return userInfo, err+ }+ defer response.Body.Close()+ data, _ := io.ReadAll(response.Body)++ respGoogleAuthUserInfo := &GoogleAuthUserInfo{}+ if err = json.Unmarshal(data, respGoogleAuthUserInfo); err != nil {+ return userInfo, fmt.Errorf("parse google oauth user info response failed: %v", err)+ }++ userInfo = plugin.ExternalLoginUserInfo{+ ExternalID: respGoogleAuthUserInfo.ID,+ Name: respGoogleAuthUserInfo.Name,+ Email: respGoogleAuthUserInfo.Email,+ MetaInfo: string(data),+ }+ return userInfo, nil+}++func (g *Google) ConnectorName() string {+ return "Google"+}++func (g *Google) ConnectorSlugName() string {+ return "google"+}diff --git a/internal/plugin/filter.go b/plugin/filter.gosimilarity index 100%rename from internal/plugin/filter.gorename to plugin/filter.godiff --git a/internal/plugin/parser.go b/plugin/parser.gosimilarity index 100%rename from internal/plugin/parser.gorename to plugin/parser.godiff --git a/internal/plugin/plugin.go b/plugin/plugin.gosimilarity index 100%rename from internal/plugin/plugin.gorename to plugin/plugin.go