chore: cleanup main
This commit is contained in:
parent
1d09d445af
commit
30d3db9d5e
1 changed files with 13 additions and 6 deletions
19
main.go
19
main.go
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -30,11 +32,11 @@ var rapidocHtml = `
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func run() error {
|
||||||
// This is rude but at least we can configure the database URL securely.
|
// This is rude but at least we can configure the database URL securely.
|
||||||
psqlUrl := os.Getenv("DATABASE_URL")
|
psqlUrl := os.Getenv("DATABASE_URL")
|
||||||
if psqlUrl == "" {
|
if psqlUrl == "" {
|
||||||
log.Fatalln("DATABASE_URL is not configured.")
|
return errors.New("DATABASE_URL is not configured.")
|
||||||
}
|
}
|
||||||
|
|
||||||
serverUrl := os.Getenv("SERVER_URL")
|
serverUrl := os.Getenv("SERVER_URL")
|
||||||
|
@ -46,15 +48,16 @@ func main() {
|
||||||
|
|
||||||
psql, err := pgx.Connect(context.Background(), psqlUrl)
|
psql, err := pgx.Connect(context.Background(), psqlUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("cannot connect to database: %v", err)
|
return fmt.Errorf("cannot connect to database: %v", err)
|
||||||
}
|
}
|
||||||
|
defer psql.Close(context.Background())
|
||||||
|
|
||||||
queries := db.New(psql)
|
queries := db.New(psql)
|
||||||
handler := api.NewHandler(serverUrl, queries)
|
handler := api.NewHandler(serverUrl, queries)
|
||||||
|
|
||||||
srv, err := oas.NewServer(handler)
|
srv, err := oas.NewServer(handler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed te create OpenAPI server: %v", err)
|
return fmt.Errorf("failed te create OpenAPI server: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
@ -62,8 +65,12 @@ func main() {
|
||||||
mux.Handle("/docs", serveContent([]byte(rapidocHtml), "text/html"))
|
mux.Handle("/docs", serveContent([]byte(rapidocHtml), "text/html"))
|
||||||
mux.Handle("/docs/openapi.yaml", serveContent(openapiFile, "application/yaml"))
|
mux.Handle("/docs/openapi.yaml", serveContent(openapiFile, "application/yaml"))
|
||||||
log.Println("Listening on :8080")
|
log.Println("Listening on :8080")
|
||||||
if err = http.ListenAndServe(":8080", mux); err != nil {
|
return http.ListenAndServe(":8080", mux)
|
||||||
log.Fatal(err)
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := run(); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue