Skip to content

Commit

Permalink
refactor(config): integrated yaml for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
benschw committed Jul 10, 2014
1 parent d6e17a1 commit b77095a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
2 changes: 2 additions & 0 deletions client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func makeRequest(method string, url string, entity interface{}) (*http.Response,
}
return http.DefaultClient.Do(req)
}

func buildRequest(method string, url string, entity interface{}) (*http.Request, error) {
body, err := encodeEntity(entity)
if err != nil {
Expand All @@ -32,6 +33,7 @@ func buildRequest(method string, url string, entity interface{}) (*http.Request,
req.Header.Set("content-type", "application/json")
return req, err
}

func encodeEntity(entity interface{}) (io.Reader, error) {
if entity == nil {
return nil, nil
Expand Down
3 changes: 0 additions & 3 deletions client/todo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (tc *TodoClient) CreateTodo(title string, description string) (api.Todo, er
}
err = processResponseEntity(r, &respTodo, 201)
return respTodo, err

}

func (tc *TodoClient) GetAllTodos() ([]api.Todo, error) {
Expand All @@ -36,7 +35,6 @@ func (tc *TodoClient) GetAllTodos() ([]api.Todo, error) {
}
err = processResponseEntity(r, &respTodos, 200)
return respTodos, err

}

func (tc *TodoClient) GetTodo(id int32) (api.Todo, error) {
Expand All @@ -49,7 +47,6 @@ func (tc *TodoClient) GetTodo(id int32) (api.Todo, error) {
}
err = processResponseEntity(r, &respTodo, 200)
return respTodo, err

}

func (tc *TodoClient) UpdateTodo(todo api.Todo) (api.Todo, error) {
Expand Down
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
svchost: :8080
dbuser: root
dbpassword: ""
dbhost: localhost
dbname: Todo
39 changes: 29 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
package main

import (
"errors"
"github.com/benschw/go-todo/service"
"gopkg.in/yaml.v1"
"io/ioutil"
"log"
"os"
)

func getConfig(yamlPath string) (service.Config, error) {
config := service.Config{}

if _, err := os.Stat(yamlPath); err != nil {
return config, errors.New("config path not valid")
}

ymlData, err := ioutil.ReadFile(yamlPath)
if err != nil {
return config, err
}

err = yaml.Unmarshal([]byte(ymlData), &config)
return config, err
}

func main() {
var (
svcHost = ":8080" // 0.0.0.0:8080
dbUser = "root"
dbPassword = ""
dbHost = "localhost"
dbName = "Todo"
)
svc := service.TodoService{SvcHost: svcHost, DbUser: dbUser, DbPassword: dbPassword, DbHost: dbHost, DbName: dbName}

err := svc.Run()
yamlPath := "config.yaml"

cfg, err := getConfig(yamlPath)
if err != nil {
log.Fatal(err)
os.Exit(1)
}

svc := service.TodoService{}

if err = svc.Run(cfg); err != nil {
log.Fatal(err)
os.Exit(1)
}

}
11 changes: 7 additions & 4 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
"github.com/jinzhu/gorm"
)

type TodoService struct {
type Config struct {
SvcHost string
DbUser string
DbPassword string
DbHost string
DbName string
}

func (s *TodoService) Run() error {
connectionString := s.DbUser + ":" + s.DbPassword + "@tcp(" + s.DbHost + ":3306)/" + s.DbName + "?charset=utf8&parseTime=True"
type TodoService struct {
}

func (s *TodoService) Run(cfg Config) error {
connectionString := cfg.DbUser + ":" + cfg.DbPassword + "@tcp(" + cfg.DbHost + ":3306)/" + cfg.DbName + "?charset=utf8&parseTime=True"

db, err := gorm.Open("mysql", connectionString)
if err != nil {
Expand All @@ -34,7 +37,7 @@ func (s *TodoService) Run() error {
r.PATCH("/todo/:id", todoResource.PatchTodo)
r.DELETE("/todo/:id", todoResource.DeleteTodo)

r.Run(s.SvcHost)
r.Run(cfg.SvcHost)

return nil
}
5 changes: 4 additions & 1 deletion service/todo_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (tr *TodoResource) GetAllTodos(c *gin.Context) {
func (tr *TodoResource) GetTodo(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(400, api.NewError("problem decoding id sent"))
return
}

Expand All @@ -56,6 +57,7 @@ func (tr *TodoResource) GetTodo(c *gin.Context) {
func (tr *TodoResource) UpdateTodo(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(400, api.NewError("problem decoding id sent"))
return
}

Expand All @@ -81,6 +83,7 @@ func (tr *TodoResource) UpdateTodo(c *gin.Context) {
func (tr *TodoResource) PatchTodo(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(400, api.NewError("problem decoding id sent"))
return
}

Expand Down Expand Up @@ -113,6 +116,7 @@ func (tr *TodoResource) PatchTodo(c *gin.Context) {
func (tr *TodoResource) DeleteTodo(c *gin.Context) {
id, err := tr.getId(c)
if err != nil {
c.JSON(400, api.NewError("problem decoding id sent"))
return
}

Expand All @@ -133,7 +137,6 @@ func (tr *TodoResource) getId(c *gin.Context) (int32, error) {
id, err := strconv.Atoi(idStr)
if err != nil {
log.Print(err)
c.JSON(400, api.NewError("problem decoding id sent"))
return 0, err
}
return int32(id), nil
Expand Down

0 comments on commit b77095a

Please sign in to comment.