Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/getevo/evo
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Dec 19, 2023
2 parents 42ba127 + db96d82 commit 85d14cf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
11 changes: 11 additions & 0 deletions evo.constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,14 @@ const (
HeaderXRobotsTag = "X-Robots-Tag"
HeaderXUACompatible = "X-UA-Compatible"
)

// Cache control headers
const (
CachePrivate = "private"
CachePublic = "public"
CacheNoStore = "no-store"
CacheNoCache = "no-cache"
CacheNoTransform = "no-transform"
CacheMustUnderstand = "must-understand"
CacheImmutable = "immutable"
)
13 changes: 13 additions & 0 deletions evo.context.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,16 @@ func (r *Request) Var(key string, value ...any) generic.Value {
func (r *Request) RestartRouting() error {
return r.Context.RestartRouting()
}

func (r *Request) SetCacheControl(t time.Duration, headers ...string) {
var ccHeader string = fmt.Sprintf("max-age=%.0f", t.Seconds())
var options string

for _, header := range headers {
options = options + fmt.Sprintf(", %s", header)
}

ccHeader = ccHeader + options

r.SetHeader("Cache-Control", ccHeader)
}
25 changes: 24 additions & 1 deletion lib/db/schema/ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/getevo/evo/v2/lib/db/schema/table"
"gorm.io/gorm"
"reflect"
"strconv"
"strings"
)
Expand Down Expand Up @@ -128,6 +129,23 @@ func FromStatement(stmt *gorm.Statement) Table {
if strings.ToLower(column.Type) == "datetime(3)" {
column.Type = "TIMESTAMP"
}

if field.FieldType.Kind() == reflect.Ptr {
if _, ok := field.TagSettings["NOT NULL"]; !ok {
column.Nullable = true
if column.Type == "TIMESTAMP" && column.Default == "0000-00-00 00:00:00" {
column.Default = "NULL"
}
if column.Default == "" {
column.Default = "NULL"
}
}
}

if column.Type == "TIMESTAMP" && column.Default == "" && !column.Nullable {
column.Default = "0000-00-00 00:00:00"
}

if column.Name == "deleted_at" {
column.Nullable = true
column.Default = "NULL"
Expand Down Expand Up @@ -230,8 +248,13 @@ func getFieldQuery(field *Column) string {
}

if field.Default != "" {
query += " DEFAULT " + field.Default
var v = field.Default
/* if !(v[0] == '\'' || v[0] == '"' || v[0] == '`') {
v = strconv.Quote(v)
}*/
query += " DEFAULT " + v
}

if field.OnUpdate != "" {
query += " ON UPDATE " + field.OnUpdate
}
Expand Down
21 changes: 20 additions & 1 deletion lib/outcome/outcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
"reflect"
"time"

"github.com/gofiber/fiber/v2"
)

type Response struct {
Expand Down Expand Up @@ -185,3 +186,21 @@ func (response *Response) Filename(filename string) *Response {
func (response *Response) ResponseSerializer() *Response {
return response
}

func (response *Response) SetCacheControl(t time.Duration, headers ...string) *Response {
var ccHeader string = fmt.Sprintf("max-age=%.0f", t.Seconds())
var options string

for _, header := range headers {
options = options + fmt.Sprintf(", %s", header)
}

ccHeader = ccHeader + options

if response.Headers == nil {
response.Headers = map[string]string{}
}
response.Headers["Cache-Control"] = ccHeader

return response
}

0 comments on commit 85d14cf

Please sign in to comment.