Giter Site home page Giter Site logo

articleapi's Introduction

Article REST API

References

Environment variables

Create a .env file in the home of the project. The content should look like (change the secret values!):

POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword
POSTGRES_DB=articlesDB
[email protected]
PGADMIN_DEFAULT_PASSWORD=mypassword

Run

With go run

You need to create the .env file mentioned above.

Run the program with the command (from the project directory):

source .env
go run cmd/main.go

Dockerized stack

Provide the .env file mentioned above.

Run the full stack with:

docker compose up -d

Middleware

The database stack is composed by the PostgreSQL database and the PgAdmin instance to manage it.

The middleware is provided as a Dockerized stack to be run with Docker compose. The reference file is database.yml in the database folder.

Start the middleware

Start the middleware:

docker compose -f middleware.yml up -d

Table creation

You can create the database tables with the initial.sql SQL script in the sql directory.

The content of the script is the following (in case you want to run it line by line):

CREATE TABLE articles (id VARCHAR(36) PRIMARY KEY, title VARCHAR(100) NOT NULL, description VARCHAR(50) NOT NULL, content VARCHAR(50) NOT NULL);

SELECT EXISTS (SELECT FROM pg_tables WHERE  schemaname = 'public' AND tablename = 'articles' );

CREATE TABLE IF NOT EXISTS  articles  (id VARCHAR(36) PRIMARY KEY, title VARCHAR(100) NOT NULL, description VARCHAR(50) NOT NULL, content VARCHAR(50) NOT NULL);

INSERT INTO articles (id,title,description,content) VALUES ('8617bf49-39a9-4268-b113-7b6bcd189ba2', 'Article 1', 'Article Description 1', 'Article Content 1');

Test

Test the API with curl.

curl http://localhost:8080
curl http://localhost:8080/articles
curl http://localhost:8080/articles/[PUT_THE_CORRECT_ID_HERE]
curl -X POST --data '{"Title":"Second","Desc":"Description","Content":"This is the fantastic content."}' --header 'Content-Type: application/json' http://localhost:8080/articles

ToDo

  • Authentication (password + JWT).
  • Hash user password in database.
  • Dependency Injection and Inversion of Control (see here).
  • Separate queries from endpoints.
  • User management.
  • Authorizations.

articleapi's People

Contributors

elman23 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.