diff --git a/common/sensitive/api.go b/common/sensitive/api.go index cd24527..df4090a 100644 --- a/common/sensitive/api.go +++ b/common/sensitive/api.go @@ -31,9 +31,9 @@ type ParamsForCheck struct { } type ResponseForCheck struct { - Pass bool - Labels []int - Detail string + Pass bool `json:"content"` + Labels []int `json:"labels,omitempty"` + Detail string `json:"detail,omitempty"` } func CheckSensitive(params ParamsForCheck) (resp *ResponseForCheck, err error) { diff --git a/sensitivity/api/route.go b/sensitivity/api/route.go new file mode 100644 index 0000000..ceafe94 --- /dev/null +++ b/sensitivity/api/route.go @@ -0,0 +1,28 @@ +package api + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/swagger" +) + +func RegisterRouts(app *fiber.App) { + app.Get("/", func(c *fiber.Ctx) error { + return c.Redirect("/api") + }) + app.Get("/docs", func(c *fiber.Ctx) error { + return c.Redirect("/docs/index.html") + }) + app.Get("/docs/*", swagger.HandlerDefault) + + api := app.Group("/api") + registerRoutes(api) +} + +func registerRoutes(r fiber.Router) { + r.Get("", func(c *fiber.Ctx) error { + return c.JSON(fiber.Map{"message": "Welcome to sensitivity checker API"}) + }) + + r.Post("/sensitive/text", CheckSensitiveText) + +} diff --git a/sensitivity/api/sensitive.go b/sensitivity/api/sensitive.go new file mode 100644 index 0000000..4265846 --- /dev/null +++ b/sensitivity/api/sensitive.go @@ -0,0 +1,28 @@ +package api + +import ( + "github.com/gofiber/fiber/v2" + . "github.com/opentreehole/backend/common" + "github.com/opentreehole/backend/common/sensitive" + . "github.com/opentreehole/backend/sensitivity/schema" + "time" +) + +func CheckSensitiveText(c *fiber.Ctx) (err error) { + var request SensitiveCheckRequest + err = ValidateBody(c, &request) + if err != nil { + return err + } + + sensitiveResp, err := sensitive.CheckSensitive(sensitive.ParamsForCheck{ + Content: request.Content, + Id: time.Now().UnixNano(), + TypeName: sensitive.TypeTitle, + }) + + if err != nil { + return nil + } + return c.JSON(SensitiveCheckResponse{ResponseForCheck: *sensitiveResp}) +} diff --git a/sensitivity/main.go b/sensitivity/main.go new file mode 100644 index 0000000..4c54e51 --- /dev/null +++ b/sensitivity/main.go @@ -0,0 +1,50 @@ +package notification + +import ( + "context" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/recover" + "github.com/opentreehole/backend/common" + "github.com/opentreehole/backend/sensitivity/api" + "github.com/spf13/viper" + "log/slog" + "os" + "os/signal" + "syscall" +) + +func main() { + var disableStartupMessage = false + if viper.GetString(common.EnvMode) == "prod" { + disableStartupMessage = true + } + app := fiber.New(fiber.Config{ + ErrorHandler: common.ErrorHandler, + DisableStartupMessage: disableStartupMessage, + }) + registerMiddlewares(app) + api.RegisterRouts(app) + go func() { + err := app.Listen("0.0.0.0:8000") + if err != nil { + slog.LogAttrs(context.Background(), slog.LevelError, "app listen failed", slog.String("err", err.Error())) + } + }() + + interrupt := make(chan os.Signal, 1) + + // wait for CTRL-C interrupt + signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM) + <-interrupt + + // close app + err := app.Shutdown() + if err != nil { + slog.LogAttrs(context.Background(), slog.LevelError, "shutdown failed", slog.String("err", err.Error())) + } +} + +func registerMiddlewares(app *fiber.App) { + app.Use(recover.New(recover.Config{EnableStackTrace: true})) + app.Use(common.MiddlewareGetUserID) +} diff --git a/sensitivity/schema/schema.go b/sensitivity/schema/schema.go new file mode 100644 index 0000000..0f6e54b --- /dev/null +++ b/sensitivity/schema/schema.go @@ -0,0 +1,11 @@ +package schema + +import "github.com/opentreehole/backend/common/sensitive" + +type SensitiveCheckRequest struct { + Content string `json:"content" validate:"required"` +} + +type SensitiveCheckResponse struct { + sensitive.ResponseForCheck +}