From 03036fdffdadbd132fbb93b725a8da8c25aec9bc Mon Sep 17 00:00:00 2001 From: ctengiz Date: Thu, 11 Apr 2024 21:04:00 +0300 Subject: [PATCH] initial auth from subscriber app --- svc/.gitignore | 23 ++++++++ svc/api/public/login.go | 18 +++++++ svc/api/public/zrouter.go | 14 +++++ svc/api/zhandler.go | 108 ++++++++++++++++++++++++++++++++++++++ svc/go.mod | 13 +++++ svc/main.go | 45 ++++++++++++++++ 6 files changed, 221 insertions(+) create mode 100644 svc/.gitignore create mode 100644 svc/api/public/login.go create mode 100644 svc/api/public/zrouter.go create mode 100644 svc/api/zhandler.go create mode 100644 svc/go.mod create mode 100644 svc/main.go diff --git a/svc/.gitignore b/svc/.gitignore new file mode 100644 index 0000000..78f591a --- /dev/null +++ b/svc/.gitignore @@ -0,0 +1,23 @@ +### Go template +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + diff --git a/svc/api/public/login.go b/svc/api/public/login.go new file mode 100644 index 0000000..f960e90 --- /dev/null +++ b/svc/api/public/login.go @@ -0,0 +1,18 @@ +package public + +import ( + "git.makki.io/makki/libgo/mhttp" + "git.makki.io/makki/libgo/svc" + "net/http" +) + +func login(w http.ResponseWriter, r *http.Request) { + authResp, err := svc.S.Authenticate(r) + if err != nil { + mhttp.InternalServerError(w, err) + return + } + + clientResp := authResp.GetEndUserReponse() + mhttp.ResponseSuccess(w, clientResp) +} diff --git a/svc/api/public/zrouter.go b/svc/api/public/zrouter.go new file mode 100644 index 0000000..228ea5f --- /dev/null +++ b/svc/api/public/zrouter.go @@ -0,0 +1,14 @@ +package public + +import ( + "github.com/go-chi/chi/v5" + "net/http" +) + +func Router() http.Handler { + r := chi.NewRouter() + + // user authentication + r.Post("/login", login) + return r +} diff --git a/svc/api/zhandler.go b/svc/api/zhandler.go new file mode 100644 index 0000000..8cdbb8e --- /dev/null +++ b/svc/api/zhandler.go @@ -0,0 +1,108 @@ +package api + +import ( + "bordro-esleme/api/public" + "fmt" + "git.makki.io/makki/libgo/nauth" + "net/http" + "os" + "path" + "path/filepath" + "strings" + + "git.makki.io/makki/libgo/enums" + "git.makki.io/makki/libgo/svc" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" + "github.com/go-chi/jwtauth/v5" +) + +func HttpHandler(re enums.TRunEnv) http.Handler { + mux := chi.NewRouter() + + // Gerekli middleware stack + mux.Use(middleware.RequestID) + mux.Use(middleware.RealIP) + mux.Use(middleware.Logger) + + // Eğer trace middleware'ı kullanırsak buna gerek kalmayacak sanırım kontrol etmeli + // https://github.com/go-chi/httptracer + mux.Use(middleware.Recoverer) + acors := cors.New(cors.Options{ + // AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts + AllowedOrigins: []string{"*"}, + // AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: true, + MaxAge: 300, // Maximum value not ignored by any of major browsers + }) + + mux.Use(acors.Handler) + + // Set a timeout value on the request context (ctx), that will signal + // through ctx.Done() that the request has timed out and further + // processing should be stopped. + // todo: bu belki endpoint bazında özelleştirlebilinir... + // mux.Use(middleware.Timeout(2500 * time.Millisecond)) + + mux.Route("/api", func(mr chi.Router) { + // Public Route endpoints + mr.Mount("/", public.Router()) + //mr.Mount("/admin", admin.Router()) + + //protected end points + mr.Group(func(r chi.Router) { + // Seek, verify and validate JWT tokens + r.Use(jwtauth.Verifier(nauth.JWT)) + + // Handle valid / invalid tokens. + r.Use(nauth.CheckTokenValidity) + + }) + }) + + // Handle Websocket + // mux.HandleFunc("/ws", ws.Handle) + + // SPA Routes + mux.Group(func(r chi.Router) { + r.NotFound(index) + r.Get("/", index) + }) + + return mux +} + +func index(w http.ResponseWriter, r *http.Request) { + p := r.URL.Path + if !strings.HasPrefix(p, "/") { + p = "/" + p + r.URL.Path = p + } + p = path.Clean(p) + + if strings.HasPrefix(p, "/api") { + http.NotFound(w, r) + return + } + + uiPath := "./ui" + if svc.S.RunEnv() == enums.RunEnvLocal { + uiPath = "../../ui/dist/spa" + } + + name := path.Join(uiPath, filepath.FromSlash(p)) + + f, err := os.Open(name) + if err != nil { + if os.IsNotExist(err) { + http.ServeFile(w, r, fmt.Sprintf("%s/index.html", uiPath)) + return + } + } + defer f.Close() + http.ServeFile(w, r, name) +} diff --git a/svc/go.mod b/svc/go.mod new file mode 100644 index 0000000..2954a7a --- /dev/null +++ b/svc/go.mod @@ -0,0 +1,13 @@ +module bordro-esleme + +go 1.22.1 + +toolchain go1.22.2 + +require ( + git.makki.io/makki/libgo v0.0.0-20240408174556-52dd3c28a9b9 // indirect + git.notitek.com.tr/common/notgo v0.0.0-20240408194744-d12ce3096580 // indirect + github.com/go-chi/chi/v5 v5.0.12 // indirect + github.com/go-chi/cors v1.2.1 // indirect + github.com/segmentio/ksuid v1.0.4 // indirect +) diff --git a/svc/main.go b/svc/main.go new file mode 100644 index 0000000..8c9af04 --- /dev/null +++ b/svc/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "bordro-esleme/api" + "git.makki.io/makki/libgo/dbu" + "git.makki.io/makki/libgo/mlog" + "git.makki.io/makki/libgo/svc" + "time" +) + +func main() { + // init base service + s := svc.Init("bordro-esleme", "bordroesleme", 40300, 40301) + + // init logger + mlog.InitLogger(s.DevMode()) + + // connnect to db + dbConnectionString := s.DBConnectionString() + err := dbu.Connect(s.Ctx, dbConnectionString, s.SqlsDir(), s.DevMode()) + if err != nil { + mlog.Fatal(err) + } + + //kv table adını atayalım + dbu.DB.SetTableNameKV("sykv") + + err = dbu.DB.Ping(s.Ctx) + if err != nil { + mlog.Fatal(err) + } + + //subscribe to auth server + expire := time.Hour * 1 + if svc.S.DevMode() { + expire = time.Hour * 48 + } + err = s.SubscribeToAuthServer(expire) + if err != nil { + mlog.Fatal(err) + } + + // Init appplication service + s.StartHttp(api.HttpHandler(s.RunEnv())) +}