github.com/wangkui503/aero@v1.0.0/Configuration.go (about)

     1  package aero
     2  
     3  import (
     4  	"os"
     5  
     6  	jsoniter "github.com/json-iterator/go"
     7  )
     8  
     9  // Configuration represents the data in your config.json file.
    10  type Configuration struct {
    11  	Domain   string               `json:"domain"`
    12  	Title    string               `json:"title"`
    13  	Fonts    []string             `json:"fonts"`
    14  	Styles   []string             `json:"styles"`
    15  	Scripts  ScriptsConfiguration `json:"scripts"`
    16  	Push     []string             `json:"push"`
    17  	Manifest Manifest             `json:"manifest"`
    18  	GZip     bool                 `json:"gzip"`
    19  	Ports    PortConfiguration    `json:"ports"`
    20  }
    21  
    22  // ScriptsConfiguration lets you configure your main entry script.
    23  type ScriptsConfiguration struct {
    24  	// Entry point for scripts
    25  	Main string `json:"main"`
    26  }
    27  
    28  // Manifest represents a web manifest
    29  type Manifest struct {
    30  	Name            string         `json:"name"`
    31  	ShortName       string         `json:"short_name"`
    32  	Icons           []ManifestIcon `json:"icons,omitempty"`
    33  	StartURL        string         `json:"start_url"`
    34  	Display         string         `json:"display"`
    35  	Lang            string         `json:"lang,omitempty"`
    36  	ThemeColor      string         `json:"theme_color,omitempty"`
    37  	BackgroundColor string         `json:"background_color,omitempty"`
    38  	GCMSenderID     string         `json:"gcm_sender_id,omitempty"`
    39  }
    40  
    41  // ManifestIcon represents a single icon in the web manifest.
    42  type ManifestIcon struct {
    43  	Source string `json:"src"`
    44  	Sizes  string `json:"sizes"`
    45  	Type   string `json:"type"`
    46  }
    47  
    48  // PortConfiguration lets you configure the ports that Aero will listen on.
    49  type PortConfiguration struct {
    50  	HTTP  int `json:"http"`
    51  	HTTPS int `json:"https"`
    52  }
    53  
    54  // Reset resets all fields to the default configuration.
    55  func (config *Configuration) Reset() {
    56  	config.GZip = true
    57  	config.Ports.HTTP = 4000
    58  	config.Ports.HTTPS = 4001
    59  	config.Manifest.StartURL = "/"
    60  	config.Manifest.Display = "standalone"
    61  	config.Manifest.Lang = "en"
    62  	config.Manifest.ShortName = "Untitled"
    63  	config.Title = "Untitled site"
    64  }
    65  
    66  // LoadConfig loads the application configuration from the file system.
    67  func LoadConfig(path string) (*Configuration, error) {
    68  	file, err := os.Open(path)
    69  
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	defer file.Close()
    75  	config := &Configuration{}
    76  	config.Reset()
    77  
    78  	decoder := jsoniter.NewDecoder(file)
    79  	err = decoder.Decode(config)
    80  
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	if config.Manifest.Name == "" {
    86  		config.Manifest.Name = config.Title
    87  	}
    88  
    89  	if config.Manifest.ShortName == "" {
    90  		config.Manifest.ShortName = config.Title
    91  	}
    92  
    93  	return config, nil
    94  }