Skip to content

Commit

Permalink
project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Aug 11, 2024
1 parent e0097fe commit d3ed4f4
Show file tree
Hide file tree
Showing 59 changed files with 733 additions and 644 deletions.
47 changes: 26 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
"github.com/mstgnz/cronjob/config"
"github.com/mstgnz/cronjob/handler/api"
"github.com/mstgnz/cronjob/handler/web"
"github.com/mstgnz/cronjob/models"
"github.com/mstgnz/cronjob/pkg/auth"
"github.com/mstgnz/cronjob/pkg/config"
"github.com/mstgnz/cronjob/pkg/load"
"github.com/mstgnz/cronjob/pkg/logger"
"github.com/mstgnz/cronjob/pkg/response"
"github.com/mstgnz/cronjob/pkg/validate"
"github.com/mstgnz/cronjob/schedule"
)

Expand Down Expand Up @@ -48,17 +53,17 @@ var (
func init() {
// Load Env
if err := godotenv.Load(".env"); err != nil {
config.App().Log.Warn(fmt.Sprintf("Load Env Error: %v", err))
logger.Warn(fmt.Sprintf("Load Env Error: %v", err))
log.Fatalf("Load Env Error: %v", err)
}
// init conf
_ = config.App()
config.CustomValidate()
validate.CustomValidate()

// Load Sql
config.App().QUERY = make(map[string]string)
if query, err := config.LoadSQLQueries(); err != nil {
config.App().Log.Warn(fmt.Sprintf("Load Sql Error: %v", err))
if query, err := load.LoadSQLQueries(); err != nil {
logger.Warn(fmt.Sprintf("Load Sql Error: %v", err))
log.Fatalf("Load Sql Error: %v", err)
} else {
config.App().QUERY = query
Expand All @@ -72,7 +77,7 @@ type HttpHandler func(w http.ResponseWriter, r *http.Request) error
func Catch(h HttpHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
config.App().Log.Info("HTTP Handler Error", "err", err.Error(), "path", r.URL.Path)
logger.Info("HTTP Handler Error", "err", err.Error(), "path", r.URL.Path)
}
}
}
Expand Down Expand Up @@ -250,7 +255,7 @@ func main() {
// Not Found
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "api") {
_ = config.WriteJSON(w, http.StatusUnauthorized, config.Response{Status: false, Message: "Not Found"})
_ = response.WriteJSON(w, http.StatusUnauthorized, response.Response{Status: false, Message: "Not Found"})
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
Expand All @@ -264,34 +269,34 @@ func main() {
go func() {
err := http.ListenAndServe(fmt.Sprintf(":%s", PORT), r)
if err != nil && err != http.ErrServerClosed {
config.App().Log.Warn("Fatal Error", "err", err.Error())
logger.Warn("Fatal Error", "err", err.Error())
log.Fatal(err.Error())
}
}()

config.App().Log.Info("Cron is running on", PORT)
logger.Info("Cron is running on", PORT)

// Block until a signal is received
<-ctx.Done()

config.App().Log.Info("Cron is shutting on", PORT)
logger.Info("Cron is shutting on", PORT)

// set Shutting
config.App().Shutting = true

// check Running
for {
if config.App().Running <= 0 {
config.App().Log.Info("Cronjobs all done")
logger.Info("Cronjobs all done")
break
} else {
config.App().Log.Info(fmt.Sprintf("Currently %d active jobs in progress. pending completion...", config.App().Running))
logger.Info(fmt.Sprintf("Currently %d active jobs in progress. pending completion...", config.App().Running))
}
time.Sleep(time.Second * 5)
}

config.App().Cron.Stop()
config.App().Log.Info("Shutting down gracefully...")
logger.Info("Shutting down gracefully...")
config.App().DB.CloseDatabase()
}

Expand All @@ -305,7 +310,7 @@ func webAuthMiddleware(next http.Handler) http.Handler {
}
token := strings.Replace(cookie.Value, "Bearer ", "", 1)

userId, err := config.GetUserIDByToken(token)
userId, err := auth.GetUserIDByToken(token)
if err != nil {
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
Expand Down Expand Up @@ -334,28 +339,28 @@ func apiAuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
_ = config.WriteJSON(w, http.StatusUnauthorized, config.Response{Status: false, Message: "Invalid Token"})
_ = response.WriteJSON(w, http.StatusUnauthorized, response.Response{Status: false, Message: "Invalid Token"})
return
}
token = strings.Replace(token, "Bearer ", "", 1)

userId, err := config.GetUserIDByToken(token)
userId, err := auth.GetUserIDByToken(token)
if err != nil {
_ = config.WriteJSON(w, http.StatusUnauthorized, config.Response{Status: false, Message: err.Error()})
_ = response.WriteJSON(w, http.StatusUnauthorized, response.Response{Status: false, Message: err.Error()})
return
}

user_id, err := strconv.Atoi(userId)
if err != nil && user_id == 0 {
_ = config.WriteJSON(w, http.StatusUnauthorized, config.Response{Status: false, Message: err.Error()})
_ = response.WriteJSON(w, http.StatusUnauthorized, response.Response{Status: false, Message: err.Error()})
return
}

user := &models.User{}
err = user.GetWithId(user_id)

if err != nil {
_ = config.WriteJSON(w, http.StatusUnauthorized, config.Response{Status: false, Message: err.Error()})
_ = response.WriteJSON(w, http.StatusUnauthorized, response.Response{Status: false, Message: err.Error()})
return
}

Expand All @@ -370,7 +375,7 @@ func isAuthMiddleware(next http.Handler) http.Handler {

if err == nil {
token := strings.Replace(cookie.Value, "Bearer ", "", 1)
_, err = config.GetUserIDByToken(token)
_, err = auth.GetUserIDByToken(token)
if err == nil {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
Expand All @@ -396,7 +401,7 @@ func headerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
checkMethod := r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH"
if checkMethod && r.Header.Get("Content-Type") != "application/json" {
_ = config.WriteJSON(w, http.StatusBadRequest, config.Response{Status: false, Message: "Invalid Content-Type"})
_ = response.WriteJSON(w, http.StatusBadRequest, response.Response{Status: false, Message: "Invalid Content-Type"})
return
}
next.ServeHTTP(w, r)
Expand Down
44 changes: 0 additions & 44 deletions config/logger.go

This file was deleted.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/redis/go-redis/v9 v9.6.1
github.com/robfig/cron/v3 v3.0.1
golang.org/x/crypto v0.22.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/eapache/go-resiliency v1.6.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/IBM/sarama v1.43.2 h1:HABeEqRUh32z8yzY2hGB/j8mHSzC/HA9zlEjqFNCzSw=
github.com/IBM/sarama v1.43.2/go.mod h1:Kyo4WkF24Z+1nz7xeVUFWIuKVV8RS3wM8mkvPKMdXFQ=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/eapache/go-resiliency v1.6.0 h1:CqGDTLtpwuWKn6Nj3uNUdflaq+/kIPsg0gfNzHton30=
github.com/eapache/go-resiliency v1.6.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 h1:Oy0F4ALJ04o5Qqpdz8XLIpNA3WM/iSIXqxtqo7UGVws=
Expand Down Expand Up @@ -73,6 +81,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
18 changes: 9 additions & 9 deletions handler/api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"net/http"

"github.com/mstgnz/cronjob/config"
"github.com/mstgnz/cronjob/pkg/response"
"github.com/mstgnz/cronjob/services"
)

Expand All @@ -12,21 +12,21 @@ type GroupHandler struct {
}

func (h *GroupHandler) ListHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.ListService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.ListService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *GroupHandler) CreateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.CreateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.CreateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *GroupHandler) UpdateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.UpdateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.UpdateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *GroupHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.DeleteService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.DeleteService(w, r)
return response.WriteJSON(w, statusCode, result)
}
22 changes: 11 additions & 11 deletions handler/api/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"net/http"

"github.com/mstgnz/cronjob/config"
"github.com/mstgnz/cronjob/pkg/response"
"github.com/mstgnz/cronjob/services"
)

Expand All @@ -12,26 +12,26 @@ type NotificationHandler struct {
}

func (h *NotificationHandler) ListHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.ListService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.ListService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotificationHandler) CreateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.CreateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.CreateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotificationHandler) UpdateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.UpdateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.UpdateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotificationHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.DeleteService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.DeleteService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotificationHandler) BulkHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.BulkService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.BulkService(w, r)
return response.WriteJSON(w, statusCode, result)
}
18 changes: 9 additions & 9 deletions handler/api/notifiy_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"net/http"

"github.com/mstgnz/cronjob/config"
"github.com/mstgnz/cronjob/pkg/response"
"github.com/mstgnz/cronjob/services"
)

Expand All @@ -12,21 +12,21 @@ type NotifyEmailHandler struct {
}

func (h *NotifyEmailHandler) ListHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.ListService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.ListService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotifyEmailHandler) CreateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.CreateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.CreateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotifyEmailHandler) UpdateHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.UpdateService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.UpdateService(w, r)
return response.WriteJSON(w, statusCode, result)
}

func (h *NotifyEmailHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) error {
statusCode, response := h.DeleteService(w, r)
return config.WriteJSON(w, statusCode, response)
statusCode, result := h.DeleteService(w, r)
return response.WriteJSON(w, statusCode, result)
}
Loading

0 comments on commit d3ed4f4

Please sign in to comment.