feat(server): main.go - listen on :8310, lib/pq open, ping db
This commit is contained in:
parent
51d1419aad
commit
6d98a3a869
1 changed files with 35 additions and 0 deletions
35
main.go
Normal file
35
main.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dsn := os.Getenv("POSTS_DSN")
|
||||
if dsn == "" {
|
||||
dsn = "postgres://posts:posts@127.0.0.1:5432/posts?sslmode=disable"
|
||||
}
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("open db: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
if err := db.Ping(); err != nil {
|
||||
log.Fatalf("ping db: %v", err)
|
||||
}
|
||||
|
||||
srv := NewServer(db)
|
||||
addr := os.Getenv("ADDR")
|
||||
if addr == "" {
|
||||
addr = ":8310" // see go-htmx-web-dev port list: 8310 is next free
|
||||
}
|
||||
log.Printf("posts listening on %s (dsn=%s)", addr, dsn)
|
||||
if err := http.ListenAndServe(addr, srv.Routes()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue