This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
/
index.go
68 lines (62 loc) · 2.17 KB
/
index.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
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/XeroAPI/golang-oauth2-example/config"
"github.com/XeroAPI/golang-oauth2-example/server/ui"
"github.com/XeroAPI/golang-oauth2-example/xero"
)
func (s *Server) handleIndexPage(w http.ResponseWriter, req *http.Request) {
ui.WriteGlobalStylesTag(w)
if s.preFlightCheck() {
// Render the 'Connect to Xero' button
w.Write([]byte("<h1>Log in to Xero with OAuth 2.0!</h1>"))
w.Write([]byte("<p style='font-size:0.67rem'><em>you know you want to</em><p>"))
w.Write([]byte("<p><a href='" + loginPath + "'>" + ui.ConnectButtonSVG + "</a></p>"))
return
}
connectionsResponse, err := s.httpClient.Get("https://api.xero.com/connections")
if err != nil && connectionsResponse.StatusCode != 200 {
errMsg := "An error occurred while trying to retrieve the organisations connected to this account."
w.Write([]byte(errMsg + " Please check the log for details"))
log.Println(errMsg)
log.Println(err)
}
respBody, err := ioutil.ReadAll(connectionsResponse.Body)
if err != nil {
errMsg := "An error occurred while trying to read the response from the Xero API"
w.Write([]byte(errMsg + " Please check the log for details"))
log.Println(errMsg)
log.Fatalln(err)
if config.DebugMode {
fmt.Println(string(respBody))
}
return
}
var connections []xero.Connection
err = json.Unmarshal(respBody, &connections)
if err != nil {
errMsg := "There was an error attempting to unmarshal the data from the connections endpoint."
w.Write([]byte(errMsg + " Please check the log for details."))
log.Println(errMsg)
log.Println(err)
if config.DebugMode {
fmt.Println(string(respBody))
}
return
}
w.Write([]byte("<h1>Connected Entities™</h1>"))
w.Write([]byte("<ul>"))
for _, org := range connections {
w.Write([]byte("<li><a href=\"/organisation?tenantId=" + org.TenantID + "\">" + org.TenantName + "</a></li>"))
}
w.Write([]byte("</ul>"))
w.Write([]byte("<h2>Other Fun Things</h2>"))
w.Write([]byte("<ul>"))
w.Write([]byte("<li><a href=\"/refresh\">Force Refresh Access Token</a></li>"))
w.Write([]byte("<li><a href=\"/segfault\">Cause a Segfault™</a></li>"))
w.Write([]byte("</ul>"))
}