diff --git a/consts.go b/consts.go index 5669217..d93318e 100644 --- a/consts.go +++ b/consts.go @@ -11,6 +11,9 @@ const CONTENT_TYPE string = "content-Type" const CONTENT_TYPE_HTML string = "text/html; charset=utf-8" const CONTENT_TYPE_JSON string = "application/json" const CONTENT_TYPE_TEXT string = "text/plain" +const CONTENT_TYPE_IMAGEPNG string = "image/png" +const CONTENT_TYPE_IMAGEJPG string = "image/jpeg" +const CONTENT_TYPE_IMAGESVGXML string = "image/svg+xml" const CONTENT_TYPE_MULTIPART_FORM_DATA string = "multipart/form-data;" const LOCALHOST string = "http://localhost" const TEST_STR string = "Testing!" diff --git a/cookies.go b/cookies.go index 8c03c10..ce17dd2 100644 --- a/cookies.go +++ b/cookies.go @@ -126,7 +126,6 @@ func SetCookie(w http.ResponseWriter, email string, token string) error { Path: "/", MaxAge: 3600, HttpOnly: true, - Secure: true, SameSite: http.SameSiteLaxMode, } diff --git a/core.go b/core.go index 59691c6..a2c8a2a 100644 --- a/core.go +++ b/core.go @@ -176,6 +176,18 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr router.HEAD(route.Path, app.makeHTTPRouterHandlerFunc(route.Controller, route.Hooks)) } } + + // check if use letsencrypt + UseCoreServicesStr := os.Getenv("App_USE_CORESERVICES") + if UseCoreServicesStr == "" { + UseCoreServicesStr = "false" + } + UseCoreServices, _ := strconv.ParseBool(UseCoreServicesStr) + if UseCoreServices { + // Register router for graphs + router.GET("/coregraph/*graph", Graph) + } + return router } diff --git a/go.mod b/go.mod index a3e044e..2c18ebd 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,10 @@ replace git.smarteching.com/goffee/core/logger => ./logger replace git.smarteching.com/goffee/core/env => ./env -go 1.20 +go 1.23.1 require ( + git.smarteching.com/zeni/go-chart/v2 v2.1.4 github.com/brianvoe/gofakeit/v6 v6.21.0 github.com/golang-jwt/jwt/v5 v5.0.0 github.com/google/uuid v1.3.0 @@ -26,6 +27,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect github.com/go-chi/chi/v5 v5.0.8 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v5 v5.3.1 // indirect @@ -37,8 +39,9 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/sendgrid/rest v2.6.9+incompatible // indirect github.com/sendgrid/sendgrid-go v3.12.0+incompatible // indirect + golang.org/x/image v0.21.0 // indirect golang.org/x/net v0.10.0 // indirect - golang.org/x/text v0.11.0 // indirect + golang.org/x/text v0.19.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 4aedeaa..21aed15 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.smarteching.com/zeni/go-chart/v2 v2.1.4 h1:pF06+F6eqJLIG8uMiTVPR5TygPGMjM/FHMzTxmu5V/Q= +git.smarteching.com/zeni/go-chart/v2 v2.1.4/go.mod h1:b3ueW9h3pGGXyhkormZAvilHaG4+mQti+bMNPdQBeOQ= github.com/SparkPost/gosparkpost v0.2.0 h1:yzhHQT7cE+rqzd5tANNC74j+2x3lrPznqPJrxC1yR8s= github.com/SparkPost/gosparkpost v0.2.0/go.mod h1:S9WKcGeou7cbPpx0kTIgo8Q69WZvUmVeVzbD+djalJ4= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= @@ -5,7 +7,9 @@ github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:W github.com/brianvoe/gofakeit/v6 v6.21.0 h1:tNkm9yxEbpuPK8Bx39tT4sSc5i9SUGiciLdNix+VDQY= github.com/brianvoe/gofakeit/v6 v6.21.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8= github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= +github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w= github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= +github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/buger/jsonparser v1.0.0/go.mod h1:tgcrVJ81GPSF0mz+0nu1Xaz0fazGPrmmJfJtxjbHhUQ= github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= @@ -31,6 +35,8 @@ github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3a github.com/gogs/chardet v0.0.0-20150115103509-2404f7772561/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14= github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE= github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -83,21 +89,25 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= +golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= +golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= -golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= +golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= diff --git a/graph.go b/graph.go new file mode 100644 index 0000000..1ca21b8 --- /dev/null +++ b/graph.go @@ -0,0 +1,140 @@ +// Copyright (c) 2024 Zeni Kim +// Use of this source code is governed by MIT-style +// license that can be found in the LICENSE file. + +package core + +import ( + "fmt" + "net/http" + "strconv" + "strings" + + "git.smarteching.com/zeni/go-chart/v2" + "github.com/julienschmidt/httprouter" +) + +func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + + // $_GET values + + service := ps.ByName("graph") + + if service != "" { + + //serv := strings.TrimPrefix(service, "/") + serv := strings.Split(service, "/") + + switch serv[1] { + + case "graph": + + kindg := serv[2] + + if kindg != "" { + + switch kindg { + // case graph pie + case "pie": + + queryValues := r.URL.Query() + labels := strings.Split(queryValues.Get("l"), "|") + values := strings.Split(queryValues.Get("v"), "|") + + qtyl := len(labels) + qtyv := len(values) + + // cjeck qty and equal values from url + if qtyl < 2 { + fmt.Fprintf(w, "Missing captions in pie") + return + } + if qtyv != qtyl { + fmt.Fprintf(w, "Labels and values mismatch") + return + } + + var cvalues []chart.Value + var row chart.Value + // fill values + for index, line := range labels { + valuefloat, _ := strconv.ParseFloat(values[index], 64) + row.Value = valuefloat + row.Label = line + cvalues = append(cvalues, row) + } + + pie := chart.PieChart{ + Width: 512, + Height: 512, + Values: cvalues, + } + w.Header().Set("Content-Type", "image/png") + err := pie.Render(chart.PNG, w) + if err != nil { + fmt.Printf("Error rendering pie chart: %v\n", err) + } + + // case graph bar + case "bar": + + queryValues := r.URL.Query() + labels := strings.Split(queryValues.Get("l"), "|") + values := strings.Split(queryValues.Get("v"), "|") + + qtyl := len(labels) + qtyv := len(values) + + // cjeck qty and equal values from url + if qtyl < 2 { + fmt.Fprintf(w, "Missing captions in pie") + return + } + if qtyv != qtyl { + fmt.Fprintf(w, "Labels and values mismatch") + return + } + + var cvalues []chart.Value + var row chart.Value + // fill values + for index, line := range labels { + valuefloat, _ := strconv.ParseFloat(values[index], 64) + row.Value = valuefloat + row.Label = line + cvalues = append(cvalues, row) + } + + bar := chart.BarChart{ + Height: 512, + BarWidth: 50, + Bars: cvalues, + } + w.Header().Set("Content-Type", "image/png") + err := bar.Render(chart.PNG, w) + if err != nil { + fmt.Printf("Error rendering pie chart: %v\n", err) + } + + default: + fmt.Fprintf(w, "Unknown graph %s!\n", kindg) + } + + } else { + fmt.Fprintf(w, "Kind graph not exists") + } + // + + default: + + fmt.Fprintf(w, "Unknown option in core service %s!\n", ps.ByName("serv")) + + } + fmt.Fprintf(w, "Service %s!\n", serv) + + } else { + + fmt.Fprintf(w, "Unknown core service %s!\n", ps.ByName("graph")) + } + +} diff --git a/template/components/content_graph.go b/template/components/content_graph.go new file mode 100644 index 0000000..3c01f97 --- /dev/null +++ b/template/components/content_graph.go @@ -0,0 +1,8 @@ +package components + +type ContentGraph struct { + Graph string // pie, bar + Labels string + Values string + Alt string +} diff --git a/template/components/content_graph.html b/template/components/content_graph.html new file mode 100644 index 0000000..4664137 --- /dev/null +++ b/template/components/content_graph.html @@ -0,0 +1,3 @@ +{{define "content_graph"}} + {{.Alt}} +{{end}} \ No newline at end of file diff --git a/template/components/content_tabledetail.go b/template/components/content_tabledetail.go new file mode 100644 index 0000000..61848b8 --- /dev/null +++ b/template/components/content_tabledetail.go @@ -0,0 +1,16 @@ +package components + +type ContentTabledetail struct { + ID string + TableClass string // table-primary, table-secondary,.. table-striped table-bordered + HeadClass string // table-dark table-light + Title string + AllTD []ContentTabledetailTD +} + +type ContentTabledetailTD struct { + ID string + Caption string + ValueType string // -> default string, href, badge + Value interface{} // string or component struct according ValueType +} diff --git a/template/components/content_tabledetail.html b/template/components/content_tabledetail.html new file mode 100644 index 0000000..636191c --- /dev/null +++ b/template/components/content_tabledetail.html @@ -0,0 +1,29 @@ +{{define "content_tabledetail"}} + + {{$stylecell := "table-success"}} + {{if .HeadClass }}{{$stylecell = .HeadClass}}{{end}} + {{if .Title }} + + + + + + {{end}} + + {{range $item := .AllTD}} + + + + + {{end}} + +
{{ .Title }}
{{ $item.Caption }} + {{ if eq $item.ValueType "href"}} + {{template "content_href" $item.Value}} + {{ else if eq $item.ValueType "badge"}} + {{template "content_badge" $item.Value}} + {{ else }} + {{ $item.Value }} + {{end}} +
+{{end}}