-
Notifications
You must be signed in to change notification settings - Fork 1
/
implementation.go
81 lines (67 loc) · 2.63 KB
/
implementation.go
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
81
package main
import (
"bytes"
"errors"
"html/template"
"os"
"regexp"
"strings"
"github.com/cubex/portcullis-go"
"github.com/cubex/potens-go/webui"
"github.com/cubex/potens-go/webui/breadcrumb"
"github.com/cubex/proto-go/applications"
"go.uber.org/zap"
"golang.org/x/net/context"
)
func (s *server) PageDefinition(ctx context.Context, in *applications.HTTPRequest) (*applications.HTTPResponse, error) {
//Retrieve auth data
response := webui.CreateResponse()
authData := portcullis.FromContext(ctx)
app.Logger.Debug("Received HTTP Request", zap.String("userID", authData.UserID), zap.String("projectID", authData.ProjectID))
if in.Path == "/" {
webui.SetPageTitle(response, "List Of Things")
} else {
// Load the hero template for the page if one exists
if _, err := os.Stat("heroTemplates" + in.Path + ".html"); err == nil {
defaultTemplate := template.Must(template.ParseFiles("heroTemplates" + in.Path + ".html"))
buf := new(bytes.Buffer)
defaultTemplate.Execute(buf, "")
response.Body = buf.String()
}
pageKey := in.Path[1:]
r, _ := regexp.Compile("(\\w)(\\w*)")
pageTitle := r.ReplaceAllStringFunc(strings.Replace(pageKey, "-", " ", -1), func(str string) string {
return strings.ToUpper(str[0:1]) + strings.ToLower(str[1:])
})
bread := breadcrumb.Breadcrumb{}
bread.AddItem(breadcrumb.BreadcrumbItem{Url: in.Path, Title: pageTitle})
webui.SetBackPath(response, "/")
webui.SetBreadcrumb(response, bread)
webui.SetPageTitle(response, pageTitle)
webui.SetPageFID(response, pageKey)
}
return response, nil
}
// HandleHTTPRequest handles requests from HTTP sources
func (s *server) HTTPResource(ctx context.Context, in *applications.HTTPRequest) (*applications.HTTPResponse, error) {
//Retrieve auth data
response := webui.CreateResponse()
defaultTemplate := template.New("")
if in.Path == "/" {
defaultTemplate = template.Must(template.ParseFiles("templates/default.html"))
} else if strings.HasSuffix(in.Path, "/details") {
defaultTemplate = template.Must(template.ParseFiles("templates/example/details.html"))
} else {
defaultTemplate = template.Must(template.ParseFiles("templates/example/example.html"))
}
buf := new(bytes.Buffer)
defaultTemplate.Execute(buf, "")
response.Body = buf.String()
return response, nil
}
func (s *server) HandleSocketAction(ctx context.Context, in *applications.SocketRequest) (*applications.HTTPResponse, error) {
return nil, errors.New("Sockets not supported")
}
func (s *server) ModifyRelationship(context.Context, *applications.ProjectModifyRequest) (*applications.ProjectModifyResponse, error) {
return &applications.ProjectModifyResponse{Success: true}, nil
}