LoadOptions loads rewrite Options from a system file.
(filename string)
| 36 | |
| 37 | // LoadOptions loads rewrite Options from a system file. |
| 38 | func LoadOptions(filename string) (opts Options) { |
| 39 | ext := ".yml" |
| 40 | if index := strings.LastIndexByte(filename, '.'); index > 1 && len(filename)-1 > index { |
| 41 | ext = filename[index:] |
| 42 | } |
| 43 | |
| 44 | f, err := os.Open(filename) |
| 45 | if err != nil { |
| 46 | panic("iris: rewrite: " + err.Error()) |
| 47 | } |
| 48 | defer f.Close() |
| 49 | |
| 50 | switch ext { |
| 51 | case ".yaml", ".yml": |
| 52 | err = yaml.NewDecoder(f).Decode(&opts) |
| 53 | case ".json": |
| 54 | err = json.NewDecoder(f).Decode(&opts) |
| 55 | default: |
| 56 | panic("iris: rewrite: unexpected file extension: " + filename) |
| 57 | } |
| 58 | |
| 59 | if err != nil { |
| 60 | panic("iris: rewrite: decode: " + err.Error()) |
| 61 | } |
| 62 | |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | // Rewrite is a struct that represents a rewrite engine for Iris web framework. |
| 67 | // It contains a slice of redirect rules, an options struct, a logger, and a domain validator function. |