Skip to content
Closed
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
7 changes: 7 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
"os"

"github.com/andy89923/nf-example/internal/logger"

Check failure on line 7 in internal/context/context.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)
"github.com/andy89923/nf-example/pkg/factory"
"github.com/google/uuid"

"github.com/free5gc/openapi/models"
)

type Task struct {
ID int `json:"id"`
Name string `json:"name"`
}
type NFContext struct {
NfId string
Name string
Expand All @@ -18,6 +23,7 @@
SBIPort int

SpyFamilyData map[string]string
Tasks []Task
}

var nfContext = NFContext{}
Expand Down Expand Up @@ -57,6 +63,7 @@
"Henry": "Henderson",
"Martha": "Marriott",
}
nfContext.Tasks = make([]Task, 0)
}

func GetSelf() *NFContext {
Expand Down
32 changes: 32 additions & 0 deletions internal/sbi/api_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sbi

import (
"net/http"

"github.com/gin-gonic/gin"
)

func (s *Server) HTTPCreateNewTask(c *gin.Context) {
s.Processor().CreateNewTask(c)
}

func (s *Server) HTTPGetAllTasks(c *gin.Context) {
s.Processor().GetAllTasks(c)
}

func (s *Server) getTaskRoute() []Route {
return []Route{
{
Name: "Get All Tasks",
Method: http.MethodGet,
Pattern: "/tasks",
APIFunc: s.HTTPGetAllTasks,
},
{
Name: "Create New Task",
Method: http.MethodPost,
Pattern: "/tasks",
APIFunc: s.HTTPCreateNewTask,
},
}
}
26 changes: 26 additions & 0 deletions internal/sbi/processor/task_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package processor

import (
"net/http"

"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)

func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)

Check failure on line 18 in internal/sbi/processor/task_handler.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

appendAssign: append result not assigned to the same slice (gocritic)

c.JSON(http.StatusCreated, newTask)
}

func (p *Processor) GetAllTasks(c *gin.Context) {
tasks := p.Context().Tasks
Comment on lines +5 to +24
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task creation logic has a race condition where the Tasks slice can be modified by concurrent requests, potentially causing data corruption or lost updates. Consider adding proper synchronization mechanisms like mutexes around the critical section.

Suggested change
"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)
func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)
c.JSON(http.StatusCreated, newTask)
}
func (p *Processor) GetAllTasks(c *gin.Context) {
tasks := p.Context().Tasks
"sync"
"github.com/andy89923/nf-example/internal/context"
"github.com/gin-gonic/gin"
)
var tasksMutex sync.Mutex
func (p *Processor) CreateNewTask(c *gin.Context) {
var newTask context.Task
if err := c.ShouldBindJSON(&newTask); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
tasksMutex.Lock()
tasks := p.Context().Tasks
newTask.ID = len(tasks) + 1
p.Context().Tasks = append(tasks, newTask)
tasksMutex.Unlock()
c.JSON(http.StatusCreated, newTask)
}
func (p *Processor) GetAllTasks(c *gin.Context) {
tasksMutex.Lock()
tasks := p.Context().Tasks
tasksMutex.Unlock()

Copilot uses AI. Check for mistakes.
c.JSON(http.StatusOK, tasks)
}
3 changes: 3 additions & 0 deletions internal/sbi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func newRouter(s *Server) *gin.Engine {
spyFamilyGroup := router.Group("/spyfamily")
applyRoutes(spyFamilyGroup, s.getSpyFamilyRoute())

taskGroup := router.Group("/api")
applyRoutes(taskGroup, s.getTaskRoute())

return router
}

Expand Down
Loading