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
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.