From 2a5c0b9ffafe19eacc98bc65a8f97dd5c4786d1f Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Wed, 7 Dec 2022 21:29:01 +0300 Subject: [PATCH] Added pars config for Server, Added basic struct of server, Added first router Hello, Added test for router Hello --- Makefile | 9 ++++ cmd/rest/main.go | 27 +++++++++- configs/parser/config.yaml | 0 configs/restServer/config.yaml | 5 ++ go.mod | 12 +++++ go.sum | 24 +++++++++ internal/parser/parser.go | 4 ++ internal/restserver/config.go | 33 ++++++++++++ internal/restserver/restserver.go | 54 +++++++++++++++++++ .../restserver/restserver_iternal_test.go | 17 ++++++ pkg/Api/GetApi.go | 4 +- 11 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 Makefile create mode 100644 configs/parser/config.yaml create mode 100644 configs/restServer/config.yaml create mode 100644 go.sum create mode 100644 internal/restserver/config.go create mode 100644 internal/restserver/restserver.go create mode 100644 internal/restserver/restserver_iternal_test.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b438c51 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +.PHONY: build +build: + go build -v ./cmd/rest +.DEFAULT_GOAL := build + + +.PHONY: test +test: + go test -v -race -timeout 30s ./... \ No newline at end of file diff --git a/cmd/rest/main.go b/cmd/rest/main.go index 0f98c3c..3667156 100644 --- a/cmd/rest/main.go +++ b/cmd/rest/main.go @@ -1,7 +1,30 @@ package main -import "fmt" +import ( + "flag" + "log" + + "git.ukamnya.ru/stulyaganov/RestApi/internal/restserver" +) + +var ( + configPath string +) + +func init() { + flag.StringVar(&configPath, "config-path", "configs/restServer/config.yaml", "path for config file") +} func main() { - fmt.Printf("Hello rest api") + + flag.Parse() + + config := restserver.NewConfig() + if err := config.SetConfig(configPath); err != nil { + log.Fatal(err) + } + s := restserver.New(config) + if err := s.Start(); err != nil { + log.Fatal(err) + } } diff --git a/configs/parser/config.yaml b/configs/parser/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/configs/restServer/config.yaml b/configs/restServer/config.yaml new file mode 100644 index 0000000..3dce3a3 --- /dev/null +++ b/configs/restServer/config.yaml @@ -0,0 +1,5 @@ +bind_addr: ":127.0.0.1" + +bind_port: ":8050" + +log_level: "debug" \ No newline at end of file diff --git a/go.mod b/go.mod index 2497dba..bed5925 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,15 @@ module git.ukamnya.ru/stulyaganov/RestApi go 1.19 + +require github.com/sirupsen/logrus v1.9.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gorilla/mux v1.8.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.1 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..278949e --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/parser/parser.go b/internal/parser/parser.go index fa41e60..aeb8c84 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -6,3 +6,7 @@ type parser struct { func (p *parser) New() *parser { return &parser{} } + +func (p *parser) Start() { + +} diff --git a/internal/restserver/config.go b/internal/restserver/config.go new file mode 100644 index 0000000..6408a5d --- /dev/null +++ b/internal/restserver/config.go @@ -0,0 +1,33 @@ +package restserver + +import ( + "io/ioutil" + + "gopkg.in/yaml.v3" +) + +type Config struct { + BindAddr string `yaml:"bind_addr"` + BindPort string `yaml:"bind_port"` + LogLevel string `yaml:"log_level"` +} + +func NewConfig() *Config { + return &Config{ + BindAddr: ":127.0.0.1", + BindPort: ":8080", + LogLevel: "debug", + } +} + +func (c *Config) SetConfig(path string) error { + yamlPars, err := ioutil.ReadFile(path) + if err != nil { + return err + } + err = yaml.Unmarshal(yamlPars, c) + if err != nil { + return err + } + return nil +} diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go new file mode 100644 index 0000000..e6b703b --- /dev/null +++ b/internal/restserver/restserver.go @@ -0,0 +1,54 @@ +package restserver + +import ( + "io" + "net/http" + + "github.com/gorilla/mux" + "github.com/sirupsen/logrus" +) + +type RestServer struct { + config *Config + logger *logrus.Logger + router *mux.Router +} + +func New(config *Config) *RestServer { + return &RestServer{ + config: config, + logger: logrus.New(), + router: mux.NewRouter(), + } +} + +func (r *RestServer) Start() error { + if err := r.configureLogger(); err != nil { + return err + } + r.configureRouter() + + r.logger.Info("Starting Server") + r.logger.Info("Listen server on ", r.config.BindPort, " Port") + return http.ListenAndServe(r.config.BindPort, r.router) +} + +func (r *RestServer) configureLogger() error { + level, err := logrus.ParseLevel(r.config.LogLevel) + if err != nil { + return err + } + r.logger.SetLevel(level) + return nil +} + +func (r *RestServer) configureRouter() { + r.router.HandleFunc("/hello", r.HandleHello()) +} + +func (r *RestServer) HandleHello() http.HandlerFunc { + + return func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, "TEsts Hello") + } +} diff --git a/internal/restserver/restserver_iternal_test.go b/internal/restserver/restserver_iternal_test.go new file mode 100644 index 0000000..94bf7ae --- /dev/null +++ b/internal/restserver/restserver_iternal_test.go @@ -0,0 +1,17 @@ +package restserver + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_RestServer_Hendler_Hello(t *testing.T) { + r := New(NewConfig()) + rec := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/hello", nil) + r.HandleHello().ServeHTTP(rec, req) + assert.Equal(t, rec.Body.String(), "TEsts Hello") +} diff --git a/pkg/Api/GetApi.go b/pkg/Api/GetApi.go index b4ae5c9..dee773b 100644 --- a/pkg/Api/GetApi.go +++ b/pkg/Api/GetApi.go @@ -3,10 +3,10 @@ package api type getApi struct { } -func (g *GetApi) VideoSdn() { +func (g *getApi) VideoSdn() { } -func (g *GetApi) Kino() { +func (g *getApi) Kino() { }