github.com/weaviate/weaviate@v1.24.6/adapters/handlers/rest/configure_server.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 "context" 16 "net/http" 17 "os" 18 "time" 19 20 "github.com/sirupsen/logrus" 21 "github.com/weaviate/weaviate/adapters/handlers/graphql" 22 "github.com/weaviate/weaviate/adapters/handlers/graphql/utils" 23 "github.com/weaviate/weaviate/adapters/handlers/rest/state" 24 "github.com/weaviate/weaviate/entities/schema" 25 "github.com/weaviate/weaviate/usecases/auth/authentication/anonymous" 26 "github.com/weaviate/weaviate/usecases/auth/authentication/apikey" 27 "github.com/weaviate/weaviate/usecases/auth/authentication/oidc" 28 "github.com/weaviate/weaviate/usecases/auth/authorization" 29 "github.com/weaviate/weaviate/usecases/config" 30 "github.com/weaviate/weaviate/usecases/modules" 31 "github.com/weaviate/weaviate/usecases/traverser" 32 ) 33 34 // As soon as server is initialized but not run yet, this function will be called. 35 // If you need to modify a config, store server instance to stop it individually later, this is the place. 36 // This function can be called multiple times, depending on the number of serving schemes. 37 // scheme value will be set accordingly: "http", "https" or "unix" 38 // 39 // we will set it through configureAPI() as it needs access to resources that 40 // are only available within there 41 var configureServer func(*http.Server, string, string) 42 43 func makeUpdateSchemaCall(logger logrus.FieldLogger, appState *state.State, traverser *traverser.Traverser) func(schema.Schema) { 44 return func(updatedSchema schema.Schema) { 45 if appState.ServerConfig.Config.DisableGraphQL { 46 return 47 } 48 49 // Note that this is thread safe; we're running in a single go-routine, because the event 50 // handlers are called when the SchemaLock is still held. 51 52 gql, err := rebuildGraphQL( 53 updatedSchema, 54 logger, 55 appState.ServerConfig.Config, 56 traverser, 57 appState.Modules, 58 ) 59 if err != nil && err != utils.ErrEmptySchema { 60 logger.WithField("action", "graphql_rebuild"). 61 WithError(err).Error("could not (re)build graphql provider") 62 } 63 appState.SetGraphQL(gql) 64 } 65 } 66 67 func rebuildGraphQL(updatedSchema schema.Schema, logger logrus.FieldLogger, 68 config config.Config, traverser *traverser.Traverser, modulesProvider *modules.Provider, 69 ) (graphql.GraphQL, error) { 70 updatedGraphQL, err := graphql.Build(&updatedSchema, traverser, logger, config, modulesProvider) 71 if err != nil { 72 return nil, err 73 } 74 75 logger.WithField("action", "graphql_rebuild").Debug("successfully rebuild graphql schema") 76 return updatedGraphQL, nil 77 } 78 79 // configureOIDC will always be called, even if OIDC is disabled, this way the 80 // middleware will still be able to provide the user with a valuable error 81 // message, even when OIDC is globally disabled. 82 func configureOIDC(appState *state.State) *oidc.Client { 83 c, err := oidc.New(appState.ServerConfig.Config) 84 if err != nil { 85 appState.Logger.WithField("action", "oidc_init").WithError(err).Fatal("oidc client could not start up") 86 os.Exit(1) 87 } 88 89 return c 90 } 91 92 func configureAPIKey(appState *state.State) *apikey.Client { 93 c, err := apikey.New(appState.ServerConfig.Config) 94 if err != nil { 95 appState.Logger.WithField("action", "oidc_init").WithError(err).Fatal("oidc client could not start up") 96 os.Exit(1) 97 } 98 99 return c 100 } 101 102 // configureAnonymousAccess will always be called, even if anonymous access is 103 // disabled. In this case the middleware provided by this client will block 104 // anonymous requests 105 func configureAnonymousAccess(appState *state.State) *anonymous.Client { 106 return anonymous.New(appState.ServerConfig.Config) 107 } 108 109 func configureAuthorizer(appState *state.State) authorization.Authorizer { 110 return authorization.New(appState.ServerConfig.Config) 111 } 112 113 func timeTillDeadline(ctx context.Context) string { 114 dl, _ := ctx.Deadline() 115 return time.Until(dl).String() 116 }