From f5a9a7934f3624763fce2094d291fbb97b96ddd1 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 21 Dec 2021 17:09:29 +0700 Subject: all: add feature to register custom navigation link Using Trunks.RegisterNavLink, one can register custom link into left navigation menu. --- http_server.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'http_server.go') diff --git a/http_server.go b/http_server.go index e83e9a3..994872e 100644 --- a/http_server.go +++ b/http_server.go @@ -35,6 +35,13 @@ var ( ResponseType: libhttp.ResponseTypeJSON, } + apiNavLinks = &libhttp.Endpoint{ + Method: libhttp.RequestMethodGet, + Path: "/_trunks/api/navlinks", + RequestType: libhttp.RequestTypeNone, + ResponseType: libhttp.ResponseTypeJSON, + } + apiTargetRunHttp = &libhttp.Endpoint{ Method: libhttp.RequestMethodPost, Path: "/_trunks/api/target/run/http", @@ -59,14 +66,23 @@ var ( func (trunks *Trunks) initHttpServer(isDevelopment bool) (err error) { logp := "initHttpServer" - httpdOpts := &libhttp.ServerOptions{ - Options: memfs.Options{ + if memfsWWW == nil { + mfsOptions := memfs.Options{ Root: "_www", Includes: []string{ `.*\.(js|html|ico|png)$`, }, - Development: isDevelopment, - }, + Development: true, + } + memfsWWW, err = memfs.New(&mfsOptions) + if err != nil { + return fmt.Errorf("%s: %w", logp, err) + } + } else { + memfsWWW.Opts.Development = isDevelopment + } + + httpdOpts := &libhttp.ServerOptions{ Memfs: memfsWWW, Address: trunks.Env.ListenAddress, } @@ -93,6 +109,12 @@ func (trunks *Trunks) initHttpServer(isDevelopment bool) (err error) { return fmt.Errorf("%s: %w", logp, err) } + apiNavLinks.Call = trunks.apiNavLinks + err = trunks.Httpd.RegisterEndpoint(apiNavLinks) + if err != nil { + return fmt.Errorf("%s: %w", logp, err) + } + apiTargetRunHttp.Call = trunks.apiTargetRunHttp err = trunks.Httpd.RegisterEndpoint(apiTargetRunHttp) if err != nil { @@ -168,6 +190,13 @@ func (trunks *Trunks) apiAttackResultGet(epr *libhttp.EndpointRequest) (resbody return json.Marshal(&res) } +func (trunks *Trunks) apiNavLinks(epr *libhttp.EndpointRequest) (resbody []byte, err error) { + res := libhttp.EndpointResponse{} + res.Code = http.StatusOK + res.Data = trunks.navLinks + return json.Marshal(&res) +} + func (trunks *Trunks) apiTargetRunHttp(epr *libhttp.EndpointRequest) ([]byte, error) { req := &RunRequest{} err := json.Unmarshal(epr.RequestBody, req) -- cgit v1.3