Upsert to database
This commit is contained in:
18
main.go
18
main.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
@@ -15,13 +14,14 @@ import (
|
|||||||
type ShortLinks struct {
|
type ShortLinks struct {
|
||||||
tmpl *template.Template
|
tmpl *template.Template
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
Short string `json:"short"`
|
Short string `json:"short"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShortLinks() (*ShortLinks, error) {
|
func NewShortLinks(db *sql.DB) (*ShortLinks, error) {
|
||||||
tmpl := template.New("index.html")
|
tmpl := template.New("index.html")
|
||||||
|
|
||||||
tmpl, err := tmpl.ParseFiles("static/index.html")
|
tmpl, err := tmpl.ParseFiles("static/index.html")
|
||||||
@@ -32,6 +32,7 @@ func NewShortLinks() (*ShortLinks, error) {
|
|||||||
sl := &ShortLinks{
|
sl := &ShortLinks{
|
||||||
tmpl: tmpl,
|
tmpl: tmpl,
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
||||||
sl.mux.HandleFunc("GET /{$}", sl.serveRoot)
|
sl.mux.HandleFunc("GET /{$}", sl.serveRoot)
|
||||||
@@ -76,7 +77,16 @@ func (sl *ShortLinks) serveSet(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
_, err = sl.db.Exec(`
|
||||||
|
INSERT INTO links (short, long)
|
||||||
|
VALUES ($1, $2)
|
||||||
|
ON CONFLICT (short)
|
||||||
|
DO UPDATE SET long = $2
|
||||||
|
`, short, long)
|
||||||
|
if err != nil {
|
||||||
|
sendError(w, http.StatusInternalServerError, "upsert: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
sendJSON(w, response{
|
sendJSON(w, response{
|
||||||
Short: short,
|
Short: short,
|
||||||
@@ -108,7 +118,7 @@ CREATE TABLE IF NOT EXISTS links (
|
|||||||
log.Fatalf("Failed to create table: %v", err)
|
log.Fatalf("Failed to create table: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sl, err := NewShortLinks()
|
sl, err := NewShortLinks(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create shortlinks: %v", err)
|
log.Fatalf("Failed to create shortlinks: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user