feat/sync-setting:

+ moved infra/sync-cfg to infra/sync-consumer-cfg
+ adjut anything related to old infra/sync
+ infra/sync is now have new setting for the sync
This commit is contained in:
2025-11-28 09:16:38 +07:00
parent 8360da06c8
commit b09c9f183c
29 changed files with 187 additions and 39 deletions
@@ -13,6 +13,8 @@ import (
mf "simrs-vx/internal/domain/main-entities/user-fes"
pa "simrs-vx/internal/lib/auth"
s "simrs-vx/internal/use-case/main-use-case/authentication"
esga "simrs-vx/internal/domain/sync-entities/authentication"
)
func Login(w http.ResponseWriter, r *http.Request) {
@@ -67,6 +69,23 @@ func Logout(w http.ResponseWriter, r *http.Request) {
func GuardMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if it's from sync
credential := esga.CredentialDto{}
credential.Source = r.Header.Get("X-Sync-Source")
credential.SecretKey = r.Header.Get("X-Sync-SecretKey")
credential.UserName = r.Header.Get("X-Sync-UserName")
if credential.Source != "" || credential.SecretKey != "" || credential.UserName != "" {
ctx := context.WithValue(r.Context(), esga.SyncKey{}, credential)
next.ServeHTTP(w, r.WithContext(ctx))
// TO DO:
// 1 Get user info manually (not by using token), based on credential.UserName
// 2 To cover the point 1, Adjust /use-case/main-use-case/authentication to have the function
// 3 Any DTO that is used in the sync, add flag Sync (tru/false), set it true if it is from sync
return
}
// Normal flow goes here
accessDetail, err := s.ExtractToken(r, s.AccessToken)
if err != nil {
rw.WriteJSON(w, http.StatusUnauthorized, err.(d.FieldError), nil)
@@ -63,7 +63,7 @@ import (
gs "simrs-vx/internal/infra/gorm-setting"
minio "simrs-vx/internal/infra/minio"
ssdb "simrs-vx/internal/infra/ss-db"
simgossync "simrs-vx/internal/infra/sync-cfg"
sync "simrs-vx/internal/infra/sync-consumer-cfg"
/******************** pkg ********************/
cmw "simrs-vx/pkg/cors-manager-mw"
@@ -135,7 +135,7 @@ func SetRoutes() http.Handler {
a.RegisterExtCall(mh.I.SetClient)
a.RegisterExtCall(ibpjs.SetConfig)
a.RegisterExtCall(validation.RegisterValidation)
a.RegisterExtCall(simgossync.SetConfig)
a.RegisterExtCall(sync.SetConfig)
a.RegisterExtCall(docscfg.ParseCfg)
r := http.NewServeMux()
@@ -0,0 +1,52 @@
package authentication
import (
"context"
"net/http"
d "github.com/karincake/dodol"
rw "github.com/karincake/risoles"
sc "simrs-vx/internal/lib/sync-credential"
e "simrs-vx/internal/domain/sync-entities/authentication"
is "simrs-vx/internal/infra/sync-cfg"
)
func NewGuardMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check the data format
input := e.CredentialDto{}
if success := sc.CheckCredentialData(&input, r, w); !success {
return
}
// check the validity
if input.Source != is.O.NewSource || input.SecretKey != is.O.NewSecretKey {
rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": "invalid consumer credential"}, nil)
return
}
ctx := context.WithValue(r.Context(), e.SyncKey{}, input)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func OldGuardMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check the data format
input := e.CredentialDto{}
if success := sc.CheckCredentialData(&input, r, w); !success {
return
}
// check the validity
if input.Source != is.O.OldSource || input.SecretKey != is.O.OldSecretKey {
rw.WriteJSON(w, http.StatusUnauthorized, d.IS{"message": "invalid consumer credential"}, nil)
return
}
ctx := context.WithValue(r.Context(), e.SyncKey{}, input)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
@@ -6,6 +6,7 @@ import (
/******************** infra ********************/
gs "simrs-vx/internal/infra/gorm-setting"
simgosdb "simrs-vx/internal/infra/simgos-db"
sync "simrs-vx/internal/infra/sync-consumer-cfg"
/******************** pkg ********************/
cmw "simrs-vx/pkg/cors-manager-mw"
@@ -27,6 +28,8 @@ import (
"simrs-vx/internal/interface/simgos-sync-handler/new/specialist"
"simrs-vx/internal/interface/simgos-sync-handler/new/subspecialist"
"simrs-vx/internal/interface/simgos-sync-handler/new/unit"
oauth "simrs-vx/internal/interface/simgos-sync-handler/old/authentication" // just a reminder, an openauth
)
func SetRoutes() http.Handler {
@@ -34,6 +37,7 @@ func SetRoutes() http.Handler {
a.RegisterExtCall(gs.Adjust)
a.RegisterExtCall(zlc.Adjust)
a.RegisterExtCall(lh.Populate)
a.RegisterExtCall(sync.SetConfig)
a.RegisterExtCall(simgosdb.SetInstance)
r := http.NewServeMux()
@@ -66,7 +70,8 @@ func SetRoutes() http.Handler {
})
/******************** SvcToNew ******************/
//prefixold := "/old-to-new"
prefixold := "/old-to-new"
hk.GroupRoutes(prefixold+"/v1/patient", r, oauth.OldGuardMW, hk.MapHandlerFunc{}) // FINISH THIS
return cmw.SetCors(handlerlogger.SetLog(r))
}