This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registrarhandler.go
125 lines (121 loc) · 3.64 KB
/
registrarhandler.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
/*
* Copyright 2019-2020 Aletheia Ware LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"aletheiaware.com/bcgo"
"aletheiaware.com/netgo"
"aletheiaware.com/spacego"
"html/template"
"log"
"math"
"net/http"
)
func RegistrarHandler(template *template.Template, get func(string) (*spacego.Registrar, error)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path)
switch r.Method {
case "GET":
registrar := netgo.QueryParameter(r.URL.Query(), "registrar")
log.Println("Registrar", registrar)
if len(registrar) > 0 {
r, err := get(registrar)
if err != nil {
log.Println(err)
return
}
data := struct {
Alias string
Domain string
Country string
Currency string
GroupPrice string
GroupSize string
Interval string
Mode string
CostExamples [][]string
}{
Alias: r.Merchant.Alias,
Domain: r.Merchant.Domain,
Country: r.Service.Country,
Currency: r.Service.Currency,
GroupPrice: bcgo.MoneyToString(r.Service.Currency, r.Service.GroupPrice),
GroupSize: bcgo.DecimalSizeToString(uint64(r.Service.GroupSize)),
Interval: r.Service.Interval.String(),
Mode: r.Service.Mode.String(),
CostExamples: make([][]string, 0),
}
for _, s := range []uint64{
0,
1234,
800000000,
28000000000,
564000000000,
1200000000000,
} {
data.CostExamples = append(data.CostExamples, []string{
bcgo.DecimalSizeToString(s),
bcgo.MoneyToString(r.Service.Currency, int64(math.Ceil(float64(s)/float64(r.Service.GroupSize)))*r.Service.GroupPrice),
})
}
err = template.Execute(w, data)
if err != nil {
log.Println(err)
return
}
} else {
w.WriteHeader(http.StatusNotFound)
}
default:
log.Println("Unsupported method", r.Method)
}
}
}
func RegistrarListHandler(template *template.Template, list func() []*spacego.Registrar) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Proto, r.Method, r.Host, r.URL.Path)
switch r.Method {
case "GET":
type TemplateRegistrar struct {
Alias string
Domain string
Country string
Currency string
Cost string
}
registrars := make([]*TemplateRegistrar, 0)
for _, registrar := range list() {
registrars = append(registrars, &TemplateRegistrar{
Alias: registrar.Merchant.Alias,
Domain: registrar.Merchant.Domain,
Country: registrar.Service.Country,
Currency: registrar.Service.Currency,
Cost: bcgo.MoneyToString(registrar.Service.Currency, registrar.Service.GroupPrice) + "/" + bcgo.DecimalSizeToString(uint64(registrar.Service.GroupSize)),
})
}
data := struct {
Registrar []*TemplateRegistrar
}{
Registrar: registrars,
}
if err := template.Execute(w, data); err != nil {
log.Println(err)
return
}
default:
log.Println("Unsupported method", r.Method)
}
}
}