mirror of
https://github.com/aykhans/slash-e.git
synced 2025-06-14 03:47:50 +00:00
chore: update server structure
This commit is contained in:
51
server/metric/metric.go
Normal file
51
server/metric/metric.go
Normal 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,
|
||||
})
|
||||
}
|
@ -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,
|
||||
})
|
||||
|
Reference in New Issue
Block a user