2
0

Update url_shortener example

This commit is contained in:
Jack Christensen
2019-05-17 14:13:03 -05:00
parent 54c520695f
commit 6d23b58b01
4 changed files with 130 additions and 115 deletions
+13 -5
View File
@@ -6,20 +6,28 @@ This is a sample REST URL shortener service implemented using pgx as the connect
Create a PostgreSQL database and run structure.sql into it to create the necessary data schema.
Edit connectionOptions in main.go with the location and credentials for your database.
Configure the database connection with `DATABASE_URL` or standard PostgreSQL (`PG*`) environment variables or
Run main.go:
go run main.go
```
go run main.go
```
## Create or Update a Shortened URL
curl -X PUT -d 'http://www.google.com' http://localhost:8080/google
```
curl -X PUT -d 'http://www.google.com' http://localhost:8080/google
```
## Get a Shortened URL
curl http://localhost:8080/google
```
curl http://localhost:8080/google
```
## Delete a Shortened URL
curl -X DELETE http://localhost:8080/google
```
curl -X DELETE http://localhost:8080/google
```
+103 -109
View File
@@ -1,127 +1,121 @@
// TODO - Fix after v4 churn ends
package main
func main() {
import (
"context"
"io/ioutil"
"net/http"
"os"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/log15adapter"
"github.com/jackc/pgx/v4/pool"
log "gopkg.in/inconshreveable/log15.v2"
)
var db *pool.Pool
// afterConnect creates the prepared statements that this application uses
func afterConnect(ctx context.Context, conn *pgx.Conn) (err error) {
_, err = conn.Prepare(ctx, "getUrl", `
select url from shortened_urls where id=$1
`)
if err != nil {
return
}
_, err = conn.Prepare(ctx, "deleteUrl", `
delete from shortened_urls where id=$1
`)
if err != nil {
return
}
_, err = conn.Prepare(ctx, "putUrl", `
insert into shortened_urls(id, url) values ($1, $2)
on conflict (id) do update set url=excluded.url
`)
return
}
// import (
// "io/ioutil"
// "net/http"
// "os"
func getUrlHandler(w http.ResponseWriter, req *http.Request) {
var url string
err := db.QueryRow(context.Background(), "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)
}
}
// "github.com/jackc/pgx/v4"
// "github.com/jackc/pgx/v4/log/log15adapter"
// log "gopkg.in/inconshreveable/log15.v2"
// )
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
}
// var pool *pgx.ConnPool
if _, err := db.Exec(context.Background(), "putUrl", id, url); err == nil {
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// // 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
// }
func deleteUrlHandler(w http.ResponseWriter, req *http.Request) {
if _, err := db.Exec(context.Background(), "deleteUrl", req.URL.Path); err == nil {
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
}
// _, err = conn.Prepare("deleteUrl", `
// delete from shortened_urls where id=$1
// `)
// if err != nil {
// return
// }
func urlHandler(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
getUrlHandler(w, req)
// _, err = conn.Prepare("putUrl", `
// insert into shortened_urls(id, url) values ($1, $2)
// on conflict (id) do update set url=excluded.url
// `)
// return
// }
case "PUT":
putUrlHandler(w, req)
// 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)
// }
// }
case "DELETE":
deleteUrlHandler(w, req)
// 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
// }
default:
w.Header().Add("Allow", "GET, PUT, DELETE")
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
// if _, err := pool.Exec("putUrl", id, url); err == nil {
// w.WriteHeader(http.StatusOK)
// } else {
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// }
// }
func main() {
logger := log15adapter.NewLogger(log.New("module", "pgx"))
// 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)
// }
// }
poolConfig, err := pool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
log.Crit("Unable to parse DATABASE_URL", "error", err)
os.Exit(1)
}
// func urlHandler(w http.ResponseWriter, req *http.Request) {
// switch req.Method {
// case "GET":
// getUrlHandler(w, req)
poolConfig.AfterConnect = afterConnect
poolConfig.ConnConfig.Logger = logger
// case "PUT":
// putUrlHandler(w, req)
db, err = pool.ConnectConfig(context.Background(), poolConfig)
if err != nil {
log.Crit("Unable to create connection pool", "error", err)
os.Exit(1)
}
// case "DELETE":
// deleteUrlHandler(w, req)
http.HandleFunc("/", urlHandler)
// default:
// w.Header().Add("Allow", "GET, PUT, DELETE")
// w.WriteHeader(http.StatusMethodNotAllowed)
// }
// }
// 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)
// }
// 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)
}
}