Skip to content

pkutschera/go-rest-footballer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A REST based microservice with GO

This repo contains a simple example written in Go.

Entity–relationship model

Entities

Note: This example uses a postgres database

Setup

Due to the fact that one of the dependencies need a Go version with modules, a module for theis service has to be created:

> go mod init github.com/pkutschera/go-rest-footballer

Installation

The necessary dependencies (handler framework, postgres) have to be installed using

> go get github.com/gorilla/mux  
> go get -u github.com/fergusstrange/embedded-postgres
> go get -u github.com/lib/pq

DB Setup

In postgres two tables have been created using pgadmin resulting in the following schema definition:

CREATE TABLE public.club
(
    id integer NOT NULL DEFAULT nextval('club_id_seq'::regclass),
    name "char"[] NOT NULL,
    founded "char"[] NOT NULL,
    CONSTRAINT club_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE public.club
    OWNER to postgres;

CREATE TABLE public.footballer
(
    id integer NOT NULL DEFAULT nextval('footballer_id_seq'::regclass),
    firstname "char"[] NOT NULL,
    lastname "char"[] NOT NULL,
    "position" "char"[] NOT NULL,
    clubid integer NOT NULL DEFAULT nextval('footballer_club_seq'::regclass),
    CONSTRAINT footballer_pkey PRIMARY KEY (id),
    CONSTRAINT "clubid constraint" FOREIGN KEY (clubid)
        REFERENCES public.club (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        NOT VALID
)

TABLESPACE pg_default;

ALTER TABLE public.footballer
    OWNER to postgres;

CREATE INDEX "fki_clubid constraint"
    ON public.footballer USING btree
    (clubid ASC NULLS LAST)
    TABLESPACE pg_default;

##Strating the REST service The service should start on port 8090.

> go run .

Call the REST API

Postman can be used to create the clinet requests.

E.g. to create a new Club you need to do a POST to http://localhost:8090/clubs with a payload like this:

{
  "name": "Real Madrid",
  "founded": 1902
}

To GET a all Clubs call http://localhost:8090/clubs, to get a certain Club by its ID call http://localhost:8090/clubs/1

To create a Footballer and associate it to a previously created Club POST this to http://localhost:8090/footballers:

{
  "footballer": {
    "firstName": "Toni",
    "lastName": "Kroos",
    "position": "Midfield"
  },
  "clubId":1
}

To GET all Footballers you have to call http://localhost:8090/footballers.

To GET all Footballers for a certain position please add the request parameter position (e.g. http://localhost:8090/footballers?position=Midfield)

Next steps

  • ORM
  • ...

About

A rest based microservice in golang using postgres

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages