github.com/gogf/gf@v1.16.9/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  	"fmt"
    13  	"strings"
    14  
    15  	"github.com/gogf/gf/os/gres"
    16  
    17  	"github.com/gogf/gf/container/garray"
    18  	"github.com/gogf/gf/os/gfile"
    19  	"github.com/gogf/gf/util/gconv"
    20  )
    21  
    22  // staticPathItem is the item struct for static path configuration.
    23  type staticPathItem struct {
    24  	prefix string // The router URI.
    25  	path   string // The static path.
    26  }
    27  
    28  // SetIndexFiles sets the index files for server.
    29  func (s *Server) SetIndexFiles(indexFiles []string) {
    30  	s.config.IndexFiles = indexFiles
    31  }
    32  
    33  // GetIndexFiles retrieves and returns the index files from server.
    34  func (s *Server) GetIndexFiles() []string {
    35  	return s.config.IndexFiles
    36  }
    37  
    38  // SetIndexFolder enables/disables listing the sub-files if requesting a directory.
    39  func (s *Server) SetIndexFolder(enabled bool) {
    40  	s.config.IndexFolder = enabled
    41  }
    42  
    43  // SetFileServerEnabled enables/disables the static file service.
    44  // It's the main switch for the static file service. When static file service configuration
    45  // functions like SetServerRoot, AddSearchPath and AddStaticPath are called, this configuration
    46  // is automatically enabled.
    47  func (s *Server) SetFileServerEnabled(enabled bool) {
    48  	s.config.FileServerEnabled = enabled
    49  }
    50  
    51  // SetServerRoot sets the document root for static service.
    52  func (s *Server) SetServerRoot(root string) {
    53  	realPath := root
    54  	if !gres.Contains(realPath) {
    55  		if p, err := gfile.Search(root); err != nil {
    56  			s.Logger().Fatal(fmt.Sprintf(`SetServerRoot failed: %v`, err))
    57  		} else {
    58  			realPath = p
    59  		}
    60  	}
    61  	s.Logger().Debug("SetServerRoot path:", realPath)
    62  	s.config.SearchPaths = []string{strings.TrimRight(realPath, gfile.Separator)}
    63  	s.config.FileServerEnabled = true
    64  }
    65  
    66  // AddSearchPath add searching directory path for static file service.
    67  func (s *Server) AddSearchPath(path string) {
    68  	realPath := path
    69  	if !gres.Contains(realPath) {
    70  		if p, err := gfile.Search(path); err != nil {
    71  			s.Logger().Fatal(fmt.Sprintf(`AddSearchPath failed: %v`, err))
    72  		} else {
    73  			realPath = p
    74  		}
    75  	}
    76  	s.config.SearchPaths = append(s.config.SearchPaths, realPath)
    77  	s.config.FileServerEnabled = true
    78  }
    79  
    80  // AddStaticPath sets the uri to static directory path mapping for static file service.
    81  func (s *Server) AddStaticPath(prefix string, path string) {
    82  	realPath := path
    83  	if !gres.Contains(realPath) {
    84  		if p, err := gfile.Search(path); err != nil {
    85  			s.Logger().Fatal(fmt.Sprintf(`AddStaticPath failed: %v`, err))
    86  		} else {
    87  			realPath = p
    88  		}
    89  	}
    90  	addItem := staticPathItem{
    91  		prefix: prefix,
    92  		path:   realPath,
    93  	}
    94  	if len(s.config.StaticPaths) > 0 {
    95  		s.config.StaticPaths = append(s.config.StaticPaths, addItem)
    96  		// Sort the array by length of prefix from short to long.
    97  		array := garray.NewSortedArray(func(v1, v2 interface{}) int {
    98  			s1 := gconv.String(v1)
    99  			s2 := gconv.String(v2)
   100  			r := len(s2) - len(s1)
   101  			if r == 0 {
   102  				r = strings.Compare(s1, s2)
   103  			}
   104  			return r
   105  		})
   106  		for _, v := range s.config.StaticPaths {
   107  			array.Add(v.prefix)
   108  		}
   109  		// Add the items to paths by previous sorted slice.
   110  		paths := make([]staticPathItem, 0)
   111  		for _, v := range array.Slice() {
   112  			for _, item := range s.config.StaticPaths {
   113  				if strings.EqualFold(gconv.String(v), item.prefix) {
   114  					paths = append(paths, item)
   115  					break
   116  				}
   117  			}
   118  		}
   119  		s.config.StaticPaths = paths
   120  	} else {
   121  		s.config.StaticPaths = []staticPathItem{addItem}
   122  	}
   123  	s.config.FileServerEnabled = true
   124  }