Comment out broken example so go test ./... can run

pull/483/head
Jack Christensen 2019-04-13 17:13:48 -05:00
parent 5cc4796c96
commit 7c1ff89703
1 changed files with 104 additions and 103 deletions

View File

@ -1,122 +1,123 @@
// TODO - Fix after v4 churn ends
package main
import (
"io/ioutil"
"net/http"
"os"
// import (
// "io/ioutil"
// "net/http"
// "os"
"github.com/jackc/pgx"
"github.com/jackc/pgx/log/log15adapter"
log "gopkg.in/inconshreveable/log15.v2"
)
// "github.com/jackc/pgx"
// "github.com/jackc/pgx/log/log15adapter"
// log "gopkg.in/inconshreveable/log15.v2"
// )
var pool *pgx.ConnPool
// var pool *pgx.ConnPool
// afterConnect creates the prepared statements that this application uses
func afterConnect(conn *pgx.Conn) (err error) {
_, err = conn.Prepare("getUrl", `
select url from shortened_urls where id=$1
`)
if err != nil {
return
}
// // afterConnect creates the prepared statements that this application uses
// func afterConnect(conn *pgx.Conn) (err error) {
// _, err = conn.Prepare("getUrl", `
// select url from shortened_urls where id=$1
// `)
// if err != nil {
// return
// }
_, err = conn.Prepare("deleteUrl", `
delete from shortened_urls where id=$1
`)
if err != nil {
return
}
// _, err = conn.Prepare("deleteUrl", `
// delete from shortened_urls where id=$1
// `)
// if err != nil {
// return
// }
_, err = conn.Prepare("putUrl", `
insert into shortened_urls(id, url) values ($1, $2)
on conflict (id) do update set url=excluded.url
`)
return
}
// _, err = conn.Prepare("putUrl", `
// insert into shortened_urls(id, url) values ($1, $2)
// on conflict (id) do update set url=excluded.url
// `)
// return
// }
func getUrlHandler(w http.ResponseWriter, req *http.Request) {
var url string
err := pool.QueryRow("getUrl", req.URL.Path).Scan(&url)
switch err {
case nil:
http.Redirect(w, req, url, http.StatusSeeOther)
case pgx.ErrNoRows:
http.NotFound(w, req)
default:
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// func getUrlHandler(w http.ResponseWriter, req *http.Request) {
// var url string
// err := pool.QueryRow("getUrl", req.URL.Path).Scan(&url)
// switch err {
// case nil:
// http.Redirect(w, req, url, http.StatusSeeOther)
// case pgx.ErrNoRows:
// http.NotFound(w, req)
// default:
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// }
// }
func putUrlHandler(w http.ResponseWriter, req *http.Request) {
id := req.URL.Path
var url string
if body, err := ioutil.ReadAll(req.Body); err == nil {
url = string(body)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
// func putUrlHandler(w http.ResponseWriter, req *http.Request) {
// id := req.URL.Path
// var url string
// if body, err := ioutil.ReadAll(req.Body); err == nil {
// url = string(body)
// } else {
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// return
// }
if _, err := pool.Exec("putUrl", id, url); err == nil {
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// if _, err := pool.Exec("putUrl", id, url); err == nil {
// w.WriteHeader(http.StatusOK)
// } else {
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// }
// }
func deleteUrlHandler(w http.ResponseWriter, req *http.Request) {
if _, err := pool.Exec("deleteUrl", req.URL.Path); err == nil {
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// func deleteUrlHandler(w http.ResponseWriter, req *http.Request) {
// if _, err := pool.Exec("deleteUrl", req.URL.Path); err == nil {
// w.WriteHeader(http.StatusOK)
// } else {
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// }
// }
func urlHandler(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
getUrlHandler(w, req)
// func urlHandler(w http.ResponseWriter, req *http.Request) {
// switch req.Method {
// case "GET":
// getUrlHandler(w, req)
case "PUT":
putUrlHandler(w, req)
// case "PUT":
// putUrlHandler(w, req)
case "DELETE":
deleteUrlHandler(w, req)
// case "DELETE":
// deleteUrlHandler(w, req)
default:
w.Header().Add("Allow", "GET, PUT, DELETE")
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
// default:
// w.Header().Add("Allow", "GET, PUT, DELETE")
// w.WriteHeader(http.StatusMethodNotAllowed)
// }
// }
func main() {
logger := log15adapter.NewLogger(log.New("module", "pgx"))
// func main() {
// logger := log15adapter.NewLogger(log.New("module", "pgx"))
var err error
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: "127.0.0.1",
User: "jack",
Password: "jack",
Database: "url_shortener",
Logger: logger,
},
MaxConnections: 5,
AfterConnect: afterConnect,
}
pool, err = pgx.NewConnPool(connPoolConfig)
if err != nil {
log.Crit("Unable to create connection pool", "error", err)
os.Exit(1)
}
// var err error
// connPoolConfig := pgx.ConnPoolConfig{
// ConnConfig: pgx.ConnConfig{
// Host: "127.0.0.1",
// User: "jack",
// Password: "jack",
// Database: "url_shortener",
// Logger: logger,
// },
// MaxConnections: 5,
// AfterConnect: afterConnect,
// }
// pool, err = pgx.NewConnPool(connPoolConfig)
// if err != nil {
// log.Crit("Unable to create connection pool", "error", err)
// os.Exit(1)
// }
http.HandleFunc("/", urlHandler)
// http.HandleFunc("/", urlHandler)
log.Info("Starting URL shortener on localhost:8080")
err = http.ListenAndServe("localhost:8080", nil)
if err != nil {
log.Crit("Unable to start web server", "error", err)
os.Exit(1)
}
}
// log.Info("Starting URL shortener on localhost:8080")
// err = http.ListenAndServe("localhost:8080", nil)
// if err != nil {
// log.Crit("Unable to start web server", "error", err)
// os.Exit(1)
// }
// }