github.com/bosssauce/ponzu@v0.11.1-0.20200102001432-9bc41b703131/system/admin/config/config.go (about)

     1  // Package config provides a content type to manage the Ponzu system's configuration
     2  // settings for things such as its name, domain, HTTP(s) port, email, server defaults
     3  // and backups.
     4  package config
     5  
     6  import (
     7  	"github.com/ponzu-cms/ponzu/management/editor"
     8  	"github.com/ponzu-cms/ponzu/system/item"
     9  )
    10  
    11  // Config represents the confirgurable options of the system
    12  type Config struct {
    13  	item.Item
    14  
    15  	Name                    string   `json:"name"`
    16  	Domain                  string   `json:"domain"`
    17  	BindAddress             string   `json:"bind_addr"`
    18  	HTTPPort                string   `json:"http_port"`
    19  	HTTPSPort               string   `json:"https_port"`
    20  	AdminEmail              string   `json:"admin_email"`
    21  	ClientSecret            string   `json:"client_secret"`
    22  	Etag                    string   `json:"etag"`
    23  	DisableCORS             bool     `json:"cors_disabled"`
    24  	DisableGZIP             bool     `json:"gzip_disabled"`
    25  	DisableHTTPCache        bool     `json:"cache_disabled"`
    26  	CacheMaxAge             int64    `json:"cache_max_age"`
    27  	CacheInvalidate         []string `json:"cache"`
    28  	BackupBasicAuthUser     string   `json:"backup_basic_auth_user"`
    29  	BackupBasicAuthPassword string   `json:"backup_basic_auth_password"`
    30  }
    31  
    32  const (
    33  	dbBackupInfo = `
    34  		<p class="flow-text">Database Backup Credentials:</p>
    35  		<p>Add a user name and password to download a backup of your data via HTTP.</p>
    36  	`
    37  )
    38  
    39  // String partially implements item.Identifiable and overrides Item's String()
    40  func (c *Config) String() string { return c.Name }
    41  
    42  // MarshalEditor writes a buffer of html to edit a Post and partially implements editor.Editable
    43  func (c *Config) MarshalEditor() ([]byte, error) {
    44  	view, err := editor.Form(c,
    45  		editor.Field{
    46  			View: editor.Input("Name", c, map[string]string{
    47  				"label":       "Site Name",
    48  				"placeholder": "Add a name to this site (internal use only)",
    49  			}),
    50  		},
    51  		editor.Field{
    52  			View: editor.Input("Domain", c, map[string]string{
    53  				"label":       "Domain Name (required for SSL certificate)",
    54  				"placeholder": "e.g. www.example.com or example.com",
    55  			}),
    56  		},
    57  		editor.Field{
    58  			View: editor.Input("BindAddress", c, map[string]string{
    59  				"type": "hidden",
    60  			}),
    61  		},
    62  		editor.Field{
    63  			View: editor.Input("HTTPPort", c, map[string]string{
    64  				"type": "hidden",
    65  			}),
    66  		},
    67  		editor.Field{
    68  			View: editor.Input("HTTPSPort", c, map[string]string{
    69  				"type": "hidden",
    70  			}),
    71  		},
    72  		editor.Field{
    73  			View: editor.Input("AdminEmail", c, map[string]string{
    74  				"label": "Administrator Email (notified of internal system information)",
    75  			}),
    76  		},
    77  		editor.Field{
    78  			View: editor.Input("ClientSecret", c, map[string]string{
    79  				"label":    "Client Secret (used to validate requests, DO NOT SHARE)",
    80  				"disabled": "true",
    81  			}),
    82  		},
    83  		editor.Field{
    84  			View: editor.Input("ClientSecret", c, map[string]string{
    85  				"type": "hidden",
    86  			}),
    87  		},
    88  		editor.Field{
    89  			View: editor.Input("Etag", c, map[string]string{
    90  				"label":    "Etag Header (used to cache resources)",
    91  				"disabled": "true",
    92  			}),
    93  		},
    94  		editor.Field{
    95  			View: editor.Input("Etag", c, map[string]string{
    96  				"type": "hidden",
    97  			}),
    98  		},
    99  		editor.Field{
   100  			View: editor.Checkbox("DisableCORS", c, map[string]string{
   101  				"label": "Disable CORS (so only " + c.Domain + " can fetch your data)",
   102  			}, map[string]string{
   103  				"true": "Disable CORS",
   104  			}),
   105  		},
   106  		editor.Field{
   107  			View: editor.Checkbox("DisableGZIP", c, map[string]string{
   108  				"label": "Disable GZIP (will increase server speed, but also bandwidth)",
   109  			}, map[string]string{
   110  				"true": "Disable GZIP",
   111  			}),
   112  		},
   113  		editor.Field{
   114  			View: editor.Checkbox("DisableHTTPCache", c, map[string]string{
   115  				"label": "Disable HTTP Cache (overrides 'Cache-Control' header)",
   116  			}, map[string]string{
   117  				"true": "Disable HTTP Cache",
   118  			}),
   119  		},
   120  		editor.Field{
   121  			View: editor.Input("CacheMaxAge", c, map[string]string{
   122  				"label": "Max-Age value for HTTP caching (in seconds, 0 = 2592000)",
   123  				"type":  "text",
   124  			}),
   125  		},
   126  		editor.Field{
   127  			View: editor.Checkbox("CacheInvalidate", c, map[string]string{
   128  				"label": "Invalidate cache on save",
   129  			}, map[string]string{
   130  				"invalidate": "Invalidate Cache",
   131  			}),
   132  		},
   133  		editor.Field{
   134  			View: []byte(dbBackupInfo),
   135  		},
   136  		editor.Field{
   137  			View: editor.Input("BackupBasicAuthUser", c, map[string]string{
   138  				"label":       "HTTP Basic Auth User",
   139  				"placeholder": "Enter a user name for Basic Auth access",
   140  				"type":        "text",
   141  			}),
   142  		},
   143  		editor.Field{
   144  			View: editor.Input("BackupBasicAuthPassword", c, map[string]string{
   145  				"label":       "HTTP Basic Auth Password",
   146  				"placeholder": "Enter a password for Basic Auth access",
   147  				"type":        "password",
   148  			}),
   149  		},
   150  	)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	open := []byte(`
   156  	<div class="card">
   157  		<div class="card-content">
   158  			<div class="card-title">System Configuration</div>
   159  		</div>
   160  		<form action="/admin/configure" method="post">
   161  	`)
   162  	close := []byte(`</form></div>`)
   163  	script := []byte(`
   164  	<script>
   165  		$(function() {
   166  			// hide default fields & labels unnecessary for the config
   167  			var fields = $('.default-fields');
   168  			fields.css('position', 'relative');
   169  			fields.find('input:not([type=submit])').remove();
   170  			fields.find('label').remove();
   171  			fields.find('button').css({
   172  				position: 'absolute',
   173  				top: '-10px',
   174  				right: '0px'
   175  			});
   176  
   177  			var contentOnly = $('.content-only.__ponzu');
   178  			contentOnly.hide();
   179  			contentOnly.find('input, textarea, select').attr('name', '');
   180  
   181  			// adjust layout of td so save button is in same location as usual
   182  			fields.find('td').css('float', 'right');
   183  
   184  			// stop some fixed config settings from being modified
   185  			fields.find('input[name=client_secret]').attr('name', '');
   186  		});
   187  	</script>
   188  	`)
   189  
   190  	view = append(open, view...)
   191  	view = append(view, close...)
   192  	view = append(view, script...)
   193  
   194  	return view, nil
   195  }