github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/web/web.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  // Package web contains the Documize static web data.
    13  package web
    14  
    15  import (
    16  	"html/template"
    17  	"net/http"
    18  
    19  	"github.com/documize/community/core/api/util"
    20  	"github.com/documize/community/core/environment"
    21  )
    22  
    23  // SiteMode defines that the web server should show the system to be in a particular state.
    24  var SiteMode string
    25  
    26  const (
    27  	// SiteModeNormal serves app
    28  	SiteModeNormal = ""
    29  	// SiteModeOffline serves offline.html
    30  	SiteModeOffline = "1"
    31  	// SiteModeSetup tells Ember to serve setup route
    32  	SiteModeSetup = "2"
    33  	// SiteModeBadDB redirects to db-error.html page
    34  	SiteModeBadDB = "3"
    35  )
    36  
    37  // SiteInfo describes set-up information about the site
    38  var SiteInfo struct {
    39  	DBname, DBhash, Issue string
    40  }
    41  
    42  func init() {
    43  	environment.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil) // no sense overriding this setting from the DB
    44  	SiteInfo.DBhash = util.GenerateRandomPassword()                                        // do this only once
    45  }
    46  
    47  // EmbedHandler is defined in each embed directory
    48  type EmbedHandler interface {
    49  	Asset(string) ([]byte, error)
    50  	AssetDir(string) ([]string, error)
    51  	StaticAssetsFileSystem() http.FileSystem
    52  }
    53  
    54  // Embed allows access to the embedded data
    55  var Embed EmbedHandler
    56  
    57  // EmberHandler provides the webserver for pages developed using the Ember programming environment.
    58  func EmberHandler(w http.ResponseWriter, r *http.Request) {
    59  	filename := "index.html"
    60  	switch SiteMode {
    61  	case SiteModeOffline:
    62  		filename = "offline.html"
    63  	case SiteModeSetup:
    64  		// NoOp
    65  	case SiteModeBadDB:
    66  		filename = "db-error.html"
    67  	default:
    68  		SiteInfo.DBhash = ""
    69  	}
    70  
    71  	data, err := Embed.Asset("bindata/" + filename)
    72  	if err != nil {
    73  		// Asset was not found.
    74  		http.Error(w, err.Error(), http.StatusInternalServerError)
    75  		return
    76  	}
    77  
    78  	emberView := template.Must(template.New(filename).Parse(string(data)))
    79  
    80  	if err := emberView.Execute(w, SiteInfo); err != nil {
    81  		http.Error(w, err.Error(), http.StatusInternalServerError)
    82  	}
    83  }
    84  
    85  // StaticAssetsFileSystem data encoded in the go:generate above.
    86  func StaticAssetsFileSystem() http.FileSystem {
    87  	return Embed.StaticAssetsFileSystem()
    88  	//return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "bindata/public"}
    89  }
    90  
    91  // ReadFile is intended to substitute for ioutil.ReadFile().
    92  func ReadFile(filename string) ([]byte, error) {
    93  	return Embed.Asset("bindata/" + filename)
    94  }
    95  
    96  // Asset fetch.
    97  func Asset(location string) ([]byte, error) {
    98  	return Embed.Asset(location)
    99  }
   100  
   101  // AssetDir returns web app "assets" folder.
   102  func AssetDir(dir string) ([]string, error) {
   103  	return Embed.AssetDir(dir)
   104  }