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