code.gitea.io/gitea@v1.22.3/routers/init.go (about) 1 // Copyright 2016 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package routers 5 6 import ( 7 "context" 8 "net/http" 9 "reflect" 10 "runtime" 11 12 "code.gitea.io/gitea/models" 13 authmodel "code.gitea.io/gitea/models/auth" 14 "code.gitea.io/gitea/modules/cache" 15 "code.gitea.io/gitea/modules/eventsource" 16 "code.gitea.io/gitea/modules/git" 17 "code.gitea.io/gitea/modules/highlight" 18 "code.gitea.io/gitea/modules/log" 19 "code.gitea.io/gitea/modules/markup" 20 "code.gitea.io/gitea/modules/markup/external" 21 "code.gitea.io/gitea/modules/setting" 22 "code.gitea.io/gitea/modules/ssh" 23 "code.gitea.io/gitea/modules/storage" 24 "code.gitea.io/gitea/modules/svg" 25 "code.gitea.io/gitea/modules/system" 26 "code.gitea.io/gitea/modules/templates" 27 "code.gitea.io/gitea/modules/translation" 28 "code.gitea.io/gitea/modules/util" 29 "code.gitea.io/gitea/modules/web" 30 "code.gitea.io/gitea/modules/web/routing" 31 actions_router "code.gitea.io/gitea/routers/api/actions" 32 packages_router "code.gitea.io/gitea/routers/api/packages" 33 apiv1 "code.gitea.io/gitea/routers/api/v1" 34 "code.gitea.io/gitea/routers/common" 35 "code.gitea.io/gitea/routers/private" 36 web_routers "code.gitea.io/gitea/routers/web" 37 actions_service "code.gitea.io/gitea/services/actions" 38 asymkey_service "code.gitea.io/gitea/services/asymkey" 39 "code.gitea.io/gitea/services/auth" 40 "code.gitea.io/gitea/services/auth/source/oauth2" 41 "code.gitea.io/gitea/services/automerge" 42 "code.gitea.io/gitea/services/cron" 43 feed_service "code.gitea.io/gitea/services/feed" 44 indexer_service "code.gitea.io/gitea/services/indexer" 45 "code.gitea.io/gitea/services/mailer" 46 mailer_incoming "code.gitea.io/gitea/services/mailer/incoming" 47 markup_service "code.gitea.io/gitea/services/markup" 48 repo_migrations "code.gitea.io/gitea/services/migrations" 49 mirror_service "code.gitea.io/gitea/services/mirror" 50 pull_service "code.gitea.io/gitea/services/pull" 51 release_service "code.gitea.io/gitea/services/release" 52 repo_service "code.gitea.io/gitea/services/repository" 53 "code.gitea.io/gitea/services/repository/archiver" 54 "code.gitea.io/gitea/services/task" 55 "code.gitea.io/gitea/services/uinotification" 56 "code.gitea.io/gitea/services/webhook" 57 ) 58 59 func mustInit(fn func() error) { 60 err := fn() 61 if err != nil { 62 ptr := reflect.ValueOf(fn).Pointer() 63 fi := runtime.FuncForPC(ptr) 64 log.Fatal("%s failed: %v", fi.Name(), err) 65 } 66 } 67 68 func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) { 69 err := fn(ctx) 70 if err != nil { 71 ptr := reflect.ValueOf(fn).Pointer() 72 fi := runtime.FuncForPC(ptr) 73 log.Fatal("%s(ctx) failed: %v", fi.Name(), err) 74 } 75 } 76 77 func syncAppConfForGit(ctx context.Context) error { 78 runtimeState := new(system.RuntimeState) 79 if err := system.AppState.Get(ctx, runtimeState); err != nil { 80 return err 81 } 82 83 updated := false 84 if runtimeState.LastAppPath != setting.AppPath { 85 log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath) 86 runtimeState.LastAppPath = setting.AppPath 87 updated = true 88 } 89 if runtimeState.LastCustomConf != setting.CustomConf { 90 log.Info("CustomConf changed from '%s' to '%s'", runtimeState.LastCustomConf, setting.CustomConf) 91 runtimeState.LastCustomConf = setting.CustomConf 92 updated = true 93 } 94 95 if updated { 96 log.Info("re-sync repository hooks ...") 97 mustInitCtx(ctx, repo_service.SyncRepositoryHooks) 98 99 log.Info("re-write ssh public keys ...") 100 mustInitCtx(ctx, asymkey_service.RewriteAllPublicKeys) 101 102 return system.AppState.Set(ctx, runtimeState) 103 } 104 return nil 105 } 106 107 func InitWebInstallPage(ctx context.Context) { 108 translation.InitLocales(ctx) 109 setting.LoadSettingsForInstall() 110 mustInit(svg.Init) 111 } 112 113 // InitWebInstalled is for global installed configuration. 114 func InitWebInstalled(ctx context.Context) { 115 mustInitCtx(ctx, git.InitFull) 116 log.Info("Git version: %s (home: %s)", git.DefaultFeatures().VersionInfo(), git.HomeDir()) 117 if !git.DefaultFeatures().SupportHashSha256 { 118 log.Warn("sha256 hash support is disabled - requires Git >= 2.42." + util.Iif(git.DefaultFeatures().UsingGogit, " Gogit is currently unsupported.", "")) 119 } 120 121 // Setup i18n 122 translation.InitLocales(ctx) 123 124 setting.LoadSettings() 125 mustInit(storage.Init) 126 127 mailer.NewContext(ctx) 128 mustInit(cache.Init) 129 mustInit(feed_service.Init) 130 mustInit(uinotification.Init) 131 mustInitCtx(ctx, archiver.Init) 132 133 highlight.NewContext() 134 external.RegisterRenderers() 135 markup.Init(markup_service.ProcessorHelper()) 136 137 if setting.EnableSQLite3 { 138 log.Info("SQLite3 support is enabled") 139 } else if setting.Database.Type.IsSQLite3() { 140 log.Fatal("SQLite3 support is disabled, but it is used for database setting. Please get or build a Gitea release with SQLite3 support.") 141 } 142 143 mustInitCtx(ctx, common.InitDBEngine) 144 log.Info("ORM engine initialization successful!") 145 mustInit(system.Init) 146 mustInitCtx(ctx, oauth2.Init) 147 148 mustInit(release_service.Init) 149 150 mustInitCtx(ctx, models.Init) 151 mustInitCtx(ctx, authmodel.Init) 152 mustInitCtx(ctx, repo_service.Init) 153 154 // Booting long running goroutines. 155 mustInit(indexer_service.Init) 156 157 mirror_service.InitSyncMirrors() 158 mustInit(webhook.Init) 159 mustInit(pull_service.Init) 160 mustInit(automerge.Init) 161 mustInit(task.Init) 162 mustInit(repo_migrations.Init) 163 eventsource.GetManager().Init() 164 mustInitCtx(ctx, mailer_incoming.Init) 165 166 mustInitCtx(ctx, syncAppConfForGit) 167 168 mustInit(ssh.Init) 169 170 auth.Init() 171 mustInit(svg.Init) 172 173 actions_service.Init() 174 175 // Finally start up the cron 176 cron.NewContext(ctx) 177 } 178 179 // NormalRoutes represents non install routes 180 func NormalRoutes() *web.Route { 181 _ = templates.HTMLRenderer() 182 r := web.NewRoute() 183 r.Use(common.ProtocolMiddlewares()...) 184 185 r.Mount("/", web_routers.Routes()) 186 r.Mount("/api/v1", apiv1.Routes()) 187 r.Mount("/api/internal", private.Routes()) 188 189 r.Post("/-/fetch-redirect", common.FetchRedirectDelegate) 190 191 if setting.Packages.Enabled { 192 // This implements package support for most package managers 193 r.Mount("/api/packages", packages_router.CommonRoutes()) 194 // This implements the OCI API (Note this is not preceded by /api but is instead /v2) 195 r.Mount("/v2", packages_router.ContainerRoutes()) 196 } 197 198 if setting.Actions.Enabled { 199 prefix := "/api/actions" 200 r.Mount(prefix, actions_router.Routes(prefix)) 201 202 // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now. 203 // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/ 204 // TODO: this prefix should be generated with a token string with runner ? 205 prefix = "/api/actions_pipeline" 206 r.Mount(prefix, actions_router.ArtifactsRoutes(prefix)) 207 prefix = actions_router.ArtifactV4RouteBase 208 r.Mount(prefix, actions_router.ArtifactsV4Routes(prefix)) 209 } 210 211 r.NotFound(func(w http.ResponseWriter, req *http.Request) { 212 routing.UpdateFuncInfo(req.Context(), routing.GetFuncInfo(http.NotFound, "GlobalNotFound")) 213 http.NotFound(w, req) 214 }) 215 return r 216 }