2024-07-06 13:32:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-29 21:14:42 +00:00
|
|
|
"context"
|
2024-07-06 13:32:06 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-07-29 21:14:42 +00:00
|
|
|
"github.com/go-pg/pg/v10"
|
2024-07-06 13:39:34 +00:00
|
|
|
"golang_demo1/handlers"
|
2024-07-06 13:32:06 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-29 21:14:42 +00:00
|
|
|
var opt, errDb = pg.ParseURL("postgres://db_user:pass@localhost:5432/repo?sslmode=disable")
|
|
|
|
if errDb != nil {
|
|
|
|
panic("Failed to parse url: " + errDb.Error())
|
|
|
|
}
|
|
|
|
var db = pg.Connect(opt)
|
|
|
|
|
|
|
|
if db == nil {
|
|
|
|
panic("failed to connect to the database")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(db *pg.DB) {
|
|
|
|
_ = db.Close()
|
|
|
|
}(db)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if err := db.Ping(ctx); err != nil {
|
|
|
|
panic("Failed to ping: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-06 13:32:06 +00:00
|
|
|
portPtr := flag.Int("port", 8080, "port for webserver")
|
|
|
|
flag.Parse()
|
|
|
|
port := *portPtr
|
|
|
|
|
|
|
|
fmt.Printf("Server is listening on port: %d", port)
|
|
|
|
|
|
|
|
server := http.NewServeMux()
|
2024-07-06 13:39:34 +00:00
|
|
|
server.HandleFunc("/status", handlers.StatusHandler)
|
|
|
|
server.HandleFunc("/greeting", handlers.GreetingHandler)
|
2024-07-06 13:32:06 +00:00
|
|
|
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|