Skip to content

Commit

Permalink
optimize: http base path
Browse files Browse the repository at this point in the history
Signed-off-by: 孙林耀 <[email protected]>
  • Loading branch information
MicroOps-cn committed Sep 24, 2024
1 parent a792f22 commit 98e1e47
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions cmd/idas/cmd/idas.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
stdlog "log"
"net"
"net/http"
"net/url"
"os"
"path"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -65,6 +67,7 @@ var (
configDisplay bool
debugAddr string
httpExternalURL httputil.URL
HTTPBase string
webPrefix string
httpAddr string
proxyHTTPAddr string
Expand Down Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/transport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions pkg/transport/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("").
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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("").
Expand All @@ -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("").
Expand Down Expand Up @@ -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("").
Expand All @@ -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").
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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("").
Expand All @@ -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("").
Expand Down Expand Up @@ -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").
Expand All @@ -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("").
Expand All @@ -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").
Expand Down

0 comments on commit 98e1e47

Please sign in to comment.