Added pars config for Server, Added basic struct of server, Added first router Hello, Added test for router Hello

main
Shuhrat Tultaganov 2022-12-07 21:29:01 +03:00
parent de0129d81f
commit 2a5c0b9ffa
11 changed files with 185 additions and 4 deletions

9
Makefile 100644
View File

@ -0,0 +1,9 @@
.PHONY: build
build:
go build -v ./cmd/rest
.DEFAULT_GOAL := build
.PHONY: test
test:
go test -v -race -timeout 30s ./...

View File

@ -1,7 +1,30 @@
package main 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() { 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)
}
} }

View File

View File

@ -0,0 +1,5 @@
bind_addr: ":127.0.0.1"
bind_port: ":8050"
log_level: "debug"

12
go.mod
View File

@ -1,3 +1,15 @@
module git.ukamnya.ru/stulyaganov/RestApi module git.ukamnya.ru/stulyaganov/RestApi
go 1.19 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
)

24
go.sum 100644
View File

@ -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=

View File

@ -6,3 +6,7 @@ type parser struct {
func (p *parser) New() *parser { func (p *parser) New() *parser {
return &parser{} return &parser{}
} }
func (p *parser) Start() {
}

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -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")
}

View File

@ -3,10 +3,10 @@ package api
type getApi struct { type getApi struct {
} }
func (g *GetApi) VideoSdn() { func (g *getApi) VideoSdn() {
} }
func (g *GetApi) Kino() { func (g *getApi) Kino() {
} }