Skip to content

Commit

Permalink
Add go-cache and cron basic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 16, 2024
1 parent 758bd29 commit 37c6c04
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go Gin

Gin gonic starter with zerolog, viper, gorm, jwt basic setup.
Gin gonic starter with zerolog, viper, gorm, jwt basic, go-cache, cron basic configuration.

[![Build Status][build-status-image]][build-status]
[![license][license-image]][repository-url]
Expand Down
24 changes: 21 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

type CliParam struct {
Version bool // Show version
ConfigName string // Config file name
Port uint // Server port
}
Expand All @@ -20,17 +21,25 @@ var (
cliParam CliParam
)

func main() {
func init() {
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.BoolVarP(&cliParam.Version, "version", "v", false, "show version")
flag.StringVarP(&cliParam.ConfigName, "config", "c", "config", "config file name")
flag.UintVarP(&cliParam.Port, "port", "p", 0, "server port")
flag.Parse()
flag.Lookup("config").NoOptDefVal = "config"

singleton.InitConfig(cliParam.ConfigName)
singleton.InitLog(singleton.Conf)
singleton.InitTimezoneAndCache()
singleton.InitDBFromPath(singleton.Conf.DBPath)
initService()
}

func main() {
if cliParam.Version {
fmt.Println(singleton.Version)
return
}

port := singleton.Conf.Server.Port
if cliParam.Port != 0 {
Expand Down Expand Up @@ -69,5 +78,14 @@ func main() {
}

func initService() {
singleton.InitSingleton()
// Load all services in the singleton package
singleton.LoadSingleton()

if _, err := singleton.Cron.AddFunc("0 * * * * *", writeHello); err != nil {
panic(err)
}
}

func writeHello() {
singleton.Log.Info().Msg("Hello world, I am a cron task")
}
2 changes: 1 addition & 1 deletion cmd/srv/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func serveStatic(r *gin.Engine) {

// Load templates
func loadTemplates(r *gin.Engine) {
new_tmpl := template.New("").Funcs(mygin.FuncMap)
new_tmpl := template.New("").Funcs(gogin.FuncMap)
var err error
new_tmpl, err = new_tmpl.ParseFS(resource.TemplateFS, "template/**/*.html", "template/*.html")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ site:
description: A simple web application using Go and Gin
base_url: http://localhost:8080

debug: true
debug: false

log:
level: debug
Expand All @@ -28,3 +28,4 @@ jwt:
refresh_token_expiration: 720 # minutes
access_token_cookie_name: go-gin-access
refresh_token_cookie_name: go-gin-refresh
location: Asia/Chongqing # Timezone
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/ory/graceful v0.1.3 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
github.com/onsi/gomega v1.27.4/go.mod h1:riYq/GJKh8hhoM01HN6Vmuy93AarCXCBGpvFDK3q3fQ=
github.com/ory/graceful v0.1.3 h1:FaeXcHZh168WzS+bqruqWEw/HgXWLdNv2nJ+fbhxbhc=
github.com/ory/graceful v0.1.3/go.mod h1:4zFz687IAF7oNHHiB586U4iL+/4aV09o/PYLE34t2bA=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
Expand All @@ -111,6 +113,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down
1 change: 1 addition & 0 deletions internal/gconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ type Config struct {
AccessTokenCookieName string `mapstructure:"access_token_cookie_name"`
RefreshTokenCookieName string `mapstructure:"refresh_token_cookie_name"`
} `mapstructure:"jwt"`
Location string `mapstructure:"location"`
}
11 changes: 6 additions & 5 deletions pkg/mygin/template.go → internal/gogin/template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package mygin
package gogin

import (
"fmt"
"go-gin/service/singleton"
"html/template"
"strconv"
"strings"
Expand All @@ -17,10 +18,10 @@ var FuncMap = template.FuncMap{
}
},
"tf": func(t time.Time) string {
return t.Format("01/02/2006 15:04:05")
return t.In(singleton.Loc).Format("01/02/2006 15:04:05")
},
"tsf": func(ts int64) string {
return time.Unix(int64(ts/1000), 0).Format("01/02/2006 15:04:05")
return time.Unix(int64(ts/1000), 0).In(singleton.Loc).Format("01/02/2006 15:04:05")
},
"text2html": func(text string) template.HTML {
text = strings.Replace(text, "\n", "</p><p>", -1)
Expand All @@ -36,7 +37,7 @@ var FuncMap = template.FuncMap{
return template.HTML(`<` + s + `>`) // #nosec
},
"stf": func(s uint64) string {
return time.Unix(int64(s), 0).Format("01/02/2006 15:04")
return time.Unix(int64(s), 0).In(singleton.Loc).Format("01/02/2006 15:04")
},
"sf": func(duration uint64) string {
return time.Duration(time.Duration(duration) * time.Second).String()
Expand Down Expand Up @@ -119,7 +120,7 @@ var FuncMap = template.FuncMap{
},
"dayBefore": func(i int) string {
year, month, day := time.Now().Date()
today := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
today := time.Date(year, month, day, 0, 0, 0, 0, singleton.Loc)
return today.AddDate(0, 0, i-29).Format("01/02")
},
"className": func(percent float32) string {
Expand Down
22 changes: 22 additions & 0 deletions service/singleton/crontask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package singleton

import (
"sync"

"github.com/robfig/cron/v3"
)

var (
Cron *cron.Cron
CronLock sync.RWMutex
)

func InitCronTask() {
Cron = cron.New(cron.WithSeconds(), cron.WithLocation(Loc))
}

func LoadCronTasks() {
InitCronTask()

Cron.Start()
}
27 changes: 21 additions & 6 deletions service/singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package singleton
import (
"fmt"
"os"
"time"

"github.com/patrickmn/go-cache"
"github.com/rs/zerolog"
"github.com/spf13/viper"
"gorm.io/driver/sqlite"
Expand All @@ -18,14 +20,27 @@ import (
var Version = "0.0.1"

var (
ViperConf *viper.Viper
Conf *gconfig.Config
Log *zerolog.Logger
DB *gorm.DB
ViperConf *viper.Viper // Viper config for the application
Conf *gconfig.Config // Global config for the application
Log *zerolog.Logger // Global logger for the application
DB *gorm.DB // Global db for the application
Cache *cache.Cache // Global cache for the application
Loc *time.Location // Global location for the application
)

func InitSingleton() {
// TOO: init db
func LoadSingleton() {
LoadCronTasks()
// TODO: Add your initialization code here, eg Service, Task, etc.
}

func InitTimezoneAndCache() {
var err error
Loc, err = time.LoadLocation(Conf.Location)
if err != nil {
panic(err)
}

Cache = cache.New(5*time.Minute, 10*time.Minute)
}

func InitConfig(name string) {
Expand Down

0 comments on commit 37c6c04

Please sign in to comment.