generated from ViBiOh/goweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding message, refactoring getting started and renderer pkg (#16)
- Loading branch information
Showing
20 changed files
with
501 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ func (a app) checkUpdates(_ time.Time) error { | |
newReleases = append(newReleases, release) | ||
|
||
if _, err = a.targetApp.Update(context.Background(), target); err != nil { | ||
logger.Error("unable to update target %s: %s", target.Repository, err) | ||
return fmt.Errorf("unable to update target %s: %s", target.Repository, err) | ||
} | ||
} else if release.TagName == target.CurrentVersion { | ||
logger.Info("%s is up-to-date!", target.Repository) | ||
|
@@ -96,8 +96,15 @@ func (a app) checkUpdates(_ time.Time) error { | |
if len(newReleases) > 0 { | ||
if a.mailerApp == nil || !a.mailerApp.Enabled() { | ||
logger.Warn("mailer is not configured") | ||
} else if err := mailer.NewEmail(a.mailerApp).Template("ketchup").From("[email protected]").As("Ketchup").WithSubject("Ketchup - New update").To(a.emailTo).Data(newReleases).Send(context.Background()); err != nil { | ||
logger.Error("unable to send email to %s: %s", a.emailTo, err) | ||
return nil | ||
} | ||
|
||
payload := map[string]interface{}{ | ||
"targets": newReleases, | ||
} | ||
|
||
if err := mailer.NewEmail(a.mailerApp).Template("ketchup").From("[email protected]").As("Ketchup").WithSubject("Ketchup - New update").To(a.emailTo).Data(payload).Send(context.Background()); err != nil { | ||
return fmt.Errorf("unable to send email to %s: %s", a.emailTo, err) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package renderer | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/ViBiOh/httputils/v3/pkg/httperror" | ||
"github.com/ViBiOh/httputils/v3/pkg/logger" | ||
"github.com/ViBiOh/httputils/v3/pkg/templates" | ||
) | ||
|
||
func (a app) getData(r *http.Request) (interface{}, error) { | ||
targets, _, err := a.targetApp.List(r.Context(), 1, 100, "", false, nil) | ||
|
||
return targets, err | ||
} | ||
|
||
func (a app) uiHandler(w http.ResponseWriter, r *http.Request, status int, message Message) { | ||
targets, err := a.getData(r) | ||
if err != nil { | ||
a.errorHandler(w, http.StatusInternalServerError, err, nil) | ||
return | ||
} | ||
|
||
content := map[string]interface{}{ | ||
"Version": a.version, | ||
"Targets": targets, | ||
} | ||
|
||
if len(message.Content) > 0 { | ||
content["Message"] = message | ||
} | ||
|
||
if err := templates.ResponseHTMLTemplate(a.tpl.Lookup("app"), w, content, status); err != nil { | ||
httperror.InternalServerError(w, err) | ||
} | ||
} | ||
|
||
func (a app) errorHandler(w http.ResponseWriter, status int, errs ...error) { | ||
logger.Error("%s", errs) | ||
|
||
content := map[string]interface{}{ | ||
"Version": a.version, | ||
} | ||
|
||
if len(errs) > 0 { | ||
content["Message"] = Message{ | ||
Level: "error", | ||
Content: errs[0].Error(), | ||
} | ||
|
||
if len(errs) > 1 { | ||
content["Errors"] = errs[1:] | ||
} | ||
} | ||
|
||
if err := templates.ResponseHTMLTemplate(a.tpl.Lookup("error"), w, content, status); err != nil { | ||
httperror.InternalServerError(w, err) | ||
return | ||
} | ||
} | ||
|
||
func (a app) svg() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
tpl := a.tpl.Lookup(fmt.Sprintf("svg-%s", strings.Trim(r.URL.Path, "/"))) | ||
if tpl == nil { | ||
httperror.NotFound(w) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "image/svg+xml") | ||
if err := templates.WriteTemplate(tpl, w, r.URL.Query().Get("fill"), "text/xml"); err != nil { | ||
httperror.InternalServerError(w, err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package renderer | ||
|
||
// Message for render | ||
type Message struct { | ||
Level string | ||
Content string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.