This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>The bird encyclopedia</title> | |
</head> | |
<body> | |
<h1>The bird encyclopedia</h1> | |
<!-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getBirdHandler(w http.ResponseWriter, r *http.Request) { | |
//Convert the "birds" variable to json | |
birdListBytes, err := json.Marshal(birds) | |
// If there is an error, print it to the console, and return a server | |
// error response to the user | |
if err != nil { | |
fmt.Println(fmt.Errorf("Error: %v", err)) | |
w.WriteHeader(http.StatusInternalServerError) | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func createBirdHandler(w http.ResponseWriter, r *http.Request) { | |
// Create a new instance of Bird | |
bird := Bird{} | |
// We send all our data as HTML form data | |
// the `ParseForm` method of the request, parses the | |
// form values | |
err := r.ParseForm() | |
// In case of any error, we respond with an error to the user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// The sql go library is needed to interact with the database | |
import ( | |
"database/sql" | |
) | |
// Our store will have two methods, to add a new bird, | |
// and to get all existing birds | |
// Each method returns an error, in case something goes wrong |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"testing" | |
// The "testify/suite" package is used to make the test suite | |
"github.com/stretchr/testify/suite" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
type Bird struct { | |
Species string `json:"species"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
type Bird struct { | |
Species string `json:"species"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/stretchr/testify/mock" | |
) | |
// The mock store contains additonal methods for inspection | |
type MockStore struct { | |
mock.Mock | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"net/http" | |
"net/http/httptest" | |
"net/url" | |
"strconv" | |
"testing" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
//... | |
// The libn/pq driver is used for postgres | |
_ "github.com/lib/pq" | |
//... | |
) | |
func main(){ | |
// ... | |
connString := "dbname=<your main db name> sslmode=disable" |