Guest User

Untitled

a guest
Feb 14th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "net/http"
  7.  
  8. "github.com/gorilla/mux"
  9. _ "github.com/lib/pq"
  10. )
  11.  
  12. const (
  13. host = "localhost"
  14. port = 5432
  15. user = "postgres"
  16. password = "0102"
  17. dbname = "bird_encyclopaedia"
  18. )
  19.  
  20. func newRouter() *mux.Router {
  21. r := mux.NewRouter()
  22. r.HandleFunc("/hello", handler).Methods("GET")
  23.  
  24.  
  25. staticFileDirectory := http.Dir("./assets/")
  26. staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
  27. r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")
  28.  
  29. r.HandleFunc("/bird", getBirdHandler).Methods("GET")
  30. r.HandleFunc("/bird", createBirdHandler).Methods("POST")
  31. return r
  32. }
  33.  
  34. func main() {
  35. fmt.Println("Starting server dickface...")
  36. connString := fmt.Sprintf("host=%s port=%d user=%s "+
  37. "password=%s dbname=%s sslmode=disable",
  38. host, port, user, password, dbname)
  39. db, err := sql.Open("postgres", connString)
  40.  
  41. if err != nil {
  42. panic(err)
  43. }
  44. err = db.Ping()
  45.  
  46. if err != nil {
  47. panic(err)
  48. }
  49.  
  50. InitStore(&dbStore{db: db})
  51.  
  52. r := newRouter()
  53. fmt.Println("Serving on port 8080")
  54. http.ListenAndServe(":8080", r)
  55. }
  56.  
  57. func handler(w http.ResponseWriter, r *http.Request) {
  58. fmt.Fprintf(w, "Hello World!")
  59. }
  60.  
  61. package main
  62.  
  63. import (
  64. "encoding/json"
  65. "fmt"
  66. "net/http"
  67. )
  68.  
  69. type Bird struct {
  70. Species string `json:"species"`
  71. Description string `json:"description"`
  72. }
  73.  
  74. func getBirdHandler(w http.ResponseWriter, r *http.Request) {
  75.  
  76. birds, err := store.GetBirds()
  77.  
  78. birdListBytes, err := json.Marshal(birds)
  79.  
  80. if err != nil {
  81. fmt.Println(fmt.Errorf("Error: %v", err))
  82. w.WriteHeader(http.StatusInternalServerError)
  83. return
  84. }
  85. w.Write(birdListBytes)
  86. }
  87.  
  88. func createBirdHandler(w http.ResponseWriter, r *http.Request) {
  89. bird := Bird{}
  90.  
  91. err := r.ParseForm()
  92.  
  93. if err != nil {
  94. fmt.Println(fmt.Errorf("Error: %v", err))
  95. w.WriteHeader(http.StatusInternalServerError)
  96. return
  97. }
  98.  
  99. bird.Species = r.Form.Get("species")
  100. bird.Description = r.Form.Get("description")
  101.  
  102.  
  103. err = store.CreateBird(&bird)
  104. if err != nil {
  105. fmt.Println(err)
  106. }
  107.  
  108. http.Redirect(w, r, "/assets/", http.StatusFound)
  109. }
  110.  
  111. package main
  112.  
  113. import (
  114. "database/sql"
  115. )
  116.  
  117. type Store interface {
  118. CreateBird(bird *Bird) error
  119. GetBirds() ([]*Bird, error)
  120. }
  121.  
  122. type dbStore struct {
  123. db *sql.DB
  124. }
  125.  
  126. func (store *dbStore) CreateBird(bird *Bird) error {
  127. _, err := store.db.Query("INSERT INTO birds(species, description) VALUES ($1,$2)", bird.Species, bird.Description)
  128. return err
  129. }
  130.  
  131. func (store *dbStore) GetBirds() ([]*Bird, error) {
  132. rows, err := store.db.Query("SELECT species, description from birds")
  133. if err != nil {
  134. return nil, err
  135. }
  136. defer rows.Close()
  137.  
  138. birds := []*Bird{}
  139. for rows.Next() {
  140. bird := &Bird{}
  141. if err := rows.Scan(&bird.Species, &bird.Description); err != nil {
  142. return nil, err
  143. }
  144. birds = append(birds, bird)
  145. }
  146. return birds, nil
  147. }
  148.  
  149. var store Store
  150.  
  151. func InitStore(s Store) {
  152. store = s
  153. }
Add Comment
Please, Sign In to add comment