1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: display api key on user interface (resolve #24)

This commit is contained in:
Ferdinand Mütsch
2020-05-24 21:19:05 +02:00
parent b92a064eb1
commit 2cca2cb0bb
8 changed files with 90 additions and 45 deletions

View File

@@ -182,33 +182,3 @@ func (h *IndexHandler) handlePostSignup(w http.ResponseWriter, r *http.Request)
msg := url.QueryEscape("account created successfully")
http.Redirect(w, r, fmt.Sprintf("%s/?success=%s", h.config.BasePath, msg), http.StatusFound)
}
func respondAlert(w http.ResponseWriter, error, success, tplName string, status int) {
w.WriteHeader(status)
if tplName == "" {
tplName = "index.tpl.html"
}
templates[tplName].Execute(w, struct {
Error string
Success string
}{Error: error})
}
// TODO: do better
func handleAlerts(w http.ResponseWriter, r *http.Request, tplName string) bool {
if err := r.URL.Query().Get("error"); err != "" {
if err == "unauthorized" {
respondAlert(w, err, "", tplName, http.StatusUnauthorized)
} else {
respondAlert(w, err, "", tplName, http.StatusInternalServerError)
}
return true
}
if success := r.URL.Query().Get("success"); success != "" {
respondAlert(w, "", success, tplName, http.StatusOK)
return true
}
return false
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/muety/wakapi/utils"
"html/template"
"io/ioutil"
"net/http"
"path"
"strings"
)
@@ -48,3 +49,33 @@ func loadTemplates() {
templates[tplName] = tpl
}
}
func respondAlert(w http.ResponseWriter, error, success, tplName string, status int) {
w.WriteHeader(status)
if tplName == "" {
tplName = "index.tpl.html"
}
templates[tplName].Execute(w, struct {
Error string
Success string
}{Error: error})
}
// TODO: do better
func handleAlerts(w http.ResponseWriter, r *http.Request, tplName string) bool {
if err := r.URL.Query().Get("error"); err != "" {
if err == "unauthorized" {
respondAlert(w, err, "", tplName, http.StatusUnauthorized)
} else {
respondAlert(w, err, "", tplName, http.StatusInternalServerError)
}
return true
}
if success := r.URL.Query().Get("success"); success != "" {
respondAlert(w, "", success, tplName, http.StatusOK)
return true
}
return false
}

View File

@@ -20,19 +20,19 @@ const (
)
type SummaryHandler struct {
cummarySrvc *services.SummaryService
summarySrvc *services.SummaryService
config *models.Config
}
func NewSummaryHandler(summaryService *services.SummaryService) *SummaryHandler {
return &SummaryHandler{
cummarySrvc: summaryService,
summarySrvc: summaryService,
config: models.GetConfig(),
}
}
func (h *SummaryHandler) ApiGet(w http.ResponseWriter, r *http.Request) {
summary, err, status := loadUserSummary(r, h.cummarySrvc)
summary, err, status := loadUserSummary(r, h.summarySrvc)
if err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
@@ -53,16 +53,22 @@ func (h *SummaryHandler) Index(w http.ResponseWriter, r *http.Request) {
r.URL.RawQuery = q.Encode()
}
summary, err, status := loadUserSummary(r, h.cummarySrvc)
summary, err, status := loadUserSummary(r, h.summarySrvc)
if err != nil {
w.WriteHeader(status)
w.Write([]byte(err.Error()))
respondAlert(w, err.Error(), "", "summary.tpl.html", status)
return
}
user := r.Context().Value(models.UserKey).(*models.User)
if user == nil {
respondAlert(w, "unauthorized", "", "summary.tpl.html", http.StatusUnauthorized)
return
}
vm := models.SummaryViewModel{
Summary: summary,
LanguageColors: utils.FilterLanguageColors(h.config.LanguageColors, summary),
ApiKey: user.ApiKey,
}
templates["summary.tpl.html"].Execute(w, vm)