Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 19, 2016
0 parents commit 677273a
Show file tree
Hide file tree
Showing 189 changed files with 22,279 additions and 0 deletions.
Binary file added 9781484210536.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2015 Shiju Varghese

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
#Apress Source Code

This repository accompanies [*Web Development with Go*](http://www.apress.com/9781484210536) by Shiju Varghese (Apress, 2015).

![Cover image](9781484210536.jpg)

Download the files as a zip using the green button, or clone the repository to your machine using Git.

##Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

##Contributions

See the file Contributing.md for more information on how you can contribute to this repository.
14 changes: 14 additions & 0 deletions contributing.md
@@ -0,0 +1,14 @@
# Contributing to Apress Source Code

Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers.

## How to Contribute

1. Make sure you have a GitHub account.
2. Fork the repository for the relevant book.
3. Create a new branch on which to make your change, e.g.
`git checkout -b my_code_contribution`
4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted.
5. Submit a pull request.

Thank you for your contribution!
24 changes: 24 additions & 0 deletions go-web-master/.gitignore
@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
22 changes: 22 additions & 0 deletions go-web-master/LICENSE
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Shiju Varghese

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1 change: 1 addition & 0 deletions go-web-master/README.md
@@ -0,0 +1 @@
# go-web
8 changes: 8 additions & 0 deletions go-web-master/chapter-1/calc/calc.go
@@ -0,0 +1,8 @@
package calc

func Add(x, y int) int {
return x + y
}
func Subtract(x, y int) int {
return x - y
}
18 changes: 18 additions & 0 deletions go-web-master/chapter-1/calc/calc_test.go
@@ -0,0 +1,18 @@
package calc

import "testing"

func TestAdd(t *testing.T) {
var v int
v = Add(15,10)
if v != 25 {
t.Error("Expected 25, got ", v)
}
}
func TestSubtract(t *testing.T) {
var v int
v = Subtract(15,10)
if v != 5 {
t.Error("Expected 5, got ", v)
}
}
14 changes: 14 additions & 0 deletions go-web-master/chapter-1/calcdemo/main.go
@@ -0,0 +1,14 @@
package main

import (
"fmt"

"github.com/shijuvar/go-web/chapter-1/calc"
)

func main() {
//var x, y int= 10, 5
x, y := 10, 5
fmt.Println(calc.Add(x, y))
fmt.Println(calc.Subtract(x, y))
}
7 changes: 7 additions & 0 deletions go-web-master/chapter-1/hello/main.go
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, world")
}
15 changes: 15 additions & 0 deletions go-web-master/chapter-1/web/main.go
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
69 changes: 69 additions & 0 deletions go-web-master/chapter-10/httptest/main.go
@@ -0,0 +1,69 @@
package main

import (
"encoding/json"
"errors"
"net/http"

"github.com/gorilla/mux"
)

type User struct {
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Email string `json:email"`
}

var userStore = []User{}

func getUsers(w http.ResponseWriter, r *http.Request) {
users, err := json.Marshal(userStore)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(users)

}
func createUser(w http.ResponseWriter, r *http.Request) {

var user User
// Decode the incoming User json
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Validate the User entity
err = validate(user)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Insert User entity into User Store
userStore = append(userStore, user)
w.WriteHeader(http.StatusCreated)
}

// Validate User entity
func validate(user User) error {
for _, u := range userStore {
if u.Email == user.Email {
return errors.New("The Email is already exists")
}
}
return nil
}
func SetUserRoutes() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/users", createUser).Methods("POST")
r.HandleFunc("/users", getUsers).Methods("GET")
return r
}

func main() {
http.ListenAndServe(":8080", SetUserRoutes())
}
106 changes: 106 additions & 0 deletions go-web-master/chapter-10/httptest/main_test.go
@@ -0,0 +1,106 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gorilla/mux"
)

func TestGetUsers(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/users", getUsers).Methods("GET")
req, err := http.NewRequest("GET", "/users", nil)
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()

r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("HTTP Status expected: 200, got: %d", w.Code)
}
}

func TestCreateUser(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/users", createUser).Methods("POST")

userJson := `{"firstname": "shiju", "lastname": "Varghese", "email": "shiju@xyz.com"}`

req, err := http.NewRequest(
"POST",
"/users",
strings.NewReader(userJson),
)
if err != nil {
t.Error(err)
}

w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 201 {
t.Errorf("HTTP Status expected: 201, got: %d", w.Code)
}
}
func TestUniqueEmail(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/users", createUser).Methods("POST")

userJson := `{"firstname": "shiju", "lastname": "Varghese", "email": "shiju@xyz.com"}`

req, err := http.NewRequest(
"POST",
"/users",
strings.NewReader(userJson),
)
if err != nil {
t.Error(err)
}

w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 400 {
t.Error("Bad Request expected, got: %d", w.Code)
}
}
func TestGetUsersClient(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/users", getUsers).Methods("GET")
server := httptest.NewServer(r)
defer server.Close()
usersUrl := fmt.Sprintf("%s/users", server.URL)
request, err := http.NewRequest("GET", usersUrl, nil)

res, err := http.DefaultClient.Do(request)

if err != nil {
t.Error(err)
}

if res.StatusCode != 200 {
t.Errorf("HTTP Status expected: 200, got: %d", res.StatusCode)
}
}
func TestCreateUserClient(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/users", createUser).Methods("POST")
server := httptest.NewServer(r)
defer server.Close()
usersUrl := fmt.Sprintf("%s/users", server.URL)
userJson := `{"firstname": "Rosmi", "lastname": "Shiju", "email": "rose@xyz.com"}`
request, err := http.NewRequest("POST", usersUrl, strings.NewReader(userJson))

res, err := http.DefaultClient.Do(request)

if err != nil {
t.Error(err)
}

if res.StatusCode != 201 {
t.Errorf("HTTP Status expected: 201, got: %d", res.StatusCode)
}
}

0 comments on commit 677273a

Please sign in to comment.