Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "database/sql"
- "fmt"
- "net/http"
- "github.com/gorilla/mux"
- _ "github.com/lib/pq"
- )
- const (
- host = "localhost"
- port = 5432
- user = "postgres"
- password = "0102"
- dbname = "bird_encyclopaedia"
- )
- func newRouter() *mux.Router {
- r := mux.NewRouter()
- r.HandleFunc("/hello", handler).Methods("GET")
- staticFileDirectory := http.Dir("./assets/")
- staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
- r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")
- r.HandleFunc("/bird", getBirdHandler).Methods("GET")
- r.HandleFunc("/bird", createBirdHandler).Methods("POST")
- return r
- }
- func main() {
- fmt.Println("Starting server dickface...")
- connString := fmt.Sprintf("host=%s port=%d user=%s "+
- "password=%s dbname=%s sslmode=disable",
- host, port, user, password, dbname)
- db, err := sql.Open("postgres", connString)
- if err != nil {
- panic(err)
- }
- err = db.Ping()
- if err != nil {
- panic(err)
- }
- InitStore(&dbStore{db: db})
- r := newRouter()
- fmt.Println("Serving on port 8080")
- http.ListenAndServe(":8080", r)
- }
- func handler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "Hello World!")
- }
- package main
- import (
- "encoding/json"
- "fmt"
- "net/http"
- )
- type Bird struct {
- Species string `json:"species"`
- Description string `json:"description"`
- }
- func getBirdHandler(w http.ResponseWriter, r *http.Request) {
- birds, err := store.GetBirds()
- birdListBytes, err := json.Marshal(birds)
- if err != nil {
- fmt.Println(fmt.Errorf("Error: %v", err))
- w.WriteHeader(http.StatusInternalServerError)
- return
- }
- w.Write(birdListBytes)
- }
- func createBirdHandler(w http.ResponseWriter, r *http.Request) {
- bird := Bird{}
- err := r.ParseForm()
- if err != nil {
- fmt.Println(fmt.Errorf("Error: %v", err))
- w.WriteHeader(http.StatusInternalServerError)
- return
- }
- bird.Species = r.Form.Get("species")
- bird.Description = r.Form.Get("description")
- err = store.CreateBird(&bird)
- if err != nil {
- fmt.Println(err)
- }
- http.Redirect(w, r, "/assets/", http.StatusFound)
- }
- package main
- import (
- "database/sql"
- )
- type Store interface {
- CreateBird(bird *Bird) error
- GetBirds() ([]*Bird, error)
- }
- type dbStore struct {
- db *sql.DB
- }
- func (store *dbStore) CreateBird(bird *Bird) error {
- _, err := store.db.Query("INSERT INTO birds(species, description) VALUES ($1,$2)", bird.Species, bird.Description)
- return err
- }
- func (store *dbStore) GetBirds() ([]*Bird, error) {
- rows, err := store.db.Query("SELECT species, description from birds")
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- birds := []*Bird{}
- for rows.Next() {
- bird := &Bird{}
- if err := rows.Scan(&bird.Species, &bird.Description); err != nil {
- return nil, err
- }
- birds = append(birds, bird)
- }
- return birds, nil
- }
- var store Store
- func InitStore(s Store) {
- store = s
- }
Add Comment
Please, Sign In to add comment