Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Add API documentation and Swagger UI configuration endpoints

lotkf8168363523f
diff --git a/core/api/settings/settings.go b/core/api/settings/settings.go
index 6286959a..967a9a53 100644
--- a/core/api/settings/settings.go
+++ b/core/api/settings/settings.go
@@ -22,4 +22,6 @@ type ISettingsV1 interface {
AddIPWhitelist(ctx context.Context, req *v1.AddIPWhitelistReq) (res *v1.AddIPWhitelistRes, err error)
SetReverseProxyDomain(ctx context.Context, req *v1.SetReverseProxyDomainReq) (res *v1.SetReverseProxyDomainRes, err error)
DeleteReverseProxyDomain(ctx context.Context, req *v1.DeleteReverseProxyDomainReq) (res *v1.DeleteReverseProxyDomainRes, err error)
+ RegenerateAPIToken(ctx context.Context, req *v1.RegenerateAPITokenReq) (res *v1.RegenerateAPITokenRes, err error)
+ SetAPIDocSwagger(ctx context.Context, req *v1.SetAPIDocSwaggerReq) (res *v1.SetAPIDocSwaggerRes, err error)
}
diff --git a/core/api/settings/v1/settings.go b/core/api/settings/v1/settings.go
index 6fa1c8cb..277c76dc 100644
--- a/core/api/settings/v1/settings.go
+++ b/core/api/settings/v1/settings.go
@@ -85,6 +85,14 @@ type SystemConfig struct {
CurrentUrl string `json:"current_url" dc:"current url"`
ReverseProxy string `json:"reverse_proxy" dc:"reverse proxy"`
} `json:"reverse_proxy_domain" dc:"reverse proxy domain"`
+
+ // API configuration
+ APIDocSwagger struct {
+ APIDocURL string `json:"api_doc_url" dc:"api doc url"`
+ SwaggerURL string `json:"swagger_url" dc:"swagger url"`
+ APIDocEnabled bool `json:"api_doc_enabled" dc:"enable API documentation and Swagger UI"`
+ APIToken string `json:"api_token" dc:"API access token"`
+ } `json:"api_doc_swagger" dc:"API doc and Swagger UI configuration"`
}
type GetVersionReq struct {
@@ -197,3 +205,27 @@ type DeleteReverseProxyDomainReq struct {
type DeleteReverseProxyDomainRes struct {
api_v1.StandardRes
}
+
+// Regenerate API Token request
+type RegenerateAPITokenReq struct {
+ g.Meta `path:"/settings/regenerate_api_token" tags:"Settings" method:"post" summary:"Regenerate API Token"`
+ Authorization string `json:"authorization" in:"header" dc:"Authorization" v:"required"`
+}
+
+// Regenerate API Token response
+type RegenerateAPITokenRes struct {
+ api_v1.StandardRes
+ Data struct {
+ APIToken string `json:"api_token" dc:"New API Token"`
+ } `json:"data" dc:"Generated API Token"`
+}
+
+// 设置api文档和Swagger的开启状态
+type SetAPIDocSwaggerReq struct {
+ g.Meta `path:"/settings/set_api_doc_swagger" tags:"Settings" method:"post" summary:"Set API Doc and Swagger UI configuration"`
+ Authorization string `json:"authorization" in:"header" dc:"Authorization" v:"required"`
+ APIDocEnabled bool `json:"api_doc_enabled" dc:"Enable API documentation and Swagger UI"`
+}
+type SetAPIDocSwaggerRes struct {
+ api_v1.StandardRes
+}
diff --git a/core/internal/controller/settings/settings_v1_delete_ip_whitelist.go b/core/internal/controller/settings/settings_v1_delete_ip_whitelist.go
index f9889e64..d56593c2 100644
--- a/core/internal/controller/settings/settings_v1_delete_ip_whitelist.go
+++ b/core/internal/controller/settings/settings_v1_delete_ip_whitelist.go
@@ -46,7 +46,7 @@ func (c *ControllerV1) DeleteIPWhitelist(ctx context.Context, req *v1.DeleteIPWh
g.Log().Error(ctx, "Failed to set IP whitelist enable: {}", err)
}
gtimer.AddOnce(500*time.Millisecond, func() {
- err = public.DockerApiFromCtx(ctx).RestartContainerByName(ctx, consts.SERVICES.Core)
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(context.Background(), consts.SERVICES.Core)
if err != nil {
g.Log().Error(ctx, "Failed to restart container: {}", err)
return
diff --git a/core/internal/controller/settings/settings_v1_get_system_config.go b/core/internal/controller/settings/settings_v1_get_system_config.go
index aaf03785..292e12a7 100644
--- a/core/internal/controller/settings/settings_v1_get_system_config.go
+++ b/core/internal/controller/settings/settings_v1_get_system_config.go
@@ -41,9 +41,28 @@ func (c *ControllerV1) GetSystemConfig(ctx context.Context, req *v1.GetSystemCon
if err != nil {
reverseProxyDomain = ""
}
-
+ BaseURL := domains.GetBaseURL()
config.ReverseProxyDomain.ReverseProxy = reverseProxyDomain
- config.ReverseProxyDomain.CurrentUrl = domains.GetBaseURL()
+ config.ReverseProxyDomain.CurrentUrl = BaseURL
+
+ // Load API configuration
+ var apiDocEnabled bool
+ var apiToken string
+
+ err = public.OptionsMgrInstance.GetOption(ctx, "API_DOC_ENABLED", &apiDocEnabled)
+ if err != nil {
+ apiDocEnabled = true
+ }
+
+ err = public.OptionsMgrInstance.GetOption(ctx, "API_TOKEN", &apiToken)
+ if err != nil {
+ apiToken = ""
+ }
+
+ config.APIDocSwagger.APIDocEnabled = apiDocEnabled
+ config.APIDocSwagger.APIToken = apiToken
+ config.APIDocSwagger.APIDocURL = BaseURL + "/api.json"
+ config.APIDocSwagger.SwaggerURL = BaseURL + "/swagger"
res.Data = config
diff --git a/core/internal/controller/settings/settings_v1_regenerate_api_token.go b/core/internal/controller/settings/settings_v1_regenerate_api_token.go
new file mode 100644
index 00000000..5007462a
--- /dev/null
+++ b/core/internal/controller/settings/settings_v1_regenerate_api_token.go
@@ -0,0 +1,51 @@
+package settings
+
+import (
+ "billionmail-core/internal/consts"
+ "billionmail-core/internal/service/public"
+ "billionmail-core/internal/service/rbac"
+ "context"
+ "github.com/gogf/gf/v2/errors/gerror"
+ "github.com/gogf/gf/v2/frame/g"
+
+ "billionmail-core/api/settings/v1"
+)
+
+func (c *ControllerV1) RegenerateAPIToken(ctx context.Context, req *v1.RegenerateAPITokenReq) (res *v1.RegenerateAPITokenRes, err error) {
+ res = &v1.RegenerateAPITokenRes{}
+ // Get current admin user info from JWT token
+ claims, err := rbac.JWT().ParseTokenByRequest(g.RequestFromCtx(ctx))
+ g.Log().Error(ctx, "RegenerateAPIToken claims: ", claims)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get current user information: {}", err)))
+ return res, nil
+ }
+
+ // Generate new API token
+ newAPIToken, _, err := rbac.JWT().GenerateApiToken(claims.AccountId, claims.Username, []string{"admin"})
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to generate new API token: {}", err)))
+ return res, nil
+ }
+
+ // Save new API token to database
+ err = public.OptionsMgrInstance.SetOption(ctx, "API_TOKEN", newAPIToken)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to save new API token: {}", err)))
+ return res, nil
+ }
+
+ // Log the operation
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.Settings,
+ Log: "Regenerated API token",
+ Data: map[string]interface{}{
+ "user": claims.Username,
+ "user_id": claims.AccountId,
+ },
+ })
+
+ res.Data.APIToken = newAPIToken
+ res.SetSuccess(public.LangCtx(ctx, "API token regenerated successfully"))
+ return res, nil
+}
diff --git a/core/internal/controller/settings/settings_v1_set_api_doc_swagger.go b/core/internal/controller/settings/settings_v1_set_api_doc_swagger.go
new file mode 100644
index 00000000..c92d7a28
--- /dev/null
+++ b/core/internal/controller/settings/settings_v1_set_api_doc_swagger.go
@@ -0,0 +1,54 @@
+package settings
+
+import (
+ "billionmail-core/api/settings/v1"
+ "billionmail-core/internal/consts"
+ "billionmail-core/internal/service/public"
+ "context"
+ "github.com/gogf/gf/os/gtimer"
+ "github.com/gogf/gf/v2/errors/gerror"
+ "time"
+)
+
+func (c *ControllerV1) SetAPIDocSwagger(ctx context.Context, req *v1.SetAPIDocSwaggerReq) (res *v1.SetAPIDocSwaggerRes, err error) {
+ res = &v1.SetAPIDocSwaggerRes{}
+
+ // Track if any configuration was modified
+ configModified := false
+
+ // Update API documentation enabled status (controls both API docs and Swagger UI)
+ var currentAPIDocEnabled bool
+ err = public.OptionsMgrInstance.GetOption(ctx, "API_DOC_ENABLED", &currentAPIDocEnabled)
+ if err != nil || currentAPIDocEnabled != req.APIDocEnabled {
+ err = public.OptionsMgrInstance.SetOption(ctx, "API_DOC_ENABLED", req.APIDocEnabled)
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to update API documentation setting: {}", err)))
+ return res, nil
+ }
+ configModified = true
+ }
+
+ if configModified {
+ // Log the operation
+ _ = public.WriteLog(ctx, public.LogParams{
+ Type: consts.LOGTYPE.Settings,
+ Log: "Updated API documentation and Swagger UI configuration",
+ Data: map[string]interface{}{
+ "api_doc_enabled": req.APIDocEnabled,
+ },
+ })
+
+ gtimer.AddOnce(500*time.Millisecond, func() {
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(context.Background(), consts.SERVICES.Core)
+ if err != nil {
+ return
+ }
+ })
+
+ res.SetSuccess(public.LangCtx(ctx, "API documentation and Swagger UI configuration updated successfully, service restarted"))
+ } else {
+ res.SetSuccess(public.LangCtx(ctx, "No configuration changes needed"))
+ }
+
+ return res, nil
+}
diff --git a/core/internal/controller/settings/settings_v1_set_ssl_config.go b/core/internal/controller/settings/settings_v1_set_ssl_config.go
index 2d3c7423..74c04a16 100644
--- a/core/internal/controller/settings/settings_v1_set_ssl_config.go
+++ b/core/internal/controller/settings/settings_v1_set_ssl_config.go
@@ -4,8 +4,10 @@ import (
"context"
"crypto/x509"
"encoding/pem"
+ "github.com/gogf/gf/os/gtimer"
"os"
"path/filepath"
+ "time"
"billionmail-core/api/settings/v1"
"billionmail-core/internal/consts"
@@ -84,11 +86,12 @@ func (c *ControllerV1) SetSSLConfig(ctx context.Context, req *v1.SetSSLConfigReq
return res, nil
}
// Restart the container
- err = public.DockerApiFromCtx(ctx).RestartContainerByName(ctx, consts.SERVICES.Core)
- if err != nil {
- res.SetError(gerror.New(public.LangCtx(ctx, "Failed to restart container: {}", err)))
- return res, nil
- }
+ gtimer.AddOnce(500*time.Millisecond, func() {
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(context.Background(), consts.SERVICES.Core)
+ if err != nil {
+ return
+ }
+ })
_ = public.WriteLog(ctx, public.LogParams{
Type: consts.LOGTYPE.Settings,
diff --git a/core/internal/controller/settings/settings_v1_set_system_config.go b/core/internal/controller/settings/settings_v1_set_system_config.go
index baa536a1..1dc3751a 100644
--- a/core/internal/controller/settings/settings_v1_set_system_config.go
+++ b/core/internal/controller/settings/settings_v1_set_system_config.go
@@ -4,9 +4,11 @@ import (
"billionmail-core/internal/consts"
"billionmail-core/internal/service/public"
"context"
+ "github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/v2/os/gfile"
"os"
"strings"
+ "time"
"github.com/gogf/gf/v2/errors/gerror"
@@ -59,12 +61,12 @@ func (c *ControllerV1) SetSystemConfig(ctx context.Context, req *v1.SetSystemCon
return res, nil
}
- err = public.DockerApiFromCtx(ctx).RestartContainerByName(ctx, consts.SERVICES.Core)
-
- if err != nil {
- res.SetError(gerror.New(public.LangCtx(ctx, "Failed to restart container: {}", err)))
- return res, nil
- }
+ gtimer.AddOnce(500*time.Millisecond, func() {
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(context.Background(), consts.SERVICES.Core)
+ if err != nil {
+ return
+ }
+ })
_ = public.WriteLog(ctx, public.LogParams{
Type: consts.LOGTYPE.Settings,
diff --git a/core/internal/controller/settings/settings_v1_set_system_config_key.go b/core/internal/controller/settings/settings_v1_set_system_config_key.go
index cf078448..d7c422ae 100644
--- a/core/internal/controller/settings/settings_v1_set_system_config_key.go
+++ b/core/internal/controller/settings/settings_v1_set_system_config_key.go
@@ -57,7 +57,7 @@ func (c *ControllerV1) SetSystemConfigKey(ctx context.Context, req *v1.SetSystem
// 4. Restart the container
gtimer.AddOnce(500*time.Millisecond, func() {
- err = public.DockerApiFromCtx(ctx).RestartContainerByName(ctx, consts.SERVICES.Core)
+ err = public.DockerApiFromCtx(ctx).RestartContainerByName(context.Background(), consts.SERVICES.Core)
if err != nil {
g.Log().Error(ctx, "Failed to restart container: {}", err)
return