Skip to content

Commit 42348be

Browse files
committed
rafactor(be): system info to controller
1 parent bd7bf24 commit 42348be

File tree

5 files changed

+112
-48
lines changed

5 files changed

+112
-48
lines changed

api/router.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
proApi "github.com/semaphoreui/semaphore/pro/api"
1616
proProjects "github.com/semaphoreui/semaphore/pro/api/projects"
17-
proFeatures "github.com/semaphoreui/semaphore/pro/pkg/features"
1817
"github.com/semaphoreui/semaphore/services/server"
1918
taskServices "github.com/semaphoreui/semaphore/services/tasks"
2019

@@ -114,6 +113,7 @@ func Route(
114113
taskController := projects.NewTaskController(ansibleTaskRepo)
115114
rolesController := proApi.NewRolesController(store)
116115
templateController := projects.NewTemplateController(store, store)
116+
systmInfoController := NewSystemInfoController()
117117

118118
r := mux.NewRouter()
119119
r.NotFoundHandler = http.HandlerFunc(servePublic)
@@ -185,7 +185,7 @@ func Route(
185185
authenticatedAPI := r.PathPrefix(webPath + "api").Subrouter()
186186
authenticatedAPI.Use(StoreMiddleware, JSONMiddleware, authentication)
187187

188-
authenticatedAPI.Path("/info").HandlerFunc(getSystemInfo).Methods("GET", "HEAD")
188+
authenticatedAPI.Path("/info").HandlerFunc(systmInfoController.GetSystemInfo).Methods("GET", "HEAD")
189189

190190
authenticatedAPI.Path("/subscription").HandlerFunc(subscriptionController.Activate).Methods("POST")
191191
authenticatedAPI.Path("/subscription").HandlerFunc(subscriptionController.GetSubscription).Methods("GET")
@@ -657,47 +657,3 @@ func serveFile(w http.ResponseWriter, r *http.Request, name string) {
657657
),
658658
)
659659
}
660-
661-
func getSystemInfo(w http.ResponseWriter, r *http.Request) {
662-
user := helpers.GetFromContext(r, "user").(*db.User)
663-
664-
var authMethods LoginAuthMethods
665-
666-
if util.Config.Auth.Totp.Enabled {
667-
authMethods.Totp = &LoginTotpAuthMethod{
668-
AllowRecovery: util.Config.Auth.Totp.AllowRecovery,
669-
}
670-
}
671-
672-
if util.Config.Auth.Email.Enabled {
673-
authMethods.Email = &LoginEmailAuthMethod{}
674-
}
675-
676-
timezone := util.Config.Schedule.Timezone
677-
678-
if timezone == "" {
679-
timezone = "UTC"
680-
}
681-
682-
roles, err := helpers.Store(r).GetGlobalRoles()
683-
if err != nil {
684-
log.WithError(err).Error("Failed to get roles")
685-
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
686-
return
687-
}
688-
689-
body := map[string]any{
690-
"version": util.Version(),
691-
"ansible": util.AnsibleVersion(),
692-
"web_host": util.Config.WebHost,
693-
"use_remote_runner": util.Config.UseRemoteRunner,
694-
"auth_methods": authMethods,
695-
"premium_features": proFeatures.GetFeatures(user),
696-
"git_client": util.Config.GitClientId,
697-
"schedule_timezone": timezone,
698-
"teams": util.Config.Teams,
699-
"roles": roles,
700-
}
701-
702-
helpers.WriteJSON(w, http.StatusOK, body)
703-
}

api/system_info.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/semaphoreui/semaphore/api/helpers"
7+
"github.com/semaphoreui/semaphore/db"
8+
proFeatures "github.com/semaphoreui/semaphore/pro/pkg/features"
9+
"github.com/semaphoreui/semaphore/pro_interfaces"
10+
"github.com/semaphoreui/semaphore/util"
11+
log "github.com/sirupsen/logrus"
12+
)
13+
14+
type SystemInfoController struct {
15+
subscriptionService pro_interfaces.SubscriptionService
16+
}
17+
18+
func NewSystemInfoController() *SystemInfoController {
19+
return &SystemInfoController{}
20+
}
21+
22+
func (c *SystemInfoController) GetSystemInfo(w http.ResponseWriter, r *http.Request) {
23+
user := helpers.GetFromContext(r, "user").(*db.User)
24+
25+
var authMethods LoginAuthMethods
26+
27+
if util.Config.Auth.Totp.Enabled {
28+
authMethods.Totp = &LoginTotpAuthMethod{
29+
AllowRecovery: util.Config.Auth.Totp.AllowRecovery,
30+
}
31+
}
32+
33+
if util.Config.Auth.Email.Enabled {
34+
authMethods.Email = &LoginEmailAuthMethod{}
35+
}
36+
37+
timezone := util.Config.Schedule.Timezone
38+
39+
if timezone == "" {
40+
timezone = "UTC"
41+
}
42+
43+
roles, err := helpers.Store(r).GetGlobalRoles()
44+
if err != nil {
45+
log.WithError(err).Error("Failed to get roles")
46+
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
47+
return
48+
}
49+
50+
token, err := c.subscriptionService.GetToken()
51+
if err != nil {
52+
log.WithError(err).Error("Failed to get subscription plan")
53+
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
54+
return
55+
}
56+
57+
body := map[string]any{
58+
"version": util.Version(),
59+
"ansible": util.AnsibleVersion(),
60+
"web_host": util.Config.WebHost,
61+
"use_remote_runner": util.Config.UseRemoteRunner,
62+
"auth_methods": authMethods,
63+
"premium_features": proFeatures.GetFeatures(user, token.Plan),
64+
"git_client": util.Config.GitClientId,
65+
"schedule_timezone": timezone,
66+
"teams": util.Config.Teams,
67+
"roles": roles,
68+
}
69+
70+
helpers.WriteJSON(w, http.StatusOK, body)
71+
}

pro/pkg/features/features.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package features
22

3-
import "github.com/semaphoreui/semaphore/db"
3+
import (
4+
"github.com/semaphoreui/semaphore/db"
5+
"github.com/semaphoreui/semaphore/pro_interfaces"
6+
)
7+
8+
func GetFeatures(user *db.User, plan string) map[string]bool {
49

5-
func GetFeatures(user *db.User) map[string]bool {
610
return map[string]bool{
711
"project_runners": false,
812
"terraform_backend": false,

pro/services/server/subscription_svc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ func (s *SubscriptionServiceImpl) CanAddProUser() (ok bool, err error) {
2323
func (s *SubscriptionServiceImpl) StartValidationCron() {
2424

2525
}
26+
27+
func (s *SubscriptionServiceImpl) CanAddRole() (ok bool, err error) {
28+
return
29+
}
30+
31+
func (s *SubscriptionServiceImpl) CanAddRunner() (ok bool, err error) {
32+
return
33+
}
34+
35+
func (s *SubscriptionServiceImpl) CanAddTerraformHTTPBackend() (ok bool, err error) {
36+
return
37+
}
38+
39+
func (s *SubscriptionServiceImpl) GetPlan() (plan string, err error) {
40+
return
41+
}

pro_interfaces/subscription_svc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package pro_interfaces
22

3+
import "time"
4+
5+
type SubscriptionToken struct {
6+
Company string `json:"company,omitempty"`
7+
State string `json:"state"`
8+
Key string `json:"key"`
9+
Plan string `json:"plan"`
10+
Users int `json:"users"`
11+
ExpiresAt time.Time `json:"expiresAt"`
12+
Nodes int `json:"nodes,omitempty"`
13+
}
14+
15+
func (t *SubscriptionToken) Validate() error {
16+
return nil
17+
}
18+
319
type SubscriptionService interface {
420
HasActiveSubscription() bool
521
CanAddProUser() (ok bool, err error)
622
CanAddRunner() (ok bool, err error)
723
CanAddTerraformHTTPBackend() (ok bool, err error)
824
StartValidationCron()
25+
GetToken() (res SubscriptionToken, err error)
926
}

0 commit comments

Comments
 (0)