chore: update server structure

This commit is contained in:
Steven
2023-10-17 21:54:22 +08:00
parent 328397612c
commit 3225e7c47b
10 changed files with 76 additions and 2 deletions

51
server/metric/metric.go Normal file
View File

@ -0,0 +1,51 @@
package metric
import (
"github.com/posthog/posthog-go"
"github.com/boojack/slash/server/profile"
)
const (
PostHogAPIKey = "phc_YFEi1aqUBW9sX2KDzdvMtK43DNu0mkeoKMKc0EQum2t"
)
var (
client *MetricClient
)
type MetricClient struct {
workspaceID string
profile *profile.Profile
phClient *posthog.Client
}
func NewMetricClient(workspaceID string, profile profile.Profile) (*MetricClient, error) {
phClient, err := posthog.NewWithConfig(PostHogAPIKey, posthog.Config{
Endpoint: "https://app.posthog.com",
})
if err != nil {
return nil, err
}
client = &MetricClient{
workspaceID: workspaceID,
profile: &profile,
phClient: &phClient,
}
return client, nil
}
func Enqueue(event string) {
if client == nil {
return
}
if client.profile.Mode != "prod" {
return
}
// nolint
(*client.phClient).Enqueue(posthog.Capture{
DistinctId: `slash-` + client.workspaceID,
Event: event,
})
}

View File

@ -18,6 +18,7 @@ import (
apiv2 "github.com/boojack/slash/api/v2"
"github.com/boojack/slash/internal/log"
storepb "github.com/boojack/slash/proto/gen/store"
"github.com/boojack/slash/server/metric"
"github.com/boojack/slash/server/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/server/service/resource"
@ -29,6 +30,7 @@ type Server struct {
Profile *profile.Profile
Store *store.Store
Secret string
licenseService *license.LicenseService
@ -93,11 +95,12 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
secret := "slash"
if profile.Mode == "prod" {
var err error
secret, err = s.getSystemSecretSessionName(ctx)
secret, err = s.getSecretSessionName(ctx)
if err != nil {
return nil, err
}
}
s.Secret = secret
rootGroup := e.Group("")
// Register API v1 routes.
@ -133,6 +136,7 @@ func (s *Server) Start(ctx context.Context) error {
}
}()
metric.Enqueue("server start")
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
}
@ -161,7 +165,7 @@ func grpcRequestSkipper(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/slash.api.v2.")
}
func (s *Server) getSystemSecretSessionName(ctx context.Context) (string, error) {
func (s *Server) getSecretSessionName(ctx context.Context) (string, error) {
secretSessionSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_SECRET_SESSION,
})