Adds api
This commit is contained in:
67
cmd/tpapi/httpTemplates.go
Normal file
67
cmd/tpapi/httpTemplates.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func tmpltError(w http.ResponseWriter, s int, m string) {
|
||||
var (
|
||||
output []byte
|
||||
o = struct {
|
||||
Error bool `json:"error" yaml:"error"`
|
||||
ErrorMsg string `json:"errorMessage" yaml:"errorMessage"`
|
||||
}{
|
||||
Error: true,
|
||||
ErrorMsg: m,
|
||||
}
|
||||
err error
|
||||
)
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
output, err = json.MarshalIndent(o, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[TRACE] Unable to marshal error message: %v.", err)
|
||||
}
|
||||
w.WriteHeader(s)
|
||||
w.Write(output) //nolint:errcheck
|
||||
}
|
||||
|
||||
func tmpltWebRoot(w http.ResponseWriter) {
|
||||
o := struct {
|
||||
Application string `json:"application" yaml:"application"`
|
||||
Description string `json:"description" yaml:"description"`
|
||||
Version string `json:"version" yaml:"version"`
|
||||
}{
|
||||
Application: "TP-Link Web API",
|
||||
Description: "API for automated calls to TP-Link devices",
|
||||
Version: "v1.0.0",
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
output, err := json.MarshalIndent(o, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[TRACE] Unable to marshal error message: %v.", err)
|
||||
}
|
||||
w.Write(output) //nolint:errcheck
|
||||
}
|
||||
|
||||
func tmpltHealthCheck(w http.ResponseWriter) {
|
||||
o := struct {
|
||||
WebServer bool `json:"webServerActive" yaml:"webServerActive"`
|
||||
Status string `json:"status" yaml:"status"`
|
||||
}{
|
||||
WebServer: true,
|
||||
Status: "healthy",
|
||||
}
|
||||
|
||||
output, err := json.MarshalIndent(o, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[TRACE] Unable to marshal status message: %v.", err)
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(output) //nolint:errcheck
|
||||
}
|
Reference in New Issue
Block a user