cmd

package
v0.0.0-...-00b4b05 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			api := s.GetOpenApi()
			oauth := goai.SecurityRequirement{
				"idxauth": {"idxauth"},
			}
			api.Security = &goai.SecurityRequirements{
				oauth,
			}

			api.Components.SecuritySchemes = goai.SecuritySchemes{
				"idxauth": goai.SecuritySchemeRef{
					Value: &goai.SecurityScheme{Type: "oauth",
						OpenIdConnectUrl: "http://localhost:9097/idx/",
						Flows: &goai.OAuthFlows{AuthorizationCode: &goai.OAuthFlow{
							AuthorizationURL: "/oauth2/authorize",
							TokenURL:         "/idx/oauth2/token",
							Scopes: map[string]string{
								"admin": "admin",
							},
						}}},
				},
			}

			s.Group("/api/v1", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Middleware(middleware.OidcAuth)
				group.Group("/oauth2", func(group *ghttp.RouterGroup) {
					group.Bind(
						client.NewV1(),
						scope.NewV1(),
					)
				})
				group.Group("/system", func(group *ghttp.RouterGroup) {
					group.Bind(
						user.NewV1(),
						orgunit.NewV1(),
						common.NewV1(),
						role.NewV1(),
						ggroup.NewV1(),
					)
				})
			})
			options := conf.GetOptions(ctx)
			if options.EnabledUI {
				s.AddStaticPath("/ui", options.UIPath)

				s.AddStaticPath("/ui", options.UIPath)
				s.BindHandler("GET:/ui/*", func(r *ghttp.Request) {
					ext := path.Ext(r.URL.Path)
					if strings.EqualFold(ext, "") {
						r.Response.ServeFile(path.Join(options.UIPath, "index.html"))
					}

				})
				s.BindHandler("GET:/", func(r *ghttp.Request) {
					r.Response.RedirectTo("/ui")
				})
			}

			s.Run()
			return nil
		},
	}
)
View Source
var (
	Preset = gcmd.Command{
		Name:  "preset",
		Usage: "preset",
		Brief: "Initialize the preset client and user",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			g.Log().Info(ctx, "Begin to initialize preset data...")
			ctx = context.WithValue(ctx, consts.SUBJECT_KEY, "1838872840128958464")
			ctx = context.WithValue(ctx, consts.ACCOUNT_KEY, "admin")

			admin := &entity.Users{
				Id:       1838872840128958464,
				UserName: "admin",

				Email:       "admin@idx.local",
				PhoneNumber: "123456789",
			}

			service.User().Save(ctx, &v1.SaveReq{
				Users: *admin,
			})
			idStr := strconv.Itoa(int(admin.Id))
			newPwd, err := service.User().ResetPwd(ctx, &v1.ResetPwdReq{Id: idStr})
			if err != nil {
				g.Log().Error(ctx, err)
				panic(err)
			}
			g.Log().Info(ctx, "admin password:", newPwd)
			adminui := &entity.Clients{
				Id:                 1838872840128958465,
				ClientName:         "IDX Admin UI",
				ClientId:           "adminui",
				Enabled:            true,
				GrantTypes:         "authorization_code",
				ClientUri:          "http://localhost:8090/",
				Scopes:             "openid profile email admin",
				Description:        "IDX Admin UI",
				RedirectUris:       "http://localhost:*/ui/auth/callback http://localhost:5666/auth/callback http://localhost:*/auth/callback http://localhost/ui/auth/callback",
				RequireSecret:      false,
				RequirePkce:        true,
				AllowPlainTextPkce: false,
			}
			err = service.Client().Save(ctx, &clientReqV1.SaveReq{
				Clients: *adminui,
			})
			if err != nil {
				g.Log().Error(ctx, "create admin ui client error", "error", err)
				panic(err)
			}
			req := &clientReqV1.SetCORSReq{}
			req.ClientId = adminui.Id
			req.Origins = []string{"*", "http://localhost"}
			err = service.Client().SetCORS(ctx, req)
			if err != nil {
				g.Log().Error(ctx, "create admin ui client cors error", "error", err)
				panic(err)
			}

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL