From 320d614538262a830ed13047ff5e9b450ea309c1 Mon Sep 17 00:00:00 2001 From: HiImPeggy Date: Wed, 5 Mar 2025 13:26:20 +0800 Subject: [PATCH 1/5] feat: add greeting api with POST and GET --- go.mod | 3 -- internal/sbi/api_greeting.go | 36 +++++++++++++++ internal/sbi/api_greeting_test.go | 77 +++++++++++++++++++++++++++++++ internal/sbi/router.go | 3 ++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 internal/sbi/api_greeting.go create mode 100644 internal/sbi/api_greeting_test.go diff --git a/go.mod b/go.mod index a313925..d36d9ba 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/google/uuid v1.6.0 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.15 go.uber.org/mock v0.4.0 gopkg.in/yaml.v2 v2.4.0 @@ -19,7 +18,6 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -35,7 +33,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/internal/sbi/api_greeting.go b/internal/sbi/api_greeting.go new file mode 100644 index 0000000..4a21dcb --- /dev/null +++ b/internal/sbi/api_greeting.go @@ -0,0 +1,36 @@ +package sbi + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) getGreetingRoute() []Route { + return []Route{ + { + Name: "Greeting", + Method: "GET", + Pattern: "/", + APIFunc: s.GetGreeting, + // Use + // curl -X GET http://127.0.0.163:8000/greeting -w "\n" + }, + { + Name: "Farewell", + Method: "POST", + Pattern: "/", + APIFunc: s.PostFarewell, + // Use + // curl -X POST http://127.0.0.163:8000/greeting -w "\n" + }, + } +} + +func (s *Server) GetGreeting(c *gin.Context) { + c.String(http.StatusOK, "Greetings!\n") +} + +func (s *Server) PostFarewell(c *gin.Context) { + c.String(http.StatusOK, "Farewells!\n") +} diff --git a/internal/sbi/api_greeting_test.go b/internal/sbi/api_greeting_test.go new file mode 100644 index 0000000..0f15041 --- /dev/null +++ b/internal/sbi/api_greeting_test.go @@ -0,0 +1,77 @@ +package sbi_test + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/andy89923/nf-example/internal/sbi" + "github.com/andy89923/nf-example/pkg/factory" + "github.com/gin-gonic/gin" + "go.uber.org/mock/gomock" +) + +func Test_getGreetingRoutes(t *testing.T) { + gin.SetMode(gin.TestMode) + + mockCtrl := gomock.NewController(t) + nfApp := sbi.NewMocknfApp(mockCtrl) + nfApp.EXPECT().Config().Return(&factory.Config{ + Configuration: &factory.Configuration{ + Sbi: &factory.Sbi{ + Port: 8000, + }, + }, + }).AnyTimes() + server := sbi.NewServer(nfApp, "") + + t.Run("GetGreeting", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const EXPECTED_BODY = "Greetings!\n" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("GET", "/greeting", nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.GetGreeting(ginCtx) + + if httpRecorder.Code != EXPECTED_STATUS { + t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) + } + + if httpRecorder.Body.String() != EXPECTED_BODY { + t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) + } + }) + + t.Run("PostFarewell", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const EXPECTED_BODY = "Farewells!\n" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + ginCtx.Request, err = http.NewRequest("POST", "/greeting", nil) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.PostFarewell(ginCtx) + + if httpRecorder.Code != EXPECTED_STATUS { + t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) + } + + if httpRecorder.Body.String() != EXPECTED_BODY { + t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) + } + }) +} diff --git a/internal/sbi/router.go b/internal/sbi/router.go index 1d16794..6b4c537 100644 --- a/internal/sbi/router.go +++ b/internal/sbi/router.go @@ -63,6 +63,9 @@ func newRouter(s *Server) *gin.Engine { helloGroup := router.Group("/hello") applyRoutes(helloGroup, s.getHelloRoute()) + greetingGroup := router.Group("/greeting") + applyRoutes(greetingGroup, s.getGreetingRoute()) + return router } From 9ca333b69d2dc8551a98810962699cf0fa8cd3eb Mon Sep 17 00:00:00 2001 From: HiImPeggy Date: Tue, 25 Mar 2025 16:19:45 +0800 Subject: [PATCH 2/5] feat: Get some data from POST --- internal/sbi/api_greeting.go | 27 +++++++++++++++++++++++++-- internal/sbi/api_greeting_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/internal/sbi/api_greeting.go b/internal/sbi/api_greeting.go index 4a21dcb..e136754 100644 --- a/internal/sbi/api_greeting.go +++ b/internal/sbi/api_greeting.go @@ -14,7 +14,7 @@ func (s *Server) getGreetingRoute() []Route { Pattern: "/", APIFunc: s.GetGreeting, // Use - // curl -X GET http://127.0.0.163:8000/greeting -w "\n" + // curl -X GET http://127.0.0.163:8000/greeting/ -w "\n" }, { Name: "Farewell", @@ -22,7 +22,15 @@ func (s *Server) getGreetingRoute() []Route { Pattern: "/", APIFunc: s.PostFarewell, // Use - // curl -X POST http://127.0.0.163:8000/greeting -w "\n" + // curl -X POST http://127.0.0.163:8000/greeting/ -w "\n" + }, + { + Name: "Farewell to someone", + Method: http.MethodPost, + Pattern: "/to", + APIFunc: s.Greetingto, + // Use + // curl -X POST http://127.0.0.163:8000/greeting/to -d '{"name": "Alisa"}' -w "\n" }, } } @@ -34,3 +42,18 @@ func (s *Server) GetGreeting(c *gin.Context) { func (s *Server) PostFarewell(c *gin.Context) { c.String(http.StatusOK, "Farewells!\n") } + +func (s *Server) Greetingto(c *gin.Context) { + var names struct { + Name string `json:"name"` + } + + err := c.ShouldBindJSON(&names) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + list.Characters = append(list.Characters, names.Name) + c.String(http.StatusOK, "Farewell ~ "+names.Name+"!") +} \ No newline at end of file diff --git a/internal/sbi/api_greeting_test.go b/internal/sbi/api_greeting_test.go index 0f15041..936c5ca 100644 --- a/internal/sbi/api_greeting_test.go +++ b/internal/sbi/api_greeting_test.go @@ -4,6 +4,7 @@ import ( "net/http" "net/http/httptest" "testing" + "strings" "github.com/andy89923/nf-example/internal/sbi" "github.com/andy89923/nf-example/pkg/factory" @@ -74,4 +75,31 @@ func Test_getGreetingRoutes(t *testing.T) { t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) } }) + + t.Run("Farewell to ", func(t *testing.T) { + const EXPECTED_STATUS = http.StatusOK + const NAME = "Alisa" + const EXPECTED_BODY = "Farewell ~ " + NAME + "!" + + httpRecorder := httptest.NewRecorder() + ginCtx, _ := gin.CreateTestContext(httpRecorder) + + var err error + jsonBody := `{"name":"` + NAME + `"}` + ginCtx.Request, err = http.NewRequest("POST", "/greeting/to", strings.NewReader(jsonBody)) + if err != nil { + t.Errorf("Failed to create request: %s", err) + return + } + + server.Greetingto(ginCtx) + + if httpRecorder.Code != EXPECTED_STATUS { + t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) + } + + if httpRecorder.Body.String() != EXPECTED_BODY { + t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) + } + }) } From d3e162c1d7f2d6af24e6fbf5395e6cab4ce97dea Mon Sep 17 00:00:00 2001 From: HiImPeggy Date: Tue, 25 Mar 2025 16:46:28 +0800 Subject: [PATCH 3/5] fix: test problem and gci problem --- internal/sbi/api_greeting.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/sbi/api_greeting.go b/internal/sbi/api_greeting.go index e136754..254be25 100644 --- a/internal/sbi/api_greeting.go +++ b/internal/sbi/api_greeting.go @@ -6,6 +6,10 @@ import ( "github.com/gin-gonic/gin" ) +var name_list struct { + someone_name []string +} + func (s *Server) getGreetingRoute() []Route { return []Route{ { @@ -54,6 +58,6 @@ func (s *Server) Greetingto(c *gin.Context) { return } - list.Characters = append(list.Characters, names.Name) + name_list.someone_name = append(name_list.someone_name, names.Name) c.String(http.StatusOK, "Farewell ~ "+names.Name+"!") } \ No newline at end of file From bd13cc929d419b8fe352048eb79309136730a5bb Mon Sep 17 00:00:00 2001 From: HiImPeggy Date: Tue, 25 Mar 2025 21:46:41 +0800 Subject: [PATCH 4/5] fix: reslove gci issue --- go | 0 internal/sbi/api_greeting.go | 8 ++++---- internal/sbi/api_greeting_test.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 go diff --git a/go b/go new file mode 100644 index 0000000..e69de29 diff --git a/internal/sbi/api_greeting.go b/internal/sbi/api_greeting.go index 254be25..229c506 100644 --- a/internal/sbi/api_greeting.go +++ b/internal/sbi/api_greeting.go @@ -6,8 +6,8 @@ import ( "github.com/gin-gonic/gin" ) -var name_list struct { - someone_name []string +var namelist struct { + firstname []string } func (s *Server) getGreetingRoute() []Route { @@ -58,6 +58,6 @@ func (s *Server) Greetingto(c *gin.Context) { return } - name_list.someone_name = append(name_list.someone_name, names.Name) + namelist.firstname = append(namelist.firstname, names.Name) c.String(http.StatusOK, "Farewell ~ "+names.Name+"!") -} \ No newline at end of file +} diff --git a/internal/sbi/api_greeting_test.go b/internal/sbi/api_greeting_test.go index 936c5ca..ac10a59 100644 --- a/internal/sbi/api_greeting_test.go +++ b/internal/sbi/api_greeting_test.go @@ -3,8 +3,8 @@ package sbi_test import ( "net/http" "net/http/httptest" - "testing" "strings" + "testing" "github.com/andy89923/nf-example/internal/sbi" "github.com/andy89923/nf-example/pkg/factory" @@ -76,7 +76,7 @@ func Test_getGreetingRoutes(t *testing.T) { } }) - t.Run("Farewell to ", func(t *testing.T) { + t.Run("Farewell to", func(t *testing.T) { const EXPECTED_STATUS = http.StatusOK const NAME = "Alisa" const EXPECTED_BODY = "Farewell ~ " + NAME + "!" From f8162a78badf07f31b288cd03ad64c4ded9fe5ab Mon Sep 17 00:00:00 2001 From: HiImPeggy Date: Wed, 26 Mar 2025 02:16:58 +0800 Subject: [PATCH 5/5] docs: remove redundant file --- go | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 go diff --git a/go b/go deleted file mode 100644 index e69de29..0000000