github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_server_config_static.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // 静态文件搜索优先级: ServerPaths > ServerRoot > SearchPath 8 9 package ghttp 10 11 import ( 12 "fmt" 13 "github.com/zhongdalu/gf/g/container/garray" 14 "github.com/zhongdalu/gf/g/os/gfile" 15 "github.com/zhongdalu/gf/g/os/glog" 16 "github.com/zhongdalu/gf/g/util/gconv" 17 "strings" 18 ) 19 20 // 静态文件目录映射关系对象 21 type staticPathItem struct { 22 prefix string // 映射的URI前缀 23 path string // 静态文件目录绝对路径 24 } 25 26 // 设置http server参数 - IndexFiles,默认展示文件,如:index.html, index.htm 27 func (s *Server) SetIndexFiles(index []string) { 28 if s.Status() == SERVER_STATUS_RUNNING { 29 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 30 return 31 } 32 s.config.IndexFiles = index 33 } 34 35 // 允许展示访问目录的文件列表 36 func (s *Server) SetIndexFolder(enabled bool) { 37 if s.Status() == SERVER_STATUS_RUNNING { 38 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 39 return 40 } 41 s.config.IndexFolder = enabled 42 } 43 44 // 是否开启/关闭静态文件服务,当关闭时仅提供动态接口服务,路由性能会得到一定提升 45 func (s *Server) SetFileServerEnabled(enabled bool) { 46 if s.Status() == SERVER_STATUS_RUNNING { 47 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 48 return 49 } 50 s.config.FileServerEnabled = enabled 51 } 52 53 // 设置http server参数 - ServerRoot 54 func (s *Server) SetServerRoot(root string) { 55 if s.Status() == SERVER_STATUS_RUNNING { 56 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 57 return 58 } 59 // RealPath的作用除了校验地址正确性以外,还转换分隔符号为当前系统正确的文件分隔符号 60 realPath, err := gfile.Search(root) 61 if err != nil { 62 glog.Fatal(fmt.Sprintf(`[ghttp] SetServerRoot failed: %s`, err.Error())) 63 } 64 glog.Debug("[ghttp] SetServerRoot path:", realPath) 65 s.config.SearchPaths = []string{strings.TrimRight(realPath, gfile.Separator)} 66 s.config.FileServerEnabled = true 67 } 68 69 // 添加静态文件搜索**目录**,必须给定目录的绝对路径 70 func (s *Server) AddSearchPath(path string) { 71 if s.Status() == SERVER_STATUS_RUNNING { 72 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 73 return 74 } 75 realPath, err := gfile.Search(path) 76 if err != nil { 77 glog.Fatal(fmt.Sprintf(`[ghttp] AddSearchPath failed: %s`, err.Error())) 78 } 79 s.config.SearchPaths = append(s.config.SearchPaths, realPath) 80 s.config.FileServerEnabled = true 81 } 82 83 // 添加URI与静态**目录**的映射 84 func (s *Server) AddStaticPath(prefix string, path string) { 85 if s.Status() == SERVER_STATUS_RUNNING { 86 glog.Error(gCHANGE_CONFIG_WHILE_RUNNING_ERROR) 87 return 88 } 89 realPath, err := gfile.Search(path) 90 if err != nil { 91 glog.Fatal(fmt.Sprintf(`[ghttp] AddStaticPath failed: %s`, err.Error())) 92 } 93 addItem := staticPathItem{ 94 prefix: prefix, 95 path: realPath, 96 } 97 if len(s.config.StaticPaths) > 0 { 98 // 先添加item 99 s.config.StaticPaths = append(s.config.StaticPaths, addItem) 100 // 按照prefix从长到短进行排序 101 array := garray.NewSortedArray(func(v1, v2 interface{}) int { 102 s1 := gconv.String(v1) 103 s2 := gconv.String(v2) 104 r := len(s2) - len(s1) 105 if r == 0 { 106 r = strings.Compare(s1, s2) 107 } 108 return r 109 }, false) 110 for _, v := range s.config.StaticPaths { 111 array.Add(v.prefix) 112 } 113 // 按照重新排序的顺序重新添加item 114 paths := make([]staticPathItem, 0) 115 for _, v := range array.Slice() { 116 for _, item := range s.config.StaticPaths { 117 if strings.EqualFold(gconv.String(v), item.prefix) { 118 paths = append(paths, item) 119 break 120 } 121 } 122 } 123 s.config.StaticPaths = paths 124 } else { 125 s.config.StaticPaths = []staticPathItem{addItem} 126 } 127 s.config.FileServerEnabled = true 128 }