Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package filestorage |
| 16 | |
| 17 | import ( |
| 18 | "github.com/caddyserver/certmagic" |
| 19 | |
| 20 | "github.com/caddyserver/caddy/v2" |
| 21 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | caddy.RegisterModule(FileStorage{}) |
| 26 | } |
| 27 | |
| 28 | // FileStorage is a certmagic.Storage wrapper for certmagic.FileStorage. |
| 29 | type FileStorage struct { |
| 30 | // The base path to the folder used for storage. |
| 31 | Root string `json:"root,omitempty"` |
| 32 | } |
| 33 | |
| 34 | // CaddyModule returns the Caddy module information. |
| 35 | func (FileStorage) CaddyModule() caddy.ModuleInfo { |
| 36 | return caddy.ModuleInfo{ |
| 37 | ID: "caddy.storage.file_system", |
| 38 | New: func() caddy.Module { return new(FileStorage) }, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // CertMagicStorage converts s to a certmagic.Storage instance. |
| 43 | func (s FileStorage) CertMagicStorage() (certmagic.Storage, error) { |
| 44 | return &certmagic.FileStorage{Path: s.Root}, nil |
| 45 | } |
| 46 | |
| 47 | // UnmarshalCaddyfile sets up the storage module from Caddyfile tokens. |
| 48 | func (s *FileStorage) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 49 | if !d.Next() { |
| 50 | return d.Err("expected tokens") |
| 51 | } |
| 52 | if d.NextArg() { |
| 53 | s.Root = d.Val() |
| 54 | } |
| 55 | if d.NextArg() { |
| 56 | return d.ArgErr() |
| 57 | } |
| 58 | for d.NextBlock(0) { |
| 59 | switch d.Val() { |
| 60 | case "root": |
| 61 | if !d.NextArg() { |
| 62 | return d.ArgErr() |
| 63 | } |
| 64 | if s.Root != "" { |
| 65 | return d.Err("root already set") |
| 66 | } |
| 67 | s.Root = d.Val() |
| 68 | if d.NextArg() { |
| 69 | return d.ArgErr() |
| 70 | } |
| 71 | default: |
| 72 | return d.Errf("unrecognized parameter '%s'", d.Val()) |
| 73 | } |
| 74 | } |
| 75 | if s.Root == "" { |
| 76 | return d.Err("missing root path (to use default, omit storage config entirely)") |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // Interface guards |
| 82 | var ( |
| 83 | _ caddy.StorageConverter = (*FileStorage)(nil) |
| 84 | _ caddyfile.Unmarshaler = (*FileStorage)(nil) |
| 85 | ) |