Skip to content

Commit 41b0f9b

Browse files
committed
pass db connection to routes
1 parent 9bc4d6b commit 41b0f9b

File tree

7 files changed

+65
-23
lines changed

7 files changed

+65
-23
lines changed

_httptest/status.http

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
###
2+
GET http://localhost:8081/api/status
3+
Accept: application/json

_httptest/v1.http

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
###
2+
GET http://localhost:8081/api/v1
3+
Accept: application/json

cmd/api/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package main
22

33
import (
4+
"database/sql"
45
"fmt"
56
"log"
67
"net/http"
78
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
89
"sandbox-go-api-sqlboiler-rest-auth/internal/routes"
10+
11+
_ "github.com/lib/pq"
912
)
1013

1114
func main() {
12-
cfg, err := config.NewEnvConfig()
15+
cfg, err := config.NewConfig()
1316
if err != nil {
1417
log.Fatalln(err)
1518
return
1619
}
20+
connStr := "host=localhost port=5433 dbname=sandbox user=postgres password=postgres sslmode=disable"
21+
db, err := sql.Open("postgres", connStr)
22+
if err != nil {
23+
log.Fatalln("cannot open the database")
24+
}
25+
defer db.Close()
1726

18-
r := routes.NewRouter()
27+
r := routes.NewRouter(db)
1928

2029
log.Printf("server is running on port %v", cfg.Port)
2130
err = http.ListenAndServe(fmt.Sprintf(":%s", cfg.Port), r)

internal/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package config
22

33
import "github.com/caarlos0/env/v6"
44

5-
type EnvConfig struct {
5+
type Config struct {
66
Port string `env:"PORT,required"`
77
}
88

9-
func NewEnvConfig() (*EnvConfig, error) {
10-
config := EnvConfig{}
9+
func NewConfig() (*Config, error) {
10+
config := Config{}
1111
if err := env.Parse(&config); err != nil {
1212
return &config, err
1313
}

internal/routes/no_versioning.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package routes
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
"net/http"
7+
8+
"github.com/go-chi/chi/v5"
9+
)
10+
11+
func noVersioningRoutes(db *sql.DB) chi.Router {
12+
r := chi.NewRouter()
13+
r.Get("/status", func(w http.ResponseWriter, r *http.Request) {
14+
_, err := w.Write([]byte("working"))
15+
if err != nil {
16+
log.Fatalln(err)
17+
}
18+
})
19+
return r
20+
}

internal/routes/routes.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package routes
22

33
import (
4-
"net/http"
4+
"database/sql"
55
"time"
66

77
"github.com/go-chi/render"
@@ -10,15 +10,7 @@ import (
1010
"github.com/go-chi/chi/v5/middleware"
1111
)
1212

13-
type Health struct {
14-
OK bool `json:"ok"`
15-
}
16-
17-
func (h *Health) Render(_ http.ResponseWriter, _ *http.Request) error {
18-
return nil
19-
}
20-
21-
func NewRouter() *chi.Mux {
13+
func NewRouter(db *sql.DB) *chi.Mux {
2214
r := chi.NewRouter()
2315
r.Use(middleware.RequestID)
2416
r.Use(middleware.RealIP)
@@ -27,14 +19,9 @@ func NewRouter() *chi.Mux {
2719
r.Use(middleware.Timeout(60 * time.Second))
2820
r.Use(render.SetContentType(render.ContentTypeJSON))
2921

30-
r.Route("/api/", func(r chi.Router) {
31-
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
32-
err := render.Render(w, r, &Health{OK: true})
33-
if err != nil {
34-
http.Error(w, http.StatusText(500), 500)
35-
return
36-
}
37-
})
22+
r.Route("/api", func(r chi.Router) {
23+
r.Mount("/", noVersioningRoutes(db))
24+
r.Mount("/v1", v1Routes(db))
3825
})
3926
return r
4027
}

internal/routes/v1.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package routes
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
"net/http"
7+
8+
"github.com/go-chi/chi/v5"
9+
)
10+
11+
func v1Routes(db *sql.DB) chi.Router {
12+
r := chi.NewRouter()
13+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
14+
_, err := w.Write([]byte("v1 index"))
15+
if err != nil {
16+
log.Fatalln(err)
17+
}
18+
})
19+
return r
20+
}

0 commit comments

Comments
 (0)