Some kind of starting point
This commit is contained in:
commit
1811f200c0
2
.env_example
Normal file
2
.env_example
Normal file
|
@ -0,0 +1,2 @@
|
|||
DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:5432/pwrpln"
|
||||
WEB_BIND_HOST=":4000"
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.env
|
11
README.md
Normal file
11
README.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
## Databaes migration
|
||||
|
||||
Done using [dbmate](https://github.com/amacneil/dbmate). Db stuff is stored in `./db`.
|
||||
|
||||
Example of running the migrations:
|
||||
|
||||
`docker run --rm -it -e DATABASE_URL="postgres://postgres:postgres@127.0.0.1:5432/pwrpln?sslmode=disable" --network=host -v "$(pwd)/db:/db" amacneil/dbmate up`
|
||||
|
||||
Example of setting up a postgres SQL server:
|
||||
|
||||
`docker run -d --name postgres --network=host -e POSTGRES_PASSWORD=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=pwrlpln postgres`
|
21
db/migrations/20201207191913_first.sql
Normal file
21
db/migrations/20201207191913_first.sql
Normal file
|
@ -0,0 +1,21 @@
|
|||
-- migrate:up
|
||||
|
||||
CREATE TABLE "users" (
|
||||
"id" uuid PRIMARY KEY,
|
||||
"created" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"username" text NOT NULL,
|
||||
"password" text NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE "usersFields" (
|
||||
"id" uuid PRIMARY KEY,
|
||||
"created" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"userId" uuid NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"value" text[] NOT NULL
|
||||
);
|
||||
ALTER TABLE "usersFields"
|
||||
ADD FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT;
|
||||
CREATE UNIQUE INDEX idx_usersfields ON "usersFields" ("userId", "name");
|
||||
|
||||
-- migrate:down
|
99
db/schema.sql
Normal file
99
db/schema.sql
Normal file
|
@ -0,0 +1,99 @@
|
|||
SET statement_timeout = 0;
|
||||
SET lock_timeout = 0;
|
||||
SET idle_in_transaction_session_timeout = 0;
|
||||
SET client_encoding = 'UTF8';
|
||||
SET standard_conforming_strings = on;
|
||||
SELECT pg_catalog.set_config('search_path', '', false);
|
||||
SET check_function_bodies = false;
|
||||
SET xmloption = content;
|
||||
SET client_min_messages = warning;
|
||||
SET row_security = off;
|
||||
|
||||
SET default_tablespace = '';
|
||||
|
||||
SET default_table_access_method = heap;
|
||||
|
||||
--
|
||||
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.schema_migrations (
|
||||
version character varying(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: users; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public.users (
|
||||
id uuid NOT NULL,
|
||||
created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
username text NOT NULL,
|
||||
password text NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: usersFields; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE public."usersFields" (
|
||||
id uuid NOT NULL,
|
||||
created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
"userId" uuid NOT NULL,
|
||||
name text NOT NULL,
|
||||
value text[] NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.schema_migrations
|
||||
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
|
||||
|
||||
|
||||
--
|
||||
-- Name: usersFields usersFields_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public."usersFields"
|
||||
ADD CONSTRAINT "usersFields_pkey" PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.users
|
||||
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: idx_usersfields; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX idx_usersfields ON public."usersFields" USING btree ("userId", name);
|
||||
|
||||
|
||||
--
|
||||
-- Name: usersFields usersFields_userId_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public."usersFields"
|
||||
ADD CONSTRAINT "usersFields_userId_fkey" FOREIGN KEY ("userId") REFERENCES public.users(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
|
||||
|
||||
|
||||
--
|
||||
-- PostgreSQL database dump complete
|
||||
--
|
||||
|
||||
|
||||
--
|
||||
-- Dbmate schema migrations
|
||||
--
|
||||
|
||||
INSERT INTO public.schema_migrations (version) VALUES
|
||||
('20201207191913');
|
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -0,0 +1,10 @@
|
|||
module gitlab.larvit.se/power-plan/api
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/gofiber/fiber/v2 v2.3.2
|
||||
github.com/google/uuid v1.1.2
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/sirupsen/logrus v1.7.0
|
||||
)
|
58
go.sum
Normal file
58
go.sum
Normal file
|
@ -0,0 +1,58 @@
|
|||
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
||||
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gofiber/fiber v1.14.6 h1:QRUPvPmr8ijQuGo1MgupHBn8E+wW0IKqiOvIZPtV70o=
|
||||
github.com/gofiber/fiber v1.14.6/go.mod h1:Yw2ekF1YDPreO9V6TMYjynu94xRxZBdaa8X5HhHsjCM=
|
||||
github.com/gofiber/fiber/v2 v2.3.2 h1:8ecrfzlfTUsboMybK6TQIfPoObmPR1hEoKU7Ni1pElg=
|
||||
github.com/gofiber/fiber/v2 v2.3.2/go.mod h1:f8BRRIMjMdRyt2qmJ/0Sea3j3rwwfufPrh9WNBRiVZ0=
|
||||
github.com/gofiber/utils v0.0.10 h1:3Mr7X7JdCUo7CWf/i5sajSaDmArEDtti8bM1JUVso2U=
|
||||
github.com/gofiber/utils v0.0.10/go.mod h1:9J5aHFUIjq0XfknT4+hdSMG6/jzfaAgCu4HEbWDeBlo=
|
||||
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
|
||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
|
||||
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
|
||||
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
|
||||
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
|
||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ=
|
||||
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
|
||||
github.com/valyala/fasthttp v1.18.0 h1:IV0DdMlatq9QO1Cr6wGJPVW1sV1Q8HvZXAIcjorylyM=
|
||||
github.com/valyala/fasthttp v1.18.0/go.mod h1:jjraHZVbKOXftJfsOYoAjaeygpj5hr8ermTRJNroD7A=
|
||||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
|
||||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
|
||||
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201210223839-7e3030f88018 h1:XKi8B/gRBuTZN1vU9gFsLMm6zVz5FSCDzm8JYACnjy8=
|
||||
golang.org/x/sys v0.0.0-20201210223839-7e3030f88018/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
35
main.go
Normal file
35
main.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/joho/godotenv"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"gitlab.larvit.se/power-plan/api/src/handlers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
// Add this line for logging filename and line number!
|
||||
// log.SetReportCaller(true)
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
// Always require application/json
|
||||
app.Use(handlers.RequireJSON)
|
||||
|
||||
app.Get("/", handlers.Hello)
|
||||
app.Get("/user/:userID", handlers.UserGet)
|
||||
app.Post("/user", handlers.UserCreate)
|
||||
|
||||
log.WithFields(log.Fields{"WEB_BIND_HOST": os.Getenv("WEB_BIND_HOST")}).Debug("Trying to start web server")
|
||||
|
||||
app.Listen(os.Getenv("WEB_BIND_HOST"))
|
||||
}
|
11
src/db/types.go
Normal file
11
src/db/types.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// User is a user in the system
|
||||
type User struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
17
src/handlers/get.go
Normal file
17
src/handlers/get.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Hello handler
|
||||
func Hello(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
}
|
||||
|
||||
// UserGet handler
|
||||
func UserGet(c *fiber.Ctx) error {
|
||||
log.WithFields(log.Fields{"userID": c.Params("userID")}).Debug("GETing user")
|
||||
return c.SendString("USER ffs")
|
||||
}
|
20
src/handlers/middlewares.go
Normal file
20
src/handlers/middlewares.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// RequireJSON is a middleware that makes sure the request content-type always is application/json (or nothing, defaulting to application/json)
|
||||
func RequireJSON(c *fiber.Ctx) error {
|
||||
c.Accepts("application/json")
|
||||
contentType := string(c.Request().Header.ContentType())
|
||||
|
||||
if contentType != "application/json" && contentType != "" {
|
||||
log.WithFields(log.Fields{"content-type": contentType}).Debug("Invalid content-type in request")
|
||||
return c.Status(415).JSON([]ResJSONError{{Error: "Invalid content-type"}})
|
||||
}
|
||||
|
||||
c.Next()
|
||||
return nil
|
||||
}
|
44
src/handlers/post.go
Normal file
44
src/handlers/post.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitlab.larvit.se/power-plan/api/src/db"
|
||||
)
|
||||
|
||||
// UserCreate creates a new user
|
||||
func UserCreate(c *fiber.Ctx) error {
|
||||
type UserInput struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
userInput := new(UserInput)
|
||||
|
||||
if err := c.BodyParser(userInput); err != nil {
|
||||
return c.Status(400).JSON([]ResJSONError{
|
||||
{Error: err.Error()},
|
||||
})
|
||||
}
|
||||
|
||||
var errors []ResJSONError
|
||||
|
||||
if userInput.Username == "" {
|
||||
errors = append(errors, ResJSONError{Error: "Can not be empty", Field: "username"})
|
||||
}
|
||||
if userInput.Password == "" {
|
||||
errors = append(errors, ResJSONError{Error: "Can not be empty", Field: "password"})
|
||||
}
|
||||
|
||||
if len(errors) != 0 {
|
||||
return c.Status(400).JSON(errors)
|
||||
}
|
||||
|
||||
createdUser := db.User{
|
||||
ID: uuid.New(),
|
||||
Username: userInput.Username,
|
||||
}
|
||||
|
||||
return c.Status(201).JSON(createdUser)
|
||||
}
|
7
src/handlers/types.go
Normal file
7
src/handlers/types.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package handlers
|
||||
|
||||
// ResJSONError is an error field that is used in JSON error responses
|
||||
type ResJSONError struct {
|
||||
Error string `json:"error"`
|
||||
Field string `json:"field,omitempty"`
|
||||
}
|
62
src/utils.go
Normal file
62
src/utils.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type malformedRequest struct {
|
||||
status int
|
||||
msg string
|
||||
}
|
||||
|
||||
func (mr *malformedRequest) Error() string {
|
||||
return mr.msg
|
||||
}
|
||||
|
||||
// ValidateJSONBody validates a JSON request body to a destionation interface
|
||||
func ValidateJSONBody(req *http.Request, dst interface{}) error {
|
||||
dec := json.NewDecoder(req.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
|
||||
err := dec.Decode(&dst)
|
||||
if err != nil {
|
||||
var syntaxError *json.SyntaxError
|
||||
var unmarshalTypeError *json.UnmarshalTypeError
|
||||
|
||||
if errors.As(err, &syntaxError) {
|
||||
msg := fmt.Sprintf("Request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
} else if errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
msg := fmt.Sprintf("Request body contains badly-formed JSON")
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
} else if errors.As(err, &unmarshalTypeError) {
|
||||
msg := fmt.Sprintf("Request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset)
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
} else if strings.HasPrefix(err.Error(), "json: unknown field ") {
|
||||
fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ")
|
||||
msg := fmt.Sprintf("Request body contains unknown field %s", fieldName)
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
} else if errors.Is(err, io.EOF) {
|
||||
msg := "Request body must not be empty"
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
} else if err.Error() == "http: request body too large" {
|
||||
msg := "Request body must not be larger than 1MB"
|
||||
return &malformedRequest{status: http.StatusRequestEntityTooLarge, msg: msg}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = dec.Decode(&struct{}{})
|
||||
if err != io.EOF {
|
||||
msg := "Request body must only contain a single JSON object"
|
||||
return &malformedRequest{status: http.StatusBadRequest, msg: msg}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user