Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sensitivity check module #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/sensitive/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions sensitivity/api/route.go
Original file line number Diff line number Diff line change
@@ -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)

}
28 changes: 28 additions & 0 deletions sensitivity/api/sensitive.go
Original file line number Diff line number Diff line change
@@ -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})
}
50 changes: 50 additions & 0 deletions sensitivity/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions sensitivity/schema/schema.go
Original file line number Diff line number Diff line change
@@ -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
}