This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// middleware/getAccount.js | |
module.exports = async (req, res, next) => { | |
try { | |
const account = await req.db.Account.findByPk(req.body.account_id); | |
res.locals.account = account; | |
next(); | |
} catch (err) { | |
next(err); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app.js | |
app.use((err, req, res, next) => { | |
console.error(err); | |
res.status(err.status || 500).json({ | |
message: err.message, | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// middleware/validateBodyParams.js | |
const validId = require('./validId'); | |
module.exports = (req, res, next) => { | |
const { | |
account_id: accId, | |
currency, | |
} = req.body; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const validId = require('./validId'); | |
const router = express.Router(); | |
router.post('/some-route', async (req, res, next) => { | |
if (!req.body.account_id) { | |
return res.status(412).json({ | |
code: 412, | |
message: 'missing body params', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cmd/api/handlers/getuser/getuser.go | |
package getuser | |
import ( | |
"database/sql" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"net/http" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cmd/api/handlers/getuser/validarequest.go | |
package getuser | |
import ( | |
"context" | |
"fmt" | |
"net/http" | |
"strconv" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pkg/middleware/logging.go | |
package middleware | |
import ( | |
"net/http" | |
"github.com/boilerplate/pkg/logger" | |
"github.com/julienschmidt/httprouter" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pkg/middleware/middleware.go | |
package middleware | |
import ( | |
"github.com/julienschmidt/httprouter" | |
) | |
type Middleware func(httprouter.Handle) httprouter.Handle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
go run cmd/dbmigrate/main.go | |
go run cmd/dbmigrate/main.go -dbname=boilerplatetest | |
GO111MODULE=off go get github.com/githubnemo/CompileDaemon | |
CompileDaemon --build="go build -o main cmd/api/main.go" --command=./main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cmd/dbmigrate/main.go | |
package main | |
import ( | |
"log" | |
"github.com/boilerplate/pkg/config" | |
"github.com/golang-migrate/migrate/v4" | |
_ "github.com/golang-migrate/migrate/v4/database/postgres" |
NewerOlder