This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
146 lines (118 loc) · 3.78 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
// get the index template from embedded filesystem
func (s *server) getIndexTemplate(FSS fs.FS) *template.Template {
// get the html template from dist, have it ready for requests
tmplContent, ioErr := fs.ReadFile(FSS, "static/index.html")
if ioErr != nil {
log.Println("Error opening index template")
if !embedUseOS {
log.Fatal(ioErr.Error())
}
}
tmplString := string(tmplContent)
tmpl, tmplErr := template.New("index").Parse(tmplString)
if tmplErr != nil {
log.Println("Error parsing index template")
if !embedUseOS {
log.Fatal(tmplErr.Error())
}
}
return tmpl
}
// handleIndex parses the index html file, injecting any relevant data
func (s *server) handleIndex(FSS fs.FS) http.HandlerFunc {
type AppConfig struct {
ToastTimeout int
DefaultLocale string
AppVersion string
PathPrefix string
}
type UIConfig struct {
AppConfig AppConfig
}
tmpl := s.getIndexTemplate(FSS)
appConfig := AppConfig{
ToastTimeout: viper.GetInt("config.toast_timeout"),
DefaultLocale: viper.GetString("config.default_locale"),
AppVersion: s.config.Version,
PathPrefix: s.config.PathPrefix,
}
data := UIConfig{
AppConfig: appConfig,
}
return func(w http.ResponseWriter, r *http.Request) {
if embedUseOS {
tmpl = s.getIndexTemplate(FSS)
}
tmpl.Execute(w, data)
}
}
// GetHostnames route handler gets the hostnames json array
// and responds with the JSON output
// @TODO - add error handling
func GetHostnames(w http.ResponseWriter, r *http.Request) {
var hostnames = getHostnamesFromJSON()
// if err != nil {
// respondWithError(w, http.StatusInternalServerError, err.Error())
// return
// }
respondWithJSON(w, http.StatusOK, hostnames)
}
// GetHostname route handler gets the hostname by Domain
// and responds with the JSON output
// @TODO - add error handling
func GetHostname(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var hostname = getHostnameByDomain(params["domain"])
respondWithJSON(w, http.StatusOK, hostname)
}
// CreateHostname route handler attempts to add the hostname to
// hostnames array and responds with the updated hostnames JSON array output
// @TODO - add error handling
func CreateHostname(w http.ResponseWriter, r *http.Request) {
var hostname Hostname
_ = json.NewDecoder(r.Body).Decode(&hostname)
var hostnames = createHostname(hostname)
respondWithJSON(w, http.StatusOK, hostnames)
}
// UpdateHostname route handler attempts to update the hostname in
// hostnames array and responds with the updated hostnames JSON array output
// @TODO - add error handling
func UpdateHostname(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var updatedHostname Hostname
_ = json.NewDecoder(r.Body).Decode(&updatedHostname)
updatedHostname.Domain = params["domain"]
var hostnames = updateHostname(updatedHostname)
respondWithJSON(w, http.StatusOK, hostnames)
}
// DeleteHostname route handler attempts to delete the hostname from
// hostnames array and responds with the updated hostnames JSON array output
// @TODO - add error handling
func DeleteHostname(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var domain = params["domain"]
var hostnames = deleteHostname(domain)
respondWithJSON(w, http.StatusOK, hostnames)
}
// TriggerUpdate route handler triggers the attemptIPAddressUpdates job
// and responds with a string message to let the caller know it was triggered
// @TODO - add error handling
func TriggerUpdate(w http.ResponseWriter, r *http.Request) {
attemptIPAddressUpdates()
// if err != nil {
// respondWithError(w, http.StatusInternalServerError, err.Error())
// return
// }
fmt.Fprintf(w, "Update Triggered")
}