summaryrefslogtreecommitdiffstats
path: root/api/api.go
blob: 4fecd3dd4a71c79a8b07d8bfb545480f0f860871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"bytes"
	l4g "github.com/alecthomas/log4go"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"
	"html/template"
	"net/http"

	_ "github.com/cloudfoundry/jibber_jabber"
	_ "github.com/nicksnyder/go-i18n/i18n"
)

var ServerTemplates *template.Template

type ServerTemplatePage Page

func NewServerTemplatePage(templateName, locale string) *ServerTemplatePage {
	return &ServerTemplatePage{
		TemplateName: templateName,
		Props:        make(map[string]string),
		Extra:        make(map[string]string),
		Html:         make(map[string]template.HTML),
		ClientCfg:    utils.ClientCfg,
		Locale:       locale,
	}
}

func (me *ServerTemplatePage) Render() string {
	var text bytes.Buffer

	T := utils.GetUserTranslations(me.Locale)
	me.Props["Footer"] = T("api.templates.email_footer")
	me.Html["EmailInfo"] = template.HTML(T("api.templates.email_info",
		map[string]interface{}{"SupportEmail": me.ClientCfg["SupportEmail"], "SiteName": me.ClientCfg["SiteName"]}))

	if err := ServerTemplates.ExecuteTemplate(&text, me.TemplateName, me); err != nil {
		l4g.Error(utils.T("api.api.render.error"), me.TemplateName, err)
	}

	return text.String()
}

func InitApi() {
	r := Srv.Router.PathPrefix("/api/v1").Subrouter()
	InitUser(r)
	InitTeam(r)
	InitChannel(r)
	InitPost(r)
	InitWebSocket(r)
	InitFile(r)
	InitCommand(r)
	InitAdmin(r)
	InitOAuth(r)
	InitWebhook(r)
	InitPreference(r)
	InitLicense(r)

	templatesDir := utils.FindDir("api/templates")
	l4g.Debug(utils.T("api.api.init.parsing_templates.debug"), templatesDir)
	var err error
	if ServerTemplates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
		l4g.Error(utils.T("api.api.init.parsing_templates.error"), err)
	}
}

func HandleEtag(etag string, w http.ResponseWriter, r *http.Request) bool {
	if et := r.Header.Get(model.HEADER_ETAG_CLIENT); len(etag) > 0 {
		if et == etag {
			w.WriteHeader(http.StatusNotModified)
			return true
		}
	}

	return false
}