Skip to content

Commit

Permalink
Fix the swagger spec and failing webtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jul 7, 2018
1 parent 5c63dc2 commit 058f672
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 35 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ You must use Go 1.7 or newer because it uses the http context.

## Quick Start with MySQL

Use one of the following commands to start a MySQL container with Docker:

- Start MySQL without a password: `docker run -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.7`
- Start MySQL with a password: `docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=somepassword mysql:5.7`

Start MySQL and import `migration/mysql.sql` to create the database and tables.

Copy `config.json` to `src/app/webapi/cmd/webapi/config.json` and edit the
Expand Down
9 changes: 0 additions & 9 deletions src/app/webapi/cmd/webapi/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"os"
"runtime"
"time"

"app/webapi"
"app/webapi/middleware"
Expand All @@ -20,14 +19,6 @@ func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}

// Clock is a clock.
type Clock struct{}

// Now returns the current time.
func (c *Clock) Now() time.Time {
return time.Now()
}

func main() {
// Create the logger.
appLogger := log.New(os.Stderr, "", log.LstdFlags)
Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/user/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCreate(t *testing.T) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, http.StatusCreated, w.Code)
assert.Contains(t, w.Body.String(), `{"status":"Created","record_id"`)
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/webapi/component/user/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (p *Endpoint) Show(w http.ResponseWriter, r *http.Request) (int, error) {
// Required: true
Status string `json:"status"`
// Required: true
Data []store.TUser `json:"data"`
Data []store.User `json:"data"`
}
}

resp := new(response)
return p.Response.Results(w, &resp.Body, []store.TUser{*u})
return p.Response.Results(w, &resp.Body, []store.User{*u})
}
3 changes: 3 additions & 0 deletions src/app/webapi/internal/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (o *Output) OK(w http.ResponseWriter, message string) (int, error) {
r.Body.Message = message

// Write the content.
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(r.Body)
if err != nil {
Expand All @@ -37,6 +38,7 @@ func (o *Output) Created(w http.ResponseWriter, recordID string) (int, error) {
r.Body.RecordID = recordID

// Write the content.
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(r.Body)
if err != nil {
Expand Down Expand Up @@ -89,6 +91,7 @@ func (o *Output) Results(w http.ResponseWriter, body interface{}, data interface
}

// Write the content.
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(body)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions src/app/webapi/internal/webtoken/clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package webtoken

import "time"

// IClock represents a system clock.
type IClock interface {
Now() time.Time
}

// clock is the standard system clock.
type clock struct{}

// Now returns the current time.
func (c *clock) Now() time.Time {
return time.Now()
}
6 changes: 1 addition & 5 deletions src/app/webapi/internal/webtoken/webtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,10 @@ type Configuration struct {
Secret SecretKey `json:"Secret"`
}

// IClock represents a clock.
type IClock interface {
Now() time.Time
}

// New creates a new JWT configuration.
func New(secret []byte) *Configuration {
return &Configuration{
clock: new(clock),
Secret: secret,
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/app/webapi/store/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

// NewUser returns a new query object.
func NewUser(db component.IDatabase, q component.IQuery) *TUser {
return &TUser{
func NewUser(db component.IDatabase, q component.IQuery) *User {
return &User{
IQuery: q,
db: db,
}
}

// TUser represents a user.
type TUser struct {
// User is a user of the system.
type User struct {
component.IQuery `json:"-"`
db component.IDatabase

Expand All @@ -32,23 +32,23 @@ type TUser struct {
}

// Table returns the table name.
func (x *TUser) Table() string {
func (x *User) Table() string {
return "user"
}

// PrimaryKey returns the primary key field.
func (x *TUser) PrimaryKey() string {
func (x *User) PrimaryKey() string {
return "id"
}

// NewGroup returns an empty group.
func (x *TUser) NewGroup() *TUserGroup {
func (x *User) NewGroup() *TUserGroup {
group := make(TUserGroup, 0)
return &group
}

// TUserGroup represents a group of users.
type TUserGroup []TUser
// UserGroup represents a group of users.
type TUserGroup []User

// Table returns the table name.
func (x TUserGroup) Table() string {
Expand All @@ -65,7 +65,7 @@ func (x TUserGroup) PrimaryKey() string {
// *****************************************************************************

// Create adds a new user.
func (x *TUser) Create(firstName, lastName, email, password string) (string, error) {
func (x *User) Create(firstName, lastName, email, password string) (string, error) {
// Generate a UUID.
uuid, err := securegen.UUID()
if err != nil {
Expand All @@ -89,7 +89,7 @@ func (x *TUser) Create(firstName, lastName, email, password string) (string, err
// *****************************************************************************

// Update makes changes to one entity.
func (x *TUser) Update(ID, firstName, lastName, email, password string) (err error) {
func (x *User) Update(ID, firstName, lastName, email, password string) (err error) {
// Update the entity.
_, err = x.db.Exec(`
UPDATE user
Expand Down
18 changes: 11 additions & 7 deletions src/app/webapi/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
"definitions": {
"TUser": {
"type": "object",
"title": "TUser represents users.",
"title": "TUser is a user of the system.",
"properties": {
"created_at": {
"type": "string",
Expand Down Expand Up @@ -337,7 +337,15 @@
"x-go-name": "UpdatedAt"
}
},
"x-go-package": "app/webapi/component/user"
"x-go-package": "app/webapi/store"
},
"TUserGroup": {
"type": "array",
"title": "TUserGroup represents a group of users.",
"items": {
"$ref": "#/definitions/TUser"
},
"x-go-package": "app/webapi/store"
}
},
"responses": {
Expand Down Expand Up @@ -506,11 +514,7 @@
],
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/TUser"
},
"x-go-name": "Data"
"$ref": "#/definitions/TUserGroup"
},
"status": {
"type": "string",
Expand Down

0 comments on commit 058f672

Please sign in to comment.