From 1811f200c023c45546322f934da467a73f8588da Mon Sep 17 00:00:00 2001 From: Lilleman Date: Tue, 29 Dec 2020 13:46:58 +0100 Subject: [PATCH] Some kind of starting point --- .env_example | 2 + .gitignore | 1 + README.md | 11 +++ db/migrations/20201207191913_first.sql | 21 ++++++ db/schema.sql | 99 ++++++++++++++++++++++++++ go.mod | 10 +++ go.sum | 58 +++++++++++++++ main.go | 35 +++++++++ src/db/types.go | 11 +++ src/handlers/get.go | 17 +++++ src/handlers/middlewares.go | 20 ++++++ src/handlers/post.go | 44 ++++++++++++ src/handlers/types.go | 7 ++ src/utils.go | 62 ++++++++++++++++ 14 files changed, 398 insertions(+) create mode 100644 .env_example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 db/migrations/20201207191913_first.sql create mode 100644 db/schema.sql create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 src/db/types.go create mode 100644 src/handlers/get.go create mode 100644 src/handlers/middlewares.go create mode 100644 src/handlers/post.go create mode 100644 src/handlers/types.go create mode 100644 src/utils.go diff --git a/.env_example b/.env_example new file mode 100644 index 0000000..14c4c52 --- /dev/null +++ b/.env_example @@ -0,0 +1,2 @@ +DATABASE_URL="postgresql://postgres:postgres@127.0.0.1:5432/pwrpln" +WEB_BIND_HOST=":4000" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..db09860 --- /dev/null +++ b/README.md @@ -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` \ No newline at end of file diff --git a/db/migrations/20201207191913_first.sql b/db/migrations/20201207191913_first.sql new file mode 100644 index 0000000..5181e25 --- /dev/null +++ b/db/migrations/20201207191913_first.sql @@ -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 \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..d6d8c6a --- /dev/null +++ b/db/schema.sql @@ -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'); diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ab0b3b --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..57424d7 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b0a42c0 --- /dev/null +++ b/main.go @@ -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")) +} diff --git a/src/db/types.go b/src/db/types.go new file mode 100644 index 0000000..2939245 --- /dev/null +++ b/src/db/types.go @@ -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"` +} diff --git a/src/handlers/get.go b/src/handlers/get.go new file mode 100644 index 0000000..d2a8f3a --- /dev/null +++ b/src/handlers/get.go @@ -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") +} diff --git a/src/handlers/middlewares.go b/src/handlers/middlewares.go new file mode 100644 index 0000000..c58e308 --- /dev/null +++ b/src/handlers/middlewares.go @@ -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 +} diff --git a/src/handlers/post.go b/src/handlers/post.go new file mode 100644 index 0000000..a11f1e7 --- /dev/null +++ b/src/handlers/post.go @@ -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) +} diff --git a/src/handlers/types.go b/src/handlers/types.go new file mode 100644 index 0000000..34c24e1 --- /dev/null +++ b/src/handlers/types.go @@ -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"` +} diff --git a/src/utils.go b/src/utils.go new file mode 100644 index 0000000..382bdc8 --- /dev/null +++ b/src/utils.go @@ -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 +}