mirror of
https://forgejo.merr.is/annika/isl-api.git
synced 2025-12-11 11:10:59 -05:00
Made Changes To Use a pgx Connection Pool
This commit is contained in:
parent
c327b571f6
commit
c1231d487a
5 changed files with 45 additions and 21 deletions
|
|
@ -11,6 +11,14 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
// TODO: Figure out a better place to put this.
|
||||
func MiddleCORS(next httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
next(w, r, ps)
|
||||
}
|
||||
}
|
||||
|
||||
type PowerItemController struct {
|
||||
powerItemService *Services.PowerItemService
|
||||
}
|
||||
|
|
@ -24,15 +32,15 @@ func NewPowerItemController(router *httprouter.Router, powerItemService *Service
|
|||
}
|
||||
|
||||
func (p *PowerItemController) setPowerItemEndpoints(router *httprouter.Router, prefix string) {
|
||||
router.HandlerFunc("GET", fmt.Sprintf("%v", prefix), p.getAll)
|
||||
router.HandlerFunc("GET", fmt.Sprintf("%v/asMap", prefix), p.getAllAsMap)
|
||||
router.HandlerFunc("GET", fmt.Sprintf("%v/byType/:type", prefix), p.getAllByType)
|
||||
router.HandlerFunc("GET", fmt.Sprintf("%v/byType/:type/asMap", prefix), p.getAllByTypeAsMap)
|
||||
router.HandlerFunc("POST", fmt.Sprintf("%v", prefix), p.add)
|
||||
router.HandlerFunc("POST", fmt.Sprintf("%v/multiple", prefix), p.addMultiple)
|
||||
router.GET(fmt.Sprintf("%v", prefix), MiddleCORS(p.getAll))
|
||||
router.GET(fmt.Sprintf("%v/asMap", prefix), MiddleCORS(p.getAllAsMap))
|
||||
router.GET(fmt.Sprintf("%v/byType/:type", prefix), MiddleCORS(p.getAllByType))
|
||||
router.GET(fmt.Sprintf("%v/byType/:type/asMap", prefix), MiddleCORS(p.getAllByTypeAsMap))
|
||||
router.POST(fmt.Sprintf("%v", prefix), MiddleCORS(p.add))
|
||||
router.POST(fmt.Sprintf("%v/multiple", prefix), MiddleCORS(p.addMultiple))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) add(w http.ResponseWriter, r *http.Request) {
|
||||
func (p *PowerItemController) add(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
var newItem Types.PowerItem
|
||||
err := json.NewDecoder(r.Body).Decode(&newItem)
|
||||
if err != nil {
|
||||
|
|
@ -56,7 +64,7 @@ func (p *PowerItemController) add(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprint(w, string(returnValue))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) addMultiple(w http.ResponseWriter, r *http.Request) {
|
||||
func (p *PowerItemController) addMultiple(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
var itemType int32 = 3
|
||||
var newItems map[string]Types.PowerItem
|
||||
err := json.NewDecoder(r.Body).Decode(&newItems)
|
||||
|
|
@ -84,7 +92,7 @@ func (p *PowerItemController) addMultiple(w http.ResponseWriter, r *http.Request
|
|||
fmt.Fprint(w, string(returnValue))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) getAll(w http.ResponseWriter, r *http.Request) {
|
||||
func (p *PowerItemController) getAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
result, err := p.powerItemService.GetAll()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
@ -101,7 +109,7 @@ func (p *PowerItemController) getAll(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) getAllAsMap(w http.ResponseWriter, r *http.Request) {
|
||||
func (p *PowerItemController) getAllAsMap(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
items, err := p.powerItemService.GetAll()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
@ -124,9 +132,8 @@ func (p *PowerItemController) getAllAsMap(w http.ResponseWriter, r *http.Request
|
|||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) getAllByType(w http.ResponseWriter, r *http.Request) {
|
||||
params := httprouter.ParamsFromContext(r.Context())
|
||||
typeCode, err := strconv.Atoi(params.ByName("type"))
|
||||
func (p *PowerItemController) getAllByType(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
typeCode, err := strconv.Atoi(ps.ByName("type"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -148,9 +155,8 @@ func (p *PowerItemController) getAllByType(w http.ResponseWriter, r *http.Reques
|
|||
fmt.Fprint(w, string(data))
|
||||
}
|
||||
|
||||
func (p *PowerItemController) getAllByTypeAsMap(w http.ResponseWriter, r *http.Request) {
|
||||
params := httprouter.ParamsFromContext(r.Context())
|
||||
typeCode, err := strconv.Atoi(params.ByName("type"))
|
||||
func (p *PowerItemController) getAllByTypeAsMap(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
typeCode, err := strconv.Atoi(ps.ByName("type"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@ version: '3.1'
|
|||
|
||||
services:
|
||||
api:
|
||||
image: moosetheory/isl-api:0.1.1
|
||||
image: forgejo.merr.is/annika/isl-api:latest
|
||||
restart: always
|
||||
environment:
|
||||
- ISL_API_DB_CONNECTION_STRING=postgres://isl:development@db:5432/isl
|
||||
- ISL_API_HTTP_PORT=3000
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 3080:3000
|
||||
db:
|
||||
image: postgres
|
||||
restart: always
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -17,6 +17,7 @@ require (
|
|||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/puddle v1.3.0 // indirect
|
||||
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
|
|
|
|||
1
go.sum
1
go.sum
|
|
@ -70,6 +70,7 @@ github.com/jackc/pgx/v4 v4.18.1/go.mod h1:FydWkUyadDmdNH/mHnGob881GawxeEm7TcMCzk
|
|||
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
|
||||
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
|
|
|
|||
22
main.go
22
main.go
|
|
@ -9,7 +9,7 @@ import (
|
|||
"forgejo.merr.is/annika/isl-api/Controllers"
|
||||
"forgejo.merr.is/annika/isl-api/Services"
|
||||
"forgejo.merr.is/annika/isl-api/sql/powerItem"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -26,6 +26,7 @@ func main() {
|
|||
|
||||
deps.router.HandlerFunc("GET", "/", index)
|
||||
|
||||
fmt.Printf("Preparing to listen on `:%v`\n", conf.HttpPort)
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%v", conf.HttpPort), deps.router)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -49,7 +50,7 @@ func setupConfig() {
|
|||
|
||||
type dependencies struct {
|
||||
router *httprouter.Router
|
||||
postgresConnection *pgx.Conn
|
||||
postgresConnection *pgxpool.Pool
|
||||
context context.Context
|
||||
powerItemQuerier *powerItem.DBQuerier
|
||||
powerItemService *Services.PowerItemService
|
||||
|
|
@ -60,8 +61,16 @@ func (d *dependencies) initializeDependencies() error {
|
|||
var err error
|
||||
|
||||
d.router = httprouter.New()
|
||||
d.router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Access-Control-Request-Method") != "" {
|
||||
header := w.Header()
|
||||
header.Set("Access-Control-Allow-Methods", header.Get("Allow"))
|
||||
header.Set("Access-Control-Allow-Headers", "Cache-Control, Expires, Pragma")
|
||||
header.Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
})
|
||||
d.context = context.Background()
|
||||
d.postgresConnection, err = pgx.Connect(d.context, conf.ConnectionString)
|
||||
d.postgresConnection, err = pgxpool.Connect(d.context, conf.ConnectionString)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -71,6 +80,13 @@ func (d *dependencies) initializeDependencies() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func MiddleCORS(next httprouter.Handle) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
next(w, r, ps)
|
||||
}
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, "Index")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue