github.com/navikt/knorten@v0.0.0-20240419132333-1333f46ed8b6/pkg/api/api.go (about)

     1  package api
     2  
     3  import (
     4  	"github.com/gin-gonic/gin"
     5  	"github.com/navikt/knorten/pkg/api/auth"
     6  	"github.com/navikt/knorten/pkg/database"
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  type client struct {
    11  	azureClient *auth.Azure
    12  	router      *gin.Engine
    13  	repo        *database.Repo
    14  	log         *logrus.Entry
    15  	dryRun      bool
    16  	gcpProject  string
    17  	gcpZone     string
    18  }
    19  
    20  func New(router *gin.Engine, db *database.Repo, azureClient *auth.Azure, log *logrus.Entry, dryRun bool, project, zone string) error {
    21  	router.Use(gin.Recovery())
    22  	router.Use(func(ctx *gin.Context) {
    23  		log.WithField("subsystem", "gin").Infof("%v %v %v", ctx.Request.Method, ctx.Request.URL.Path, ctx.Writer.Status())
    24  	})
    25  
    26  	api := client{
    27  		azureClient: azureClient,
    28  		router:      router,
    29  		repo:        db,
    30  		log:         log,
    31  		dryRun:      dryRun,
    32  		gcpProject:  project,
    33  		gcpZone:     zone,
    34  	}
    35  
    36  	api.setupAuthenticatedRoutes()
    37  	api.router.Use(api.adminAuthMiddleware())
    38  	api.setupAdminRoutes()
    39  
    40  	return nil
    41  }
    42  
    43  func (c *client) setupAuthenticatedRoutes() {
    44  	c.setupUserRoutes()
    45  	c.setupTeamRoutes()
    46  	c.setupComputeRoutes()
    47  	c.setupSecretRoutes()
    48  	c.setupChartRoutes()
    49  }