46 lines
750 B
Go
46 lines
750 B
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
func main() {
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
log.Fatalf("please set PORT")
|
|
}
|
|
|
|
pgConn := os.Getenv("PGCONN")
|
|
if pgConn == "" {
|
|
log.Fatalf("please set PGCONN")
|
|
}
|
|
|
|
db, err := sql.Open("postgres", pgConn)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Execute the SQL statement
|
|
_, err = db.Exec(`
|
|
CREATE TABLE IF NOT EXISTS links (
|
|
short VARCHAR(100) PRIMARY KEY,
|
|
long VARCHAR(255) NOT NULL
|
|
);`)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create table: %v", err)
|
|
}
|
|
|
|
bind := fmt.Sprintf(":%s", port)
|
|
log.Printf("listening on %s", bind)
|
|
|
|
if err := http.ListenAndServe(bind, nil); err != nil {
|
|
log.Fatalf("listen: %s", err)
|
|
}
|
|
}
|