forked from goffee/core
Merge pull request 'develop' (#6) from develop into main
Reviewed-on: goffee/core#6
This commit is contained in:
commit
b9cd82867b
38 changed files with 695 additions and 61 deletions
39
context.go
39
context.go
|
@ -68,10 +68,49 @@ func (c *Context) RequestParamExists(key string) bool {
|
|||
return c.Request.httpRequest.Form.Has(key)
|
||||
}
|
||||
|
||||
func (c *Context) GetRequesForm() interface{} {
|
||||
return c.Request.httpRequest.Form
|
||||
}
|
||||
|
||||
func (c *Context) GetRequesBodyMap() map[string]interface{} {
|
||||
var dat map[string]any
|
||||
body := c.Request.httpRequest.Body
|
||||
if body != nil {
|
||||
if content, err := io.ReadAll(body); err == nil {
|
||||
json.Unmarshal(content, &dat)
|
||||
}
|
||||
}
|
||||
return dat
|
||||
}
|
||||
|
||||
// get json body and bind to dest interface
|
||||
func (c *Context) GetRequesBodyStruct(dest interface{}) error {
|
||||
body := c.Request.httpRequest.Body
|
||||
if body != nil {
|
||||
value := reflect.ValueOf(dest)
|
||||
if value.Kind() != reflect.Ptr {
|
||||
fmt.Println("dest is not a pointer")
|
||||
return errors.New("dest is not a pointer")
|
||||
}
|
||||
err := json.NewDecoder(body).Decode(dest)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Context) GetHeader(key string) string {
|
||||
return c.Request.httpRequest.Header.Get(key)
|
||||
}
|
||||
|
||||
func (c *Context) GetCookie() (UserCookie, error) {
|
||||
|
||||
user, err := GetCookie(c.Request.httpRequest)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (c *Context) GetUploadedFile(name string) *UploadedFileInfo {
|
||||
file, fileHeader, err := c.Request.httpRequest.FormFile(name)
|
||||
if err != nil {
|
||||
|
|
271
cookies.go
Normal file
271
cookies.go
Normal file
|
@ -0,0 +1,271 @@
|
|||
// Copyright (c) 2024 Zeni Kim <zenik@smarteching.com>
|
||||
// Use of this source code is governed by MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrValueTooLong = errors.New("cookie value too long")
|
||||
ErrInvalidValue = errors.New("invalid cookie value")
|
||||
)
|
||||
|
||||
// Declare the User type.
|
||||
type UserCookie struct {
|
||||
Email string
|
||||
Token string
|
||||
}
|
||||
|
||||
var secretcookie []byte
|
||||
|
||||
func GetCookie(r *http.Request) (UserCookie, error) {
|
||||
|
||||
var err error
|
||||
// Create a new instance of a User type.
|
||||
var user UserCookie
|
||||
|
||||
// check if template engine is enable
|
||||
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
|
||||
if TemplateEnableStr == "" {
|
||||
TemplateEnableStr = "false"
|
||||
}
|
||||
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
|
||||
// if enabled,
|
||||
if TemplateEnable {
|
||||
cookie_secret := os.Getenv("COOKIE_SECRET")
|
||||
if cookie_secret == "" {
|
||||
panic("cookie secret key is not set")
|
||||
}
|
||||
secretcookie, err = hex.DecodeString(cookie_secret)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
} else {
|
||||
panic("Templates are disabled")
|
||||
}
|
||||
|
||||
gobEncodedValue, err := CookieReadEncrypted(r, "goffee", secretcookie)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
|
||||
// Create an strings.Reader containing the gob-encoded value.
|
||||
reader := strings.NewReader(gobEncodedValue)
|
||||
|
||||
// Decode it into the User type. Notice that we need to pass a *pointer* to
|
||||
// the Decode() target here?
|
||||
if err := gob.NewDecoder(reader).Decode(&user); err != nil {
|
||||
return user, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
|
||||
}
|
||||
|
||||
func SetCookie(w http.ResponseWriter, email string, token string) error {
|
||||
// Initialize a User struct containing the data that we want to store in the
|
||||
// cookie.
|
||||
var err error
|
||||
|
||||
// check if template engine is enable
|
||||
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
|
||||
if TemplateEnableStr == "" {
|
||||
TemplateEnableStr = "false"
|
||||
}
|
||||
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
|
||||
// if enabled,
|
||||
if TemplateEnable {
|
||||
cookie_secret := os.Getenv("COOKIE_SECRET")
|
||||
if cookie_secret == "" {
|
||||
panic("cookie secret key is not set")
|
||||
}
|
||||
secretcookie, err = hex.DecodeString(cookie_secret)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
panic("Templates are disabled")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user := UserCookie{Email: email, Token: token}
|
||||
|
||||
// Initialize a buffer to hold the gob-encoded data.
|
||||
var buf bytes.Buffer
|
||||
|
||||
// Gob-encode the user data, storing the encoded output in the buffer.
|
||||
err = gob.NewEncoder(&buf).Encode(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Call buf.String() to get the gob-encoded value as a string and set it as
|
||||
// the cookie value.
|
||||
cookie := http.Cookie{
|
||||
Name: "goffee",
|
||||
Value: buf.String(),
|
||||
Path: "/",
|
||||
MaxAge: 3600,
|
||||
HttpOnly: true,
|
||||
Secure: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
}
|
||||
|
||||
// Write an encrypted cookie containing the gob-encoded data as normal.
|
||||
err = CookieWriteEncrypted(w, cookie, secretcookie)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CookieWrite(w http.ResponseWriter, cookie http.Cookie) error {
|
||||
// Encode the cookie value using base64.
|
||||
cookie.Value = base64.URLEncoding.EncodeToString([]byte(cookie.Value))
|
||||
|
||||
// Check the total length of the cookie contents. Return the ErrValueTooLong
|
||||
// error if it's more than 4096 bytes.
|
||||
if len(cookie.String()) > 4096 {
|
||||
return ErrValueTooLong
|
||||
}
|
||||
|
||||
// Write the cookie as normal.
|
||||
http.SetCookie(w, &cookie)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CookieRead(r *http.Request, name string) (string, error) {
|
||||
// Read the cookie as normal.
|
||||
cookie, err := r.Cookie(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Decode the base64-encoded cookie value. If the cookie didn't contain a
|
||||
// valid base64-encoded value, this operation will fail and we return an
|
||||
// ErrInvalidValue error.
|
||||
value, err := base64.URLEncoding.DecodeString(cookie.Value)
|
||||
if err != nil {
|
||||
return "", ErrInvalidValue
|
||||
}
|
||||
|
||||
// Return the decoded cookie value.
|
||||
return string(value), nil
|
||||
}
|
||||
|
||||
func CookieWriteEncrypted(w http.ResponseWriter, cookie http.Cookie, secretKey []byte) error {
|
||||
// Create a new AES cipher block from the secret key.
|
||||
block, err := aes.NewCipher(secretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wrap the cipher block in Galois Counter Mode.
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a unique nonce containing 12 random bytes.
|
||||
nonce := make([]byte, aesGCM.NonceSize())
|
||||
_, err = io.ReadFull(rand.Reader, nonce)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Prepare the plaintext input for encryption. Because we want to
|
||||
// authenticate the cookie name as well as the value, we make this plaintext
|
||||
// in the format "{cookie name}:{cookie value}". We use the : character as a
|
||||
// separator because it is an invalid character for cookie names and
|
||||
// therefore shouldn't appear in them.
|
||||
plaintext := fmt.Sprintf("%s:%s", cookie.Name, cookie.Value)
|
||||
|
||||
// Encrypt the data using aesGCM.Seal(). By passing the nonce as the first
|
||||
// parameter, the encrypted data will be appended to the nonce — meaning
|
||||
// that the returned encryptedValue variable will be in the format
|
||||
// "{nonce}{encrypted plaintext data}".
|
||||
encryptedValue := aesGCM.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
|
||||
// Set the cookie value to the encryptedValue.
|
||||
cookie.Value = string(encryptedValue)
|
||||
|
||||
// Write the cookie as normal.
|
||||
return CookieWrite(w, cookie)
|
||||
}
|
||||
|
||||
func CookieReadEncrypted(r *http.Request, name string, secretKey []byte) (string, error) {
|
||||
// Read the encrypted value from the cookie as normal.
|
||||
encryptedValue, err := CookieRead(r, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create a new AES cipher block from the secret key.
|
||||
block, err := aes.NewCipher(secretKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Wrap the cipher block in Galois Counter Mode.
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Get the nonce size.
|
||||
nonceSize := aesGCM.NonceSize()
|
||||
|
||||
// To avoid a potential 'index out of range' panic in the next step, we
|
||||
// check that the length of the encrypted value is at least the nonce
|
||||
// size.
|
||||
if len(encryptedValue) < nonceSize {
|
||||
return "", ErrInvalidValue
|
||||
}
|
||||
|
||||
// Split apart the nonce from the actual encrypted data.
|
||||
nonce := encryptedValue[:nonceSize]
|
||||
ciphertext := encryptedValue[nonceSize:]
|
||||
|
||||
// Use aesGCM.Open() to decrypt and authenticate the data. If this fails,
|
||||
// return a ErrInvalidValue error.
|
||||
plaintext, err := aesGCM.Open(nil, []byte(nonce), []byte(ciphertext), nil)
|
||||
if err != nil {
|
||||
return "", ErrInvalidValue
|
||||
}
|
||||
|
||||
// The plaintext value is in the format "{cookie name}:{cookie value}". We
|
||||
// use strings.Cut() to split it on the first ":" character.
|
||||
expectedName, value, ok := strings.Cut(string(plaintext), ":")
|
||||
if !ok {
|
||||
return "", ErrInvalidValue
|
||||
}
|
||||
|
||||
// Check that the cookie name is the expected one and hasn't been changed.
|
||||
if expectedName != name {
|
||||
return "", ErrInvalidValue
|
||||
}
|
||||
|
||||
// Return the plaintext cookie value.
|
||||
return value, nil
|
||||
}
|
8
core.go
8
core.go
|
@ -98,9 +98,7 @@ func (app *App) Run(router *httprouter.Router) {
|
|||
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
|
||||
// if enabled,
|
||||
if TemplateEnable {
|
||||
// add public path
|
||||
publicPath := os.Getenv("TEMPLATE_PUBLIC")
|
||||
router.ServeFiles("/public/*filepath", http.Dir(publicPath))
|
||||
router.ServeFiles("/public/*filepath", http.Dir("storage/public"))
|
||||
}
|
||||
|
||||
useHttpsStr := os.Getenv("App_USE_HTTPS")
|
||||
|
@ -183,6 +181,7 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr
|
|||
|
||||
func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
|
||||
ctx := &Context{
|
||||
Request: &Request{
|
||||
httpRequest: r,
|
||||
|
@ -206,11 +205,13 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
|
|||
GetEventsManager: resolveEventsManager(),
|
||||
GetLogger: resolveLogger(),
|
||||
}
|
||||
|
||||
ctx.prepare(ctx)
|
||||
rhs := app.combHandlers(h, ms)
|
||||
app.prepareChain(rhs)
|
||||
app.t = 0
|
||||
app.chain.execute(ctx)
|
||||
|
||||
for _, header := range ctx.Response.headers {
|
||||
w.Header().Add(header.key, header.val)
|
||||
}
|
||||
|
@ -223,6 +224,7 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
|
|||
} else {
|
||||
ct = CONTENT_TYPE_HTML
|
||||
}
|
||||
|
||||
w.Header().Add(CONTENT_TYPE, ct)
|
||||
if ctx.Response.statusCode != 0 {
|
||||
w.WriteHeader(ctx.Response.statusCode)
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package components
|
||||
|
||||
type Button struct {
|
||||
Text string
|
||||
Link string
|
||||
Icon string
|
||||
IsSubmit bool
|
||||
IsPrimary bool
|
||||
IsDisabled bool
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
{{define "button"}}
|
||||
<button class="button-container">
|
||||
<a class="button {{if .IsPrimary}}primary{{end}}" {{if eq .IsSubmit true}}type="submit"{{else}}href="{{.Link}}"{{end}}>
|
||||
{{.Text}}
|
||||
<!-- tailwind heroicons -->
|
||||
{{if eq .Icon "gear"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z" />
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
|
||||
</svg>
|
||||
{{else if eq .Icon "arrow-right"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
|
||||
</svg>
|
||||
{{else if eq .Icon "plus"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
|
||||
</svg>
|
||||
{{end}}
|
||||
</a>
|
||||
</button>
|
||||
{{end}}
|
7
template/components/content_badge.go
Normal file
7
template/components/content_badge.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package components
|
||||
|
||||
type ContentBadge struct {
|
||||
Text string
|
||||
TypeClass string // type primary, secondary, success, danger, warning, info, light, dark, (default secondary)
|
||||
IsOutline bool
|
||||
}
|
7
template/components/content_badge.html
Normal file
7
template/components/content_badge.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
{{define "content_badge"}}
|
||||
<span class="badge {{if eq .IsOutline true}}
|
||||
border border-{{.TypeClass}} bg-{{.TypeClass}}-subtle text-{{.TypeClass}}-emphasis
|
||||
{{else}}
|
||||
text-bg-{{if .TypeClass}}{{.TypeClass}}{{else}}secondary{{end}}
|
||||
{{end}} rounded-pill ">{{.Text}}</span>
|
||||
{{end}}
|
15
template/components/content_dropdown.go
Normal file
15
template/components/content_dropdown.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package components
|
||||
|
||||
type ContentDropdown struct {
|
||||
Label string
|
||||
TypeClass string // type primary, secondary, success, danger, warning, info, light, dark, link, outline-primary
|
||||
IsDisabled bool
|
||||
Items []ContentDropdownItem
|
||||
}
|
||||
|
||||
type ContentDropdownItem struct {
|
||||
Text string
|
||||
Link string
|
||||
IsDisabled bool
|
||||
IsActive bool
|
||||
}
|
16
template/components/content_dropdown.html
Normal file
16
template/components/content_dropdown.html
Normal file
|
@ -0,0 +1,16 @@
|
|||
{{define "content_dropdown_item"}}
|
||||
<li><a class="dropdown-item {{if eq .IsActive true}}active{{end}} {{if eq .IsDisabled true}}disabled{{end}}" href="{{.Link}}">{{.Text}}</a></li>
|
||||
{{end}}
|
||||
|
||||
{{define "content_dropdown"}}
|
||||
<div class="dropdown">
|
||||
<button class="btn btn-{{.TypeClass}} dropdown-toggle {{if eq .IsDisabled true}}disabled{{end}}" type="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
{{.Label}}
|
||||
</button>
|
||||
<ul class="dropdown-menu">
|
||||
{{ range .Items}}
|
||||
{{template "content_dropdown_item" .}}
|
||||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
{{end}}
|
10
template/components/content_href.go
Normal file
10
template/components/content_href.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package components
|
||||
|
||||
type ContentHref struct {
|
||||
Text string
|
||||
Link string
|
||||
Icon string
|
||||
IsButton bool
|
||||
TypeClass string // type primary, secondary, success, danger, warning, info, light, dark, link
|
||||
IsDisabled bool
|
||||
}
|
6
template/components/content_href.html
Normal file
6
template/components/content_href.html
Normal file
|
@ -0,0 +1,6 @@
|
|||
{{define "content_href"}}
|
||||
<a class="{{if eq .IsButton true}} btn btn-{{.TypeClass}}{{end}} {{if eq .IsDisabled true}}disabled{{end}}"
|
||||
href="{{.Link}}" {{if eq .IsButton true}}role="button"{{end}} {{if eq .IsDisabled true}}aria-disabled="true"{{end}}>
|
||||
{{.Text}}
|
||||
</a>
|
||||
{{end}}
|
19
template/components/content_list.go
Normal file
19
template/components/content_list.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package components
|
||||
|
||||
type ContentList struct {
|
||||
Items []ContentListItem
|
||||
}
|
||||
|
||||
type ContentListItem struct {
|
||||
Text string
|
||||
Description string
|
||||
EndElement string
|
||||
//Link string
|
||||
TypeClass string // primary, secondary, success, danger, warning, info, light, dark
|
||||
IsDisabled bool
|
||||
//IsActive bool
|
||||
}
|
||||
|
||||
//link
|
||||
//border
|
||||
// badge
|
14
template/components/content_list.html
Normal file
14
template/components/content_list.html
Normal file
|
@ -0,0 +1,14 @@
|
|||
{{define "content_list"}}
|
||||
<ul class="list-group">
|
||||
{{ range .Items}}
|
||||
<li class="list-group-item text-wrap {{if eq .IsDisabled true}}disabled{{end}} {{if .TypeClass}}list-group-item-{{.TypeClass}}{{end}}"
|
||||
{{if eq .IsDisabled true}}aria-disabled="true"{{end}}
|
||||
><div class="d-flex justify-content-between lh-sm"><p class="mb-1">{{.Text}}</p><span class="text-body-secondary ms-5">{{.EndElement}}</span></div>
|
||||
<small>{{.Description}}</small>
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
{{end}}
|
20
template/components/content_table.go
Normal file
20
template/components/content_table.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package components
|
||||
|
||||
type ContentTable struct {
|
||||
ID string
|
||||
TableClass string // table-primary, table-secondary,.. table-striped table-bordered
|
||||
HeadClass string // table-dark table-light
|
||||
AllTH []ContentTableTH
|
||||
AllTD [][]ContentTableTD
|
||||
}
|
||||
|
||||
type ContentTableTH struct {
|
||||
ID string
|
||||
ValueType string // -> default string, href, badge
|
||||
Value string
|
||||
}
|
||||
|
||||
type ContentTableTD struct {
|
||||
ID string
|
||||
Value interface{} // string or component struct according ValueType
|
||||
}
|
25
template/components/content_table.html
Normal file
25
template/components/content_table.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
{{define "content_table"}}
|
||||
<table class="table table-hover {{.TableClass}}" {{ if .ID }}id="{{.ID}}"{{end}}>
|
||||
<thead class="{{.HeadClass}}">
|
||||
<tr>
|
||||
{{range $index, $col := .AllTH}}<th {{ if .ID }}id="{{.ID}}"{{end}} scope="col">{{ $col.Value }}</th>{{end}}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{- range .AllTD}}<tr scope="row">
|
||||
{{range $index, $item := .}}<td {{ if $item.ID }}id="{{$item.ID}}"{{end}}>
|
||||
{{ with $x := index $.AllTH $index }}
|
||||
{{ if eq $x.ValueType "href"}}
|
||||
{{template "content_href" $item.Value}}
|
||||
{{ else if eq $x.ValueType "badge"}}
|
||||
{{template "content_badge" $item.Value}}
|
||||
{{ else }}
|
||||
{{ $item.Value }}
|
||||
{{end}}
|
||||
{{end}}
|
||||
</td>
|
||||
{{end}}</tr>
|
||||
{{- end}}
|
||||
</tbody>
|
||||
</table>
|
||||
{{end}}
|
9
template/components/form_button.go
Normal file
9
template/components/form_button.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package components
|
||||
|
||||
type FormButton struct {
|
||||
Text string
|
||||
Icon string
|
||||
IsSubmit bool
|
||||
TypeClass string // type primary, secondary, success, danger, warning, info, light, dark, link, outline-primary
|
||||
IsDisabled bool
|
||||
}
|
20
template/components/form_button.html
Normal file
20
template/components/form_button.html
Normal file
|
@ -0,0 +1,20 @@
|
|||
{{define "form_button"}}
|
||||
<button class="btn btn-{{.TypeClass}}" {{if eq .IsSubmit true}}type="submit"{{else}}type="button"{{end}} {{if .IsDisabled}}disabled{{end}}>
|
||||
{{.Text}}
|
||||
</button>
|
||||
<!-- tailwind heroicons -->
|
||||
{{if eq .Icon "gear"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z" />
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
|
||||
</svg>
|
||||
{{else if eq .Icon "arrow-right"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
|
||||
</svg>
|
||||
{{else if eq .Icon "plus"}}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
|
||||
</svg>
|
||||
{{end}}
|
||||
{{end}}
|
14
template/components/form_checkbox.go
Normal file
14
template/components/form_checkbox.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package components
|
||||
|
||||
type FormCheckbox struct {
|
||||
Label string
|
||||
AllCheckbox []FormCheckboxItem
|
||||
}
|
||||
|
||||
type FormCheckboxItem struct {
|
||||
ID string
|
||||
Name string
|
||||
Value string
|
||||
Label string
|
||||
IsChecked bool
|
||||
}
|
11
template/components/form_checkbox.html
Normal file
11
template/components/form_checkbox.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{{define "form_checkbox"}}
|
||||
<div class="input-container">
|
||||
<label class="form-label">{{.Label}}</label>
|
||||
{{range $options := .AllCheckbox}}
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="{{$options.Name}}" id="{{$options.ID}}" value="{{$options.Value}}"{{if eq $options.IsChecked true}} checked{{end}}>
|
||||
<label class="form-check-label" for="{{$options.ID}}">{{$options.Label}}</label>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
13
template/components/form_input.go
Normal file
13
template/components/form_input.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package components
|
||||
|
||||
type FormInput struct {
|
||||
ID string
|
||||
Label string
|
||||
Type string
|
||||
Placeholder string
|
||||
Value string
|
||||
Hint string
|
||||
Error string
|
||||
IsDisabled bool
|
||||
IsRequired bool
|
||||
}
|
18
template/components/form_input.html
Normal file
18
template/components/form_input.html
Normal file
|
@ -0,0 +1,18 @@
|
|||
{{define "form_input"}}
|
||||
<div class="input-container">
|
||||
<label class="form-label" for="{{.ID}}">{{.Label}}</label>
|
||||
<input type="{{.Type}}" id="{{.ID}}" name="{{.ID}}" placeholder="{{.Placeholder}}" class="form-control{{if ne .Error ""}} error{{end}}"
|
||||
{{if eq .IsDisabled true}}
|
||||
disabled
|
||||
{{end}}
|
||||
{{if eq .IsRequired true}}
|
||||
required
|
||||
{{end}}
|
||||
{{if ne .Value ""}}
|
||||
value="{{.Value}}"
|
||||
{{end}}
|
||||
aria-describedby="{{.ID}}Help">
|
||||
{{if ne .Hint ""}}<small id="{{.ID}}Help" class="form-text text-muted">{{.Hint}}</small>{{end}}
|
||||
{{if ne .Error ""}}<div class="error">{{.Error}}</div>{{end}}
|
||||
</div>
|
||||
{{end}}
|
15
template/components/form_radio.go
Normal file
15
template/components/form_radio.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package components
|
||||
|
||||
type FormRadio struct {
|
||||
Label string
|
||||
AllRadios []FormRadioItem
|
||||
}
|
||||
|
||||
type FormRadioItem struct {
|
||||
ID string
|
||||
Name string
|
||||
Value string
|
||||
Label string
|
||||
IsDisabled bool
|
||||
IsChecked bool
|
||||
}
|
11
template/components/form_radio.html
Normal file
11
template/components/form_radio.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{{define "form_radio"}}
|
||||
<div class="input-container">
|
||||
<label class="form-label">{{.Label}}</label>
|
||||
{{range $options := .AllRadios}}
|
||||
<div class="form-check{{if eq $options.IsDisabled true}} disabled{{end}}">
|
||||
<input class="form-check-input" type="radio" name="{{$options.Name}}" id="{{$options.ID}}" value="{{$options.Value}}"{{if eq $options.IsDisabled true}} disabled=""{{end}}{{if eq $options.IsChecked true}} checked=""{{end}}>
|
||||
<label class="form-check-label" for="{{$options.ID}}">{{$options.Label}}</label>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
13
template/components/form_select.go
Normal file
13
template/components/form_select.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package components
|
||||
|
||||
type FormSelect struct {
|
||||
ID string
|
||||
SelectedOption FormSelectOption
|
||||
Label string
|
||||
AllOptions []FormSelectOption
|
||||
}
|
||||
|
||||
type FormSelectOption struct {
|
||||
Value string
|
||||
Caption string
|
||||
}
|
10
template/components/form_select.html
Normal file
10
template/components/form_select.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
{{define "form_select"}}
|
||||
<div class="input-container">
|
||||
<label for="{{.ID}}" class="form-label">{{.Label}}</label>
|
||||
<select class="form-select" id="{{.ID}}" name="{{.ID}}">
|
||||
{{range $options := .AllOptions}}
|
||||
<option value="{{$options.Value}}" {{if eq $options.Value $.SelectedOption.Value }}selected="selected"{{end}}>{{$options.Caption}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
</div>
|
||||
{{end}}
|
7
template/components/form_textarea.go
Normal file
7
template/components/form_textarea.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package components
|
||||
|
||||
type FormTextarea struct {
|
||||
ID string
|
||||
Label string
|
||||
Value string
|
||||
}
|
6
template/components/form_textarea.html
Normal file
6
template/components/form_textarea.html
Normal file
|
@ -0,0 +1,6 @@
|
|||
{{define "form_textarea"}}
|
||||
<div class="input-container">
|
||||
<label for="{{.ID}}" class="form-label">{{.Label}}</label>
|
||||
<textarea class="form-control" id="{{.ID}}" name="{{.ID}}" rows="3">{{.Value}}</textarea>
|
||||
</div>
|
||||
{{end}}
|
|
@ -1,8 +0,0 @@
|
|||
{{define "head"}}
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="initial-scale=1, viewport-fit=cover, width=device-width, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<title>{{.}} | Goffee</title>
|
||||
<link href="/public/style.css" rel="stylesheet">
|
||||
</head>
|
||||
{{end}}
|
8
template/components/page_card.go
Normal file
8
template/components/page_card.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package components
|
||||
|
||||
type PageCard struct {
|
||||
CardTitle string
|
||||
CardSubTitle string
|
||||
CardBody string
|
||||
CardLink string
|
||||
}
|
11
template/components/page_card.html
Normal file
11
template/components/page_card.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{{define "page_card"}}
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
{{if .CardTitle}}<h5 class="card-title">{{.CardTitle}}</h5>{{end}}
|
||||
{{if .CardSubTitle}}<h6 class="card-subtitle mb-2 text-muted">{{.CardSubTitle}}</h6>{{end}}
|
||||
{{if .CardBody}}<p class="card-text">{{.CardBody}}</p>{{end}}
|
||||
{{block "page_card_content" .}}{{end}}
|
||||
{{if .CardLink}}<a href="{{.CardLink}}" class="card-link">Card link</a>{{end}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
7
template/components/page_footer.html
Normal file
7
template/components/page_footer.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
{{define "page_footer"}}
|
||||
<footer>
|
||||
<script src="/public/app.js"></script>
|
||||
</footer>
|
||||
<script src="/public/bootstrap/js/popper.min.js"></script>
|
||||
<script src="/public/bootstrap/js/bootstrap.min.js"></script>
|
||||
{{end}}
|
11
template/components/page_head.html
Normal file
11
template/components/page_head.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{{define "page_head"}}
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>{{.}} | Goffee</title>
|
||||
<link rel="stylesheet" href="/public/style.css">
|
||||
<link rel="icon" href="/public/img/favicon.ico" type="image/x-icon">
|
||||
<link rel="stylesheet" href="/public/bootstrap/css/bootstrap.min.css">
|
||||
</head>
|
||||
{{end}}
|
16
template/components/page_nav.go
Normal file
16
template/components/page_nav.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package components
|
||||
|
||||
type PageNav struct {
|
||||
NavClass string // nav-pills, nav-underline
|
||||
NavItems []PageNavItem
|
||||
IsVertical bool
|
||||
IsTab bool
|
||||
}
|
||||
|
||||
type PageNavItem struct {
|
||||
Text string
|
||||
Link string
|
||||
IsDisabled bool
|
||||
IsActive bool
|
||||
ChildItems []PageNavItem
|
||||
}
|
23
template/components/page_nav.html
Normal file
23
template/components/page_nav.html
Normal file
|
@ -0,0 +1,23 @@
|
|||
{{define "page_nav"}}
|
||||
<ul class="nav justify-content-center {{.NavClass}} {{if eq .IsVertical true}}flex-column{{end}}{{if eq .IsTab true}}nav-tabs{{end}}" {{if eq .IsTab true}}role="tablist"{{end}}>
|
||||
{{range $item := .NavItems}}
|
||||
|
||||
{{if gt (len $item.ChildItems) 0}}
|
||||
<li class="nav-item dropdown">
|
||||
<a href="{{$item.Link}}" {{if eq .IsDisabled true}}disabled{{end}} data-bs-toggle="dropdown"
|
||||
class="nav-link dropdown-toggle {{if eq .IsActive true}}active{{end}} {{if eq .IsDisabled true}}disabled{{end}}">{{$item.Text}}</a>
|
||||
<ul class="dropdown-menu">
|
||||
{{ range $item.ChildItems}}
|
||||
{{template "content_dropdown_item" .}}
|
||||
{{end}}
|
||||
</ul>
|
||||
</li>
|
||||
{{else}}
|
||||
<li class="nav-item"><a href="{{$item.Link}}" {{if eq .IsDisabled true}}disabled{{end}} class="nav-link {{if eq .IsActive true}}active{{end}} {{if eq .IsDisabled true}}disabled{{end}}">
|
||||
{{$item.Text}}
|
||||
</a></li>
|
||||
{{end}}
|
||||
|
||||
{{end}}
|
||||
</ul>
|
||||
{{end}}
|
14
template/components/page_page.html
Normal file
14
template/components/page_page.html
Normal file
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
{{template "page_head" "Goffee"}}
|
||||
<body>
|
||||
<div class="container">
|
||||
{{block "page_content" .}}
|
||||
<main>
|
||||
<h1>Use this file as base inside cup application</h1>
|
||||
</main>
|
||||
{{end}}
|
||||
{{template "page_footer"}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,5 +0,0 @@
|
|||
package components
|
||||
|
||||
type Title struct {
|
||||
Label string
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{{define "title"}}
|
||||
<div class="title">
|
||||
<h1>{{.Label}}</h1>
|
||||
</div>
|
||||
{{end}}
|
12
validator.go
12
validator.go
|
@ -33,18 +33,14 @@ func (v *Validator) Validate(data map[string]interface{}, rules map[string]inter
|
|||
vr = validationResult{}
|
||||
vr.hasFailed = false
|
||||
res := map[string]string{}
|
||||
for key, val := range data {
|
||||
_, ok := rules[key]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
rls, err := parseRules(rules[key])
|
||||
for rule_key, rule_val := range rules {
|
||||
rls, err := parseRules(rule_val)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
err = validation.Validate(val, rls...)
|
||||
err = validation.Validate(data[rule_key], rls...)
|
||||
if err != nil {
|
||||
res[key] = fmt.Sprintf("%v: %v", key, err.Error())
|
||||
res[rule_key] = fmt.Sprintf("%v: %v", rule_key, err.Error())
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue