diff --git a/Makefile b/Makefile index e5e3b1c..8685fa1 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,8 @@ LDFlags += -X 'github.com/MicroOps-cn/idas/pkg/utils/version.BuildDate=$(Bui LDFlags += -X 'github.com/MicroOps-cn/idas/pkg/utils/version.GoVersion=$(GoVersion)' LDFlags += -X 'github.com/MicroOps-cn/idas/pkg/utils/version.Platform=$(Platform)' LDFlags += -X 'github.com/MicroOps-cn/idas/pkg/utils/version.Version=$(Version).$(GitCommit)' +LDFlags += -X 'github.com/MicroOps-cn/idas/cmd/idas/cmd.HTTPBase=$(BASE_PATH)' +LDFlags += -X 'github.com/MicroOps-cn/idas/pkg/transport.RootPath=$(BASE_PATH)/api' info: @echo "Version: $(Version)" diff --git a/README.md b/README.md index 38a3847..deeb736 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,20 @@ Based on the Go language development, the overall use of the go kit framework, t ### How to use? #### Build ```bash -mkdir -p dist && make ui idas +mkdir -p dist && make ui idas BASE_PATH=/idas # The compiled output file is dist/idas ``` #### Initialization ```bash dist/idas init +# username/password: admin/idas ``` +!!! The default username for initialization creation is `admin`, and the password is `idas`. Please change it as soon as possible after logging in. #### Run ```bash cd dist && ./idas --security.secret=GzOWcPU86JWOzTRtEoiX ``` -!!! For security reasons, please change the value of the parameter -- security.secret. Please do not change the value of -- security.secret arbitrarily after initialization, as changing it may cause some encrypted historical data to be unable to be decrypted. +!!! For security reasons, please change the value of the parameter --security.secret. Please do not change the value of --security.secret arbitrarily after initialization, as changing it may cause some encrypted historical data to be unable to be decrypted. #### OAuth2 1. Create an Application (Grant Type: Authorization Code) diff --git a/cmd/idas/cmd/idas.go b/cmd/idas/cmd/idas.go index 97f41ef..29fb592 100644 --- a/cmd/idas/cmd/idas.go +++ b/cmd/idas/cmd/idas.go @@ -24,6 +24,7 @@ import ( stdlog "log" "net" "net/http" + "net/url" "os" "path" "strings" @@ -36,6 +37,7 @@ import ( "github.com/MicroOps-cn/fuck/log" "github.com/MicroOps-cn/fuck/log/flag" "github.com/MicroOps-cn/fuck/signals" + w "github.com/MicroOps-cn/fuck/wrapper" "github.com/go-kit/kit/metrics" "github.com/go-kit/kit/metrics/prometheus" kitlog "github.com/go-kit/log" @@ -65,6 +67,7 @@ var ( configDisplay bool debugAddr string httpExternalURL httputil.URL + HTTPBase string webPrefix string httpAddr string proxyHTTPAddr string @@ -277,13 +280,15 @@ func init() { rootCmd.Flags().StringVar(&openapiPath, "http.openapi-path", "", "path of openapi") rootCmd.Flags().StringVar(&swaggerPath, "http.swagger-path", "/apidocs/", "path of swagger ui. If the value is empty, the swagger UI is disabled.") rootCmd.Flags().Var(&httpExternalURL, "http.external-url", "The URL under which IDAS is externally reachable (for example, if IDAS is served via a reverse proxy). Used for generating relative and absolute links back to IDAS itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by IDAS. If omitted, relevant URL components will be derived automatically.") - rootCmd.Flags().StringVar(&webPrefix, "http.web-prefix", "/admin/", "The path prefix of the static page. The default is the path of http.external-url.") + rootCmd.Flags().StringVar(&webPrefix, "http.web-prefix", w.M(url.JoinPath(HTTPBase, "/admin/")), "The path prefix of the static page. The default is the path of http.external-url.") rootCmd.Flags().StringVar(&swaggerFilePath, "swagger.file-path", "", "path of swagger ui local file. If the value is empty, the swagger UI is disabled.") } func initParameter() { logger := log.NewTraceLogger() - + if httpExternalURL.String() == "" { + httpExternalURL.Path = HTTPBase + } if httpExternalURL.Scheme == "" { httpExternalURL.Scheme = "http" } diff --git a/pkg/transport/http.go b/pkg/transport/http.go index 8ff166a..a94283a 100644 --- a/pkg/transport/http.go +++ b/pkg/transport/http.go @@ -471,7 +471,7 @@ func NewSimpleWebService(rootPath string, doc string) *restful.WebService { return &webservice } -const rootPath = "/api" +var RootPath = "/api" func StructToQueryParams(obj interface{}, nameFilter ...string) []*restful.Parameter { var params []*restful.Parameter diff --git a/pkg/transport/http_api.go b/pkg/transport/http_api.go index 8edbda1..6962eae 100644 --- a/pkg/transport/http_api.go +++ b/pkg/transport/http_api.go @@ -48,7 +48,7 @@ var apiServiceSet = []func(ctx context.Context, options []httptransport.ServerOp func UserService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "users", Description: "Managing users"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -134,7 +134,7 @@ func UserService(ctx context.Context, options []httptransport.ServerOption, endp func AppService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "apps", Description: "Application manager"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET("/icons"). @@ -247,7 +247,7 @@ func AppService(ctx context.Context, options []httptransport.ServerOption, endpo func FileService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "files", Description: "Managing files"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.POST(""). @@ -274,7 +274,7 @@ func FileService(ctx context.Context, options []httptransport.ServerOption, endp func PageService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "pages", Description: "Managing pages"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -389,7 +389,7 @@ func PageService(ctx context.Context, options []httptransport.ServerOption, endp func SessionService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "sessions", Description: "Managing sessions"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -414,7 +414,7 @@ func SessionService(ctx context.Context, options []httptransport.ServerOption, e func OAuthService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "oauth", Description: "OAuth2.0 Support"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) // https://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html v1ws.Route(v1ws.POST("/token"). @@ -482,7 +482,7 @@ func OAuthService(ctx context.Context, options []httptransport.ServerOption, end func CurrentUserService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "user", Description: "Current user service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.POST("/login"). @@ -653,7 +653,7 @@ func CurrentUserService(ctx context.Context, options []httptransport.ServerOptio func PermissionService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "permissions", Description: "permissions service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -670,7 +670,7 @@ func PermissionService(ctx context.Context, options []httptransport.ServerOption func RoleService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "roles", Description: "role service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -722,7 +722,7 @@ func RoleService(ctx context.Context, options []httptransport.ServerOption, endp func ConfigService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "config", Description: "config service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET("security"). @@ -747,7 +747,7 @@ func ConfigService(ctx context.Context, options []httptransport.ServerOption, en func EventService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "events", Description: "event service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET(""). @@ -774,7 +774,7 @@ func EventService(ctx context.Context, options []httptransport.ServerOption, end func GlobalService(ctx context.Context, options []httptransport.ServerOption, endpoints endpoint.Set) (spec.Tag, []*restful.WebService) { tag := spec.Tag{TagProps: spec.TagProps{Name: "global", Description: "Global service"}} tags := []string{tag.Name} - v1ws := NewWebService(rootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) + v1ws := NewWebService(RootPath, schema.GroupVersion{Group: tag.Name, Version: "v1"}, tag.Description) v1ws.Filter(HTTPAuthenticationFilter(endpoints)) v1ws.Route(v1ws.GET("config").