Files
plane/monitor/lib/router/router.go
Nikhil 4b31dd0c01 dev: new license activation workflow for one and pro users (#739)
* feat: added feature flag package for decrypting and refreshing the feature flags value

* feat: added feature flagging route for route handlers

* feat: added feature flag worker for managing and distributing feature flags

* feat: implemented feature flag api implementation inside ff handler

* feat: added exponential retry interval inside feature flagging

* fix: route pattern of feature flag endpoint from monitor

* feat: created endpoint to activate workspace from monitor

* feat: made changes to accomodate db and activate endpoint

* dev: update multi tenant environment settings

* dev: workspace license activation flow

* dev: update license activate endpoint

* dev: update workspace license activate endpoint

* feat: added feature flag user sync logic

* feat: added constants and sync logic with private key inside

* feat: added port as env variable

* feat: added modification for removing license key in healthcheck

* feat: example env pushed

* fix: update monitor endpoints and license activation function

* dev: initiate free workspace only for self hosted instances

* dev: self hosting license endpoint updates

* dev: remove monitor db

* dev: update handlers

* dev: self hosted license subscription web

* dev: removed computed in self hosted subscription

* fix: prime server url and delete old license while creating new license in sqlite

* chore: subscription activation alert after successfull activation.

* dev: update feature flag endpoints

* fix: handled several cases of feature flags

* fix: linting errors

* dev: update monitor to handle workspace activation

* dev: update decryption algorithm

* feat: separated activation payload with the sync payload

* dev: update feature flags endpoint

* dev: change type for workspace sync

* feat: added isActive flag for user activation

* feat: removed creation of free users in monitor

* feat: added method for workspace product

* dev: add user email in types

* feat: added method for workspace product

* fix: uer license feature flag fetch

* fix: handler fixed for feature key

* chore: self managed license activation

* chore: update validation license flow.

* chore: license activation success.

* dev: resync workspace license on activation

* chore: plane one success modal.

* feat: added constructs to inject private key build time

* feat: updated the docker file with private key build argument

* feat: added consumption od DEFAULT_PRIVATE_KEY when building monitor

* dev: update private key

---------

Co-authored-by: Henit Chobisa <chobisa.henit@gmail.com>
Co-authored-by: gurusainath <gurusainath007@gmail.com>
Co-authored-by: Prateek Shourya <prateekshourya29@gmail.com>
Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
2024-08-22 17:39:19 +05:30

109 lines
2.8 KiB
Go

package router
import (
"context"
"fmt"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
prime_api "github.com/makeplane/plane-ee/monitor/lib/api"
primelogger "github.com/makeplane/plane-ee/monitor/lib/logger"
"github.com/makeplane/plane-ee/monitor/lib/router/routes"
)
type MonitorRouter struct {
app *fiber.App
logger *primelogger.Handler
}
// Takes MonitorRouterOptions and returns a new monitor router pointer,
// initialized with those options
func NewMonitorRouter(config MonitorRouterOptions) *MonitorRouter {
// Set the config to the default options
cfg := defaultRouterOptions
// If the length of the config is greater that one than we set the cfg to the
// config that is provided inside the arguments.
// Create a new app router based on the cfg provided
app := fiber.New(
fiber.Config{
AppName: cfg.AppName,
Immutable: false,
JSONEncoder: cfg.Encoder,
JSONDecoder: cfg.Decoder,
DisableKeepalive: cfg.DisableKeepAlive,
ReduceMemoryUsage: cfg.ReduceMemoryUsage,
},
)
router := &MonitorRouter{
app: app,
logger: config.Logger,
}
router.bindRoutes(config.Api, config.PrivateKey)
router.bindMiddlewares()
return router
}
func (r *MonitorRouter) SetLogger(logger primelogger.Handler) {
r.logger = &logger
}
// ------------------ Orchestration Methods ----------------------
func (r *MonitorRouter) Start(host string, port string) error {
if err := r.app.Listen(fmt.Sprintf("%s:%s", host, port)); err != nil {
return err
}
return nil
}
func (r *MonitorRouter) Stop() error {
if err := r.app.ShutdownWithTimeout(5 * time.Second); err != nil {
return err
}
return nil
}
// ----------------------- Fiber App Binding ---------------------
func (r *MonitorRouter) bindRoutes(api *prime_api.IPrimeMonitorApi, privateKey string) {
v1 := r.app.Group("/api")
routes.RegisterDnsRoute(&v1, r.logger)
routes.RegisterFeatureFlags(&v1, r.logger, api, privateKey)
}
func (r *MonitorRouter) bindMiddlewares() {
r.app.Use(cache.New(cache.Config{
Next: func(c *fiber.Ctx) bool {
return c.Query("noCache") == "true"
},
Expiration: 30 * time.Minute,
CacheControl: true,
}))
r.app.Use(func(c *fiber.Ctx) error {
c.Accepts(fiber.MIMEApplicationJSONCharsetUTF8)
return c.Next()
})
r.app.Use(cors.New(cors.Config{
AllowOrigins: "*",
}))
r.app.Use(logger.New(logger.Config{
Output: dummyWriter{},
Done: func(c *fiber.Ctx, logString []byte) {
if c.Response().StatusCode() == fiber.StatusOK {
r.logger.Info(context.Background(), "[HTTP ROUTER] "+strings.ReplaceAll(string(logString), "\n", ""))
} else {
r.logger.Error(context.Background(), "[HTTP ROUTER] "+strings.ReplaceAll(string(logString), "\n", ""))
}
},
}))
}