Files
x/main.go

46 lines
750 B
Go
Raw Normal View History

2024-11-24 21:36:03 -06:00
package main
import (
2024-11-24 22:05:22 -06:00
"database/sql"
2024-11-24 21:36:03 -06:00
"fmt"
"log"
"net/http"
"os"
2024-11-24 22:05:22 -06:00
_ "github.com/lib/pq"
2024-11-24 21:36:03 -06:00
)
func main() {
port := os.Getenv("PORT")
if port == "" {
2024-11-24 22:05:22 -06:00
log.Fatalf("please set PORT")
}
pgConn := os.Getenv("PGCONN")
if pgConn == "" {
log.Fatalf("please set PGCONN")
}
2024-11-25 10:21:25 -06:00
db, err := sql.Open("postgres", pgConn)
2024-11-24 22:05:22 -06:00
if err != nil {
log.Fatal(err)
2024-11-24 21:36:03 -06:00
}
2024-11-25 10:21:25 -06:00
// 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)
}
2024-11-24 21:36:03 -06:00
bind := fmt.Sprintf(":%s", port)
log.Printf("listening on %s", bind)
if err := http.ListenAndServe(bind, nil); err != nil {
log.Fatalf("listen: %s", err)
}
}