github.com/weaviate/weaviate@v1.24.6/adapters/handlers/rest/handlers_misc.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package rest
    13  
    14  import (
    15  	"fmt"
    16  	"net/url"
    17  
    18  	middleware "github.com/go-openapi/runtime/middleware"
    19  	"github.com/sirupsen/logrus"
    20  	"github.com/weaviate/weaviate/adapters/handlers/rest/operations"
    21  	"github.com/weaviate/weaviate/adapters/handlers/rest/operations/meta"
    22  	"github.com/weaviate/weaviate/adapters/handlers/rest/operations/well_known"
    23  	"github.com/weaviate/weaviate/entities/models"
    24  	"github.com/weaviate/weaviate/entities/schema"
    25  	"github.com/weaviate/weaviate/usecases/config"
    26  	"github.com/weaviate/weaviate/usecases/monitoring"
    27  )
    28  
    29  type schemaManager interface {
    30  	GetSchema(principal *models.Principal) (schema.Schema, error)
    31  	GetSchemaSkipAuth() schema.Schema
    32  }
    33  
    34  func setupMiscHandlers(api *operations.WeaviateAPI, serverConfig *config.WeaviateConfig,
    35  	schemaManager schemaManager, modulesProvider ModulesProvider, metrics *monitoring.PrometheusMetrics, logger logrus.FieldLogger,
    36  ) {
    37  	metricRequestsTotal := newMiscRequestsTotal(metrics, logger)
    38  	api.MetaMetaGetHandler = meta.MetaGetHandlerFunc(func(params meta.MetaGetParams, principal *models.Principal) middleware.Responder {
    39  		var (
    40  			metaInfos = map[string]interface{}{}
    41  			err       error
    42  		)
    43  
    44  		if modulesProvider != nil {
    45  			metaInfos, err = modulesProvider.GetMeta()
    46  			if err != nil {
    47  				metricRequestsTotal.logError("", err)
    48  				return meta.NewMetaGetInternalServerError().WithPayload(errPayloadFromSingleErr(err))
    49  			}
    50  		}
    51  
    52  		res := &models.Meta{
    53  			Hostname: serverConfig.GetHostAddress(),
    54  			Version:  config.ServerVersion,
    55  			Modules:  metaInfos,
    56  		}
    57  		metricRequestsTotal.logOk("")
    58  		return meta.NewMetaGetOK().WithPayload(res)
    59  	})
    60  
    61  	api.WellKnownGetWellKnownOpenidConfigurationHandler = well_known.GetWellKnownOpenidConfigurationHandlerFunc(
    62  		func(params well_known.GetWellKnownOpenidConfigurationParams, principal *models.Principal) middleware.Responder {
    63  			if !serverConfig.Config.Authentication.OIDC.Enabled {
    64  				metricRequestsTotal.logUserError("")
    65  				return well_known.NewGetWellKnownOpenidConfigurationNotFound()
    66  			}
    67  
    68  			target, err := url.JoinPath(serverConfig.Config.Authentication.OIDC.Issuer, "/.well-known/openid-configuration")
    69  			if err != nil {
    70  				metricRequestsTotal.logError("", err)
    71  				return well_known.NewGetWellKnownOpenidConfigurationInternalServerError().WithPayload(errPayloadFromSingleErr(err))
    72  			}
    73  			clientID := serverConfig.Config.Authentication.OIDC.ClientID
    74  			scopes := serverConfig.Config.Authentication.OIDC.Scopes
    75  			body := &well_known.GetWellKnownOpenidConfigurationOKBody{
    76  				Href:     target,
    77  				ClientID: clientID,
    78  				Scopes:   scopes,
    79  			}
    80  
    81  			metricRequestsTotal.logOk("")
    82  			return well_known.NewGetWellKnownOpenidConfigurationOK().WithPayload(body)
    83  		})
    84  
    85  	api.WeaviateRootHandler = operations.WeaviateRootHandlerFunc(
    86  		func(params operations.WeaviateRootParams, principal *models.Principal) middleware.Responder {
    87  			origin := serverConfig.Config.Origin
    88  			body := &operations.WeaviateRootOKBody{
    89  				Links: []*models.Link{
    90  					{
    91  						Name: "Meta information about this instance/cluster",
    92  						Href: fmt.Sprintf("%s/v1/meta", origin),
    93  					},
    94  					{
    95  						Name:              "view complete schema",
    96  						Href:              fmt.Sprintf("%s/v1/schema", origin),
    97  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/schema",
    98  					},
    99  					{
   100  						Name:              "CRUD schema",
   101  						Href:              fmt.Sprintf("%s/v1/schema{/:className}", origin),
   102  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/schema",
   103  					},
   104  					{
   105  						Name:              "CRUD objects",
   106  						Href:              fmt.Sprintf("%s/v1/objects{/:id}", origin),
   107  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/objects",
   108  					},
   109  					{
   110  						Name:              "trigger and view status of classifications",
   111  						Href:              fmt.Sprintf("%s/v1/classifications{/:id}", origin),
   112  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/classification,https://weaviate.io/developers/weaviate/api/rest/classification#knn-classification",
   113  					},
   114  					{
   115  						Name:              "check if Weaviate is live (returns 200 on GET when live)",
   116  						Href:              fmt.Sprintf("%s/v1/.well-known/live", origin),
   117  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/well-known#liveness",
   118  					},
   119  					{
   120  						Name:              "check if Weaviate is ready (returns 200 on GET when ready)",
   121  						Href:              fmt.Sprintf("%s/v1/.well-known/ready", origin),
   122  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/well-known#readiness",
   123  					},
   124  					{
   125  						Name:              "view link to openid configuration (returns 404 on GET if no openid is configured)",
   126  						Href:              fmt.Sprintf("%s/v1/.well-known/openid-configuration", origin),
   127  						DocumentationHref: "https://weaviate.io/developers/weaviate/api/rest/well-known#openid-configuration",
   128  					},
   129  				},
   130  			}
   131  
   132  			metricRequestsTotal.logOk("")
   133  			return operations.NewWeaviateRootOK().WithPayload(body)
   134  		})
   135  }
   136  
   137  type miscRequestsTotal struct {
   138  	*restApiRequestsTotalImpl
   139  }
   140  
   141  func newMiscRequestsTotal(metrics *monitoring.PrometheusMetrics, logger logrus.FieldLogger) restApiRequestsTotal {
   142  	return &miscRequestsTotal{
   143  		restApiRequestsTotalImpl: &restApiRequestsTotalImpl{newRequestsTotalMetric(metrics, "rest"), "rest", "misc", logger},
   144  	}
   145  }
   146  
   147  func (e *miscRequestsTotal) logError(className string, err error) {
   148  	switch err.(type) {
   149  	default:
   150  		e.logServerError(className, err)
   151  	}
   152  }