-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
129 lines (108 loc) · 3.88 KB
/
handlers.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package views
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/fatih/structs"
"github.com/gofiber/fiber/v2"
"github.com/horkruxes/hkxserver/api"
"github.com/horkruxes/hkxserver/model"
"github.com/horkruxes/hkxserver/service"
)
func GetKeys(c *fiber.Ctx) error {
outputData := model.GenKeys()
return c.Render("templates/keys/root", structs.Map(outputData))
}
func PostKeys(c *fiber.Ctx) error {
outputData := model.GenKeys()
// Get form data and reinject into output data
outputData.Sig = strings.TrimSpace(c.FormValue("signature"))
outputData.Sec = strings.TrimSpace(c.FormValue("secret-key"))
outputData.Pub = strings.TrimSpace(c.FormValue("public-key"))
outputData.DisplayedName = strings.TrimSpace(c.FormValue("displayed-name"))
outputData.Content = strings.TrimSpace(c.FormValue("message"))
outputData.MessageID = strings.TrimSpace(c.FormValue("answer-to"))
outputData.Verif = true
if outputData.Sig == "" {
// Answers to the signature GENERATION form
outputData.Sig = model.SignMessage(outputData.Sec, outputData.Pub, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Valid = model.VerifyFromString(outputData.Pub, outputData.Sig, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Sec = ""
} else {
// Answers to the signature VERIFICATION form
outputData.Valid = model.VerifyFromString(outputData.Pub, outputData.Sig, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Sig = ""
}
return c.Render("templates/keys/root", structs.Map(outputData))
}
func GetFaq(c *fiber.Ctx) error {
return c.Render("templates/faq/root", fiber.Map{})
}
func GetMainNoFront(c *fiber.Ctx) error {
return c.SendString(`The pod admin chose to only use the 'data' part of Horkruxes.
Sorry, you'll have to use another client to see the messages.`)
}
func GetMain(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
localData := GetMessagesAndMainPageInfo(s)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func GetAuthor(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
pubKey := c.Params("pubKey")
fmt.Println("pods list", s.GeneralConfig.TrustedPods)
localData := GetAuthorMessagesAndMainPageInfo(s, pubKey)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func GetComments(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
id := c.Params("uuid")
localData := GetCommentsAndMainPageInfo(s, id)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func NewMessage(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
payload := FromFormToPayload(c)
reader, err := json.Marshal(payload)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("try to post to:", payload.Pod)
// Check if can do the db operations right now or if it should transfer the payload to another API
if payload.Pod == "" {
message, statusCode, err := api.PayloadToValidMessage(s, payload)
if err != nil {
return c.Status(statusCode).SendString(err.Error())
}
fmt.Println("new msg", message)
err = model.NewMessage(s, message)
if err != nil {
return c.Status(statusCode).SendString(err.Error())
}
} else {
resp, err := http.Post("https://"+payload.Pod+"/api/message", "application/json", bytes.NewBuffer(reader))
if err != nil {
fmt.Println("err:", err)
}
if resp.StatusCode == 409 {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(string(body))
return c.SendString("error sending the message " + string(body))
}
}
return c.Redirect("/")
}
}