github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/documize/api/endpoint/meta_endpoint.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 endpoint 13 14 import ( 15 "bytes" 16 "encoding/json" 17 "fmt" 18 "net/http" 19 "text/template" 20 21 "github.com/documize/community/documize/api/entity" 22 "github.com/documize/community/documize/api/request" 23 "github.com/documize/community/wordsmith/log" 24 "github.com/documize/community/wordsmith/utility" 25 ) 26 27 // GetMeta provides org meta data based upon request domain (e.g. acme.documize.com). 28 func GetMeta(w http.ResponseWriter, r *http.Request) { 29 method := "GetMeta" 30 p := request.GetPersister(r) 31 32 data := entity.SiteMeta{} 33 data.URL = request.GetSubdomainFromHost(r) 34 35 org, err := p.GetOrganizationByDomain(data.URL) 36 37 if err != nil { 38 log.Info(fmt.Sprintf("%s URL not found", data.URL)) 39 writeForbiddenError(w) 40 return 41 } 42 43 data.OrgID = org.RefID 44 data.Title = org.Title 45 data.Message = org.Message 46 data.AllowAnonymousAccess = org.AllowAnonymousAccess 47 data.Version = AppVersion 48 49 json, err := json.Marshal(data) 50 51 if err != nil { 52 writeJSONMarshalError(w, method, "meta", err) 53 return 54 } 55 56 writeSuccessBytes(w, json) 57 } 58 59 // GetRobots returns robots.txt depending on site configuration. 60 // Did we allow anonymouse access? 61 func GetRobots(w http.ResponseWriter, r *http.Request) { 62 method := "GetRobots" 63 p := request.GetPersister(r) 64 65 domain := request.GetSubdomainFromHost(r) 66 org, err := p.GetOrganizationByDomain(domain) 67 68 // default is to deny 69 robots := 70 `User-agent: * 71 Disallow: / 72 ` 73 74 if err != nil { 75 log.Error(fmt.Sprintf("%s failed to get Organization for domain %s", method, domain), err) 76 } 77 78 // Anonymous access would mean we allow bots to crawl. 79 if org.AllowAnonymousAccess { 80 sitemap := getAppURL(p.Context, "sitemap.xml") 81 robots = fmt.Sprintf( 82 `User-agent: * 83 Disallow: /settings/ 84 Disallow: /settings/* 85 Disallow: /profile/ 86 Disallow: /profile/* 87 Disallow: /auth/login/ 88 Disallow: /auth/login/ 89 Disallow: /auth/logout/ 90 Disallow: /auth/logout/* 91 Disallow: /auth/reset/* 92 Disallow: /auth/reset/* 93 Disallow: /auth/sso/ 94 Disallow: /auth/sso/* 95 Disallow: /share 96 Disallow: /share/* 97 Sitemap: %s`, sitemap) 98 } 99 100 writeSuccessBytes(w, []byte(robots)) 101 } 102 103 // GetSitemap returns URLs that can be indexed. 104 // We only include public folders and documents (e.g. can be seen by everyone). 105 func GetSitemap(w http.ResponseWriter, r *http.Request) { 106 method := "GetSitemap" 107 p := request.GetPersister(r) 108 109 domain := request.GetSubdomainFromHost(r) 110 org, err := p.GetOrganizationByDomain(domain) 111 112 if err != nil { 113 log.Error(fmt.Sprintf("%s failed to get Organization for domain %s", method, domain), err) 114 } 115 116 sitemap := 117 `<?xml version="1.0" encoding="UTF-8"?> 118 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> 119 {{range .}}<url> 120 <loc>{{ .URL }}</loc> 121 <lastmod>{{ .Date }}</lastmod> 122 </url>{{end}} 123 </urlset>` 124 125 var items []sitemapItem 126 127 // Anonymous access means we announce folders/documents shared with 'Everyone'. 128 if org.AllowAnonymousAccess { 129 // Grab shared folders 130 folders, err := p.GetPublicFolders(org.RefID) 131 132 if err != nil { 133 log.Error(fmt.Sprintf("%s failed to get folders for domain %s", method, domain), err) 134 } 135 136 for _, folder := range folders { 137 var item sitemapItem 138 item.URL = getAppURL(p.Context, fmt.Sprintf("s/%s/%s", folder.RefID, utility.MakeSlug(folder.Name))) 139 item.Date = folder.Revised.Format("2006-01-02T15:04:05.999999-07:00") 140 items = append(items, item) 141 } 142 143 // Grab documents from shared folders 144 documents, err := p.GetPublicDocuments(org.RefID) 145 146 if err != nil { 147 log.Error(fmt.Sprintf("%s failed to get documents for domain %s", method, domain), err) 148 } 149 150 for _, document := range documents { 151 var item sitemapItem 152 item.URL = getAppURL(p.Context, fmt.Sprintf("s/%s/%s/d/%s/%s", 153 document.FolderID, utility.MakeSlug(document.Folder), document.DocumentID, utility.MakeSlug(document.Document))) 154 item.Date = document.Revised.Format("2006-01-02T15:04:05.999999-07:00") 155 items = append(items, item) 156 } 157 158 } 159 160 buffer := new(bytes.Buffer) 161 t := template.Must(template.New("tmp").Parse(sitemap)) 162 log.IfErr(t.Execute(buffer, &items)) 163 164 writeSuccessBytes(w, buffer.Bytes()) 165 } 166 167 // sitemapItem provides a means to teleport somewhere else for free. 168 // What did you think it did? 169 type sitemapItem struct { 170 URL string 171 Date string 172 }