Initial Go server with PostgreSQL and static file serving

This commit is contained in:
Ian Gulliver
2026-02-14 20:29:42 -08:00
commit b33cf94151
5 changed files with 68 additions and 0 deletions

42
main.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
_ "github.com/lib/pq"
)
func main() {
dsn := os.Getenv("PGCONN")
if dsn == "" {
log.Fatal("PGCONN environment variable is required")
}
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
log.Fatalf("failed to connect to database: %v", err)
}
log.Println("connected to database")
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if err := db.Ping(); err != nil {
http.Error(w, "db unhealthy", http.StatusServiceUnavailable)
return
}
fmt.Fprintln(w, "ok")
})
log.Println("listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}