Coder Social home page Coder Social logo

go-with-vue's Introduction

go-with-vue

Simple web app build with Golang, PostgreSQL, Docker and Vue.js

Getting started

First, we have to create Docker images of components by executing docker-compose build. Afterward, we execute docker-compose up and this involves 3 stages:

  1. It spins up a PostgreSQL instance container that runs on port 5432
  2. After the database instance is ready, a small Golang program named newsfeeder gets executed. It fetches RSS feeds from NewYork Times and insert the entries into the database.
  3. Lastly, a Golang web server gets spun up after the database and newsfeeder are ready.

Main front page

By accessing to http://localhost:8080/, we see a list of news articles that were fetched from NewYork Times RSS feed along with Rating for each article which is implemented for this app.

Top 5 best rated articles

By accessing to http://localhost:8080/bestnews, we see a JSON object that contains an array of the five best rated news articles. This object is updated every 5 minutes, so accessing the endpoint does not make the back-end hit the database, therefore reducing the database load. Initially, all of the articles are not rated, therefore we see an empty array for the first 5 minutes.

Architectural Decision

One of the common design problems when developing a web application with Golang is how to manage a database connection such that the application runs efficiently and is unit-testable. In order to solve these issues, we use a context object and Golang's interface feature for our database type. In api/main.go we can see a struct:

type Context struct {
	DB       database.Database
	BestNews *models.BestNews
}

that holds a database object that implements Database interface and a bestnews object that gets updated every 5 minutes. The Context object gets passed to the handlers for endpoints and each handler can have an access to the database object. This brings us the following benefits:

  1. Eliminating the use of global variable for a database connection object.
  2. Each handler does not have to instantiate a new database object, which is computationally expensive.
  3. Making the handlers unit-testable by implementing a mock database object that implements the database interface.

go-with-vue's People

Contributors

kansuke231 avatar

Watchers

 avatar  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.