github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_server_config_static.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Static Searching Priority: Resource > ServerPaths > ServerRoot > SearchPath
     8  
     9  package ghttp
    10  
    11  import (
    12  	"context"
    13  	"strings"
    14  
    15  	"github.com/gogf/gf/v2/container/garray"
    16  	"github.com/gogf/gf/v2/os/gfile"
    17  	"github.com/gogf/gf/v2/os/gres"
    18  	"github.com/gogf/gf/v2/util/gconv"
    19  )
    20  
    21  // staticPathItem is the item struct for static path configuration.
    22  type staticPathItem struct {
    23  	Prefix string // The router URI.
    24  	Path   string // The static path.
    25  }
    26  
    27  // SetIndexFiles sets the index files for server.
    28  func (s *Server) SetIndexFiles(indexFiles []string) {
    29  	s.config.IndexFiles = indexFiles
    30  }
    31  
    32  // GetIndexFiles retrieves and returns the index files from the server.
    33  func (s *Server) GetIndexFiles() []string {
    34  	return s.config.IndexFiles
    35  }
    36  
    37  // SetIndexFolder enables/disables listing the sub-files if requesting a directory.
    38  func (s *Server) SetIndexFolder(enabled bool) {
    39  	s.config.IndexFolder = enabled
    40  }
    41  
    42  // SetFileServerEnabled enables/disables the static file service.
    43  // It's the main switch for the static file service. When static file service configuration
    44  // functions like SetServerRoot, AddSearchPath and AddStaticPath are called, this configuration
    45  // is automatically enabled.
    46  func (s *Server) SetFileServerEnabled(enabled bool) {
    47  	s.config.FileServerEnabled = enabled
    48  }
    49  
    50  // SetServerRoot sets the document root for static service.
    51  func (s *Server) SetServerRoot(root string) {
    52  	var (
    53  		ctx      = context.TODO()
    54  		realPath = root
    55  	)
    56  	if !gres.Contains(realPath) {
    57  		if p, err := gfile.Search(root); err != nil {
    58  			s.Logger().Fatalf(ctx, `SetServerRoot failed: %+v`, err)
    59  		} else {
    60  			realPath = p
    61  		}
    62  	}
    63  	s.Logger().Debug(ctx, "SetServerRoot path:", realPath)
    64  	s.config.SearchPaths = []string{strings.TrimRight(realPath, gfile.Separator)}
    65  	s.config.FileServerEnabled = true
    66  }
    67  
    68  // AddSearchPath add searching directory path for static file service.
    69  func (s *Server) AddSearchPath(path string) {
    70  	var (
    71  		ctx      = context.TODO()
    72  		realPath = path
    73  	)
    74  	if !gres.Contains(realPath) {
    75  		if p, err := gfile.Search(path); err != nil {
    76  			s.Logger().Fatalf(ctx, `AddSearchPath failed: %+v`, err)
    77  		} else {
    78  			realPath = p
    79  		}
    80  	}
    81  	s.config.SearchPaths = append(s.config.SearchPaths, realPath)
    82  	s.config.FileServerEnabled = true
    83  }
    84  
    85  // AddStaticPath sets the uri to static directory path mapping for static file service.
    86  func (s *Server) AddStaticPath(prefix string, path string) {
    87  	var (
    88  		ctx      = context.TODO()
    89  		realPath = path
    90  	)
    91  	if !gres.Contains(realPath) {
    92  		if p, err := gfile.Search(path); err != nil {
    93  			s.Logger().Fatalf(ctx, `AddStaticPath failed: %+v`, err)
    94  		} else {
    95  			realPath = p
    96  		}
    97  	}
    98  	addItem := staticPathItem{
    99  		Prefix: prefix,
   100  		Path:   realPath,
   101  	}
   102  	if len(s.config.StaticPaths) > 0 {
   103  		s.config.StaticPaths = append(s.config.StaticPaths, addItem)
   104  		// Sort the array by length of prefix from short to long.
   105  		array := garray.NewSortedArray(func(v1, v2 interface{}) int {
   106  			s1 := gconv.String(v1)
   107  			s2 := gconv.String(v2)
   108  			r := len(s2) - len(s1)
   109  			if r == 0 {
   110  				r = strings.Compare(s1, s2)
   111  			}
   112  			return r
   113  		})
   114  		for _, v := range s.config.StaticPaths {
   115  			array.Add(v.Prefix)
   116  		}
   117  		// Add the items to paths by previous sorted slice.
   118  		paths := make([]staticPathItem, 0)
   119  		for _, v := range array.Slice() {
   120  			for _, item := range s.config.StaticPaths {
   121  				if strings.EqualFold(gconv.String(v), item.Prefix) {
   122  					paths = append(paths, item)
   123  					break
   124  				}
   125  			}
   126  		}
   127  		s.config.StaticPaths = paths
   128  	} else {
   129  		s.config.StaticPaths = []staticPathItem{addItem}
   130  	}
   131  	s.config.FileServerEnabled = true
   132  }