@@ -31,7 +31,6 @@ import (
3131 "encoding/json"
3232 "flag"
3333 "fmt"
34- "io/ioutil"
3534 "net/http"
3635 "net/url"
3736 "os"
@@ -129,6 +128,7 @@ type CommandLineFlags struct {
129128 LogPathWebApp * string `json:"path_log_webapp"`
130129 LogName * string `json:"log_name_webapp"`
131130 APIPrefix * string `json:"api_prefix"`
131+ ApplyPrefixToIndex * bool `json:"apply_prefix_to_index"`
132132 WatchConfig * bool `json:"watch_config"`
133133 ShowCurrentConfig * bool `json:"show_current_config"`
134134 GenerateJwtSecret * bool `json:"generate_jwt_secret"`
@@ -194,6 +194,7 @@ func initFlags() {
194194 appFlags .LogName = flag .String ("webapp-log-name" , "" , "the name prefix of the log file." )
195195 appFlags .LogPathWebApp = flag .String ("webapp-log-path" , "" , "the path for the log file." )
196196 appFlags .APIPrefix = flag .String ("webapp-api-prefix" , "" , "API prefix." )
197+ appFlags .ApplyPrefixToIndex = flag .Bool ("apply-prefix-to-index" , false , "Apply API/Html prefix to index page" )
197198 appFlags .WatchConfig = flag .Bool ("watch-config" , false , "Watch the configuration for changes" )
198199 appFlags .ShowCurrentConfig = flag .Bool ("show-current-config" , false , "print out the current config and exit" )
199200
@@ -284,6 +285,14 @@ func main() {
284285 os .Exit (0 )
285286 }
286287
288+ if * appFlags .APIPrefix != "" {
289+ config .Setting .MAIN_SETTINGS .APIPrefix = * appFlags .APIPrefix
290+ }
291+
292+ if * appFlags .ApplyPrefixToIndex || config .Setting .MAIN_SETTINGS .ApplyPrefixIndexHtml {
293+ heputils .ApplyPrefixIndexHtml (* appFlags .APIPrefix , config .Setting .MAIN_SETTINGS .RootPath )
294+ }
295+
287296 // configure to serve WebServices
288297 configureAsHTTPServer ()
289298
@@ -496,6 +505,20 @@ func configureServiceObjects() {
496505 config .Setting .MAIN_SETTINGS .TimeoutHttpClient = viper .GetUint32 ("http_client.connection_timeout" )
497506 }
498507
508+ if viper .IsSet ("hhttp_settings.api_prefix" ) {
509+ config .Setting .MAIN_SETTINGS .APIPrefix = viper .GetString ("http_settings.api_prefix" )
510+ }
511+
512+ /***********************************/
513+ if viper .IsSet ("http_settings.apply_prefix_to_index" ) {
514+ config .Setting .MAIN_SETTINGS .ApplyPrefixIndexHtml = viper .GetBool ("http_settings.apply_prefix_to_index" )
515+ }
516+
517+ /***********************************/
518+ if viper .IsSet ("http_settings.root" ) {
519+ config .Setting .MAIN_SETTINGS .RootPath = viper .GetString ("http_settings.root" )
520+ }
521+
499522 /* check the auth type */
500523 if config .Setting .MAIN_SETTINGS .DefaultAuth == "" {
501524 config .Setting .MAIN_SETTINGS .DefaultAuth = "internal"
@@ -736,7 +759,7 @@ func configureAsHTTPServer() {
736759 e .GET ("/doc/api/json" , func (c echo.Context ) error {
737760
738761 logger .Debug ("Middle swagger ware: " , c .Request ().RequestURI )
739- dataJson , err := ioutil .ReadFile (config .Setting .SWAGGER .ApiJson )
762+ dataJson , err := os .ReadFile (config .Setting .SWAGGER .ApiJson )
740763 if err != nil {
741764 return httpresponse .CreateBadResponse (& c , http .StatusBadRequest , webmessages .SwaggerFileNotExistsError )
742765 }
@@ -748,13 +771,17 @@ func configureAsHTTPServer() {
748771 }
749772
750773 /* static */
751- rootPath := viper .GetString ("http_settings.root" )
752- if rootPath == "" {
753- rootPath = "/usr/local/homer/dist"
774+ //Api Prefix
775+ if config .Setting .MAIN_SETTINGS .APIPrefix != "" {
776+ groupPrefix := e .Group (config .Setting .MAIN_SETTINGS .APIPrefix )
777+ groupPrefix .Use (middleware .StaticWithConfig (middleware.StaticConfig {
778+ Root : config .Setting .MAIN_SETTINGS .RootPath ,
779+ HTML5 : true ,
780+ }))
754781 }
755782
756783 /* static */
757- e .Use (middleware .Static (rootPath ))
784+ e .Use (middleware .Static (config . Setting . MAIN_SETTINGS . RootPath ))
758785
759786 /* enable guzip*/
760787 if gzipEnable := viper .GetBool ("http_settings.gzip" ); gzipEnable {
@@ -781,7 +808,13 @@ func configureAsHTTPServer() {
781808 Skipper : func (c echo.Context ) bool {
782809
783810 if strings .HasSuffix (c .Request ().RequestURI , ".js" ) {
784- if heputils .FileExists (rootPath + c .Request ().RequestURI + ".gz" ) {
811+
812+ //Prefix
813+ if * appFlags .APIPrefix != "" && strings .HasPrefix (c .Request ().RequestURI , * appFlags .APIPrefix ) {
814+ c .Request ().RequestURI = strings .TrimPrefix (c .Request ().RequestURI , * appFlags .APIPrefix )
815+ }
816+
817+ if heputils .FileExists (config .Setting .MAIN_SETTINGS .RootPath + c .Request ().RequestURI + ".gz" ) {
785818 c .Response ().Header ().Set (echo .HeaderContentEncoding , "gzip" )
786819 c .Response ().Header ().Set (echo .HeaderContentType , echo .MIMEApplicationJavaScript )
787820 return false
@@ -796,7 +829,7 @@ func configureAsHTTPServer() {
796829 }))
797830 }
798831
799- registerGetRedirect (e , rootPath )
832+ registerGetRedirect (e , config . Setting . MAIN_SETTINGS . RootPath )
800833
801834 /* decoder */
802835 servicesObject .externalDecoder .Active = false
@@ -1775,7 +1808,7 @@ func sendIndexHtml(c echo.Context, path string) error {
17751808 return c .File (path + "/index.html" )
17761809 }
17771810
1778- content , err := ioutil .ReadFile (path + "/index.html" )
1811+ content , err := os .ReadFile (path + "/index.html" )
17791812 if err != nil {
17801813 logger .Debug ("not found...." , err .Error ())
17811814 return c .String (http .StatusNotFound , "Not found" )
0 commit comments