/
common_ctl.go
68 lines (57 loc) · 1.76 KB
/
common_ctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controller
import (
"context"
"net/http"
"github.com/go-openapi/swag"
logging "github.com/ipfs/go-log/v2"
"github.com/jiaozifs/jiaozifs/api"
"github.com/jiaozifs/jiaozifs/config"
"github.com/jiaozifs/jiaozifs/utils"
"github.com/jiaozifs/jiaozifs/version"
"go.uber.org/fx"
)
var commonLog = logging.Logger("common")
type CommonController struct {
fx.In
VersionChecker version.IChecker
Config *config.Config
}
func (c CommonController) GetVersion(_ context.Context, w *api.JiaozifsResponse, _ *http.Request) {
swagger, err := api.GetSwagger()
if err != nil {
w.Error(err)
return
}
latestVersionResp, err := c.VersionChecker.CheckLatestVersion()
if err == nil {
commonLog.Errorf("fetch latest version failed: %v", err)
}
var latestVersion *string
if latestVersionResp != nil {
latestVersion = utils.String(latestVersionResp.LatestVersion)
}
w.JSON(api.VersionResult{
ApiVersion: swagger.Info.Version,
Version: version.UserVersion(),
LatestVersion: latestVersion,
})
}
func newLoginConfig(c *config.AuthConfig) *api.LoginConfig {
return &api.LoginConfig{
RBAC: (*api.LoginConfigRBAC)(&c.UIConfig.RBAC),
LoginUrl: c.UIConfig.LoginURL,
LoginFailedMessage: &c.UIConfig.LoginFailedMessage,
FallbackLoginUrl: c.UIConfig.FallbackLoginURL,
FallbackLoginLabel: c.UIConfig.FallbackLoginLabel,
LoginCookieNames: c.UIConfig.LoginCookieNames,
LogoutUrl: c.UIConfig.LogoutURL,
}
}
func (c CommonController) GetSetupState(_ context.Context, w *api.JiaozifsResponse, _ *http.Request) {
state := api.SetupState{
State: (*api.SetupStateState)(swag.String("initialized")),
LoginConfig: newLoginConfig(&c.Config.Auth),
CommPrefsMissing: swag.Bool(false),
}
w.JSON(state)
}