Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Oct 1, 2023
1 parent fdbedd7 commit c500e95
Show file tree
Hide file tree
Showing 44 changed files with 31,164 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Template Docker CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build the Template Backend Docker image
run: cd backend && docker build . --file Dockerfile --tag template-backend:$(date +%s)

- uses: actions/checkout@v3
- name: Build the Template FrontEnd Docker image
run: cd frontend && docker build . --file Dockerfile --tag template-frontend:$(date +%s)

- uses: actions/checkout@v3
- name: Build the Template CORS Docker image
run: cd backend && docker build . --file Dockerfile --tag template-cors:$(date +%s)
25 changes: 25 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19.2

- name: Build
run: cd backend && go build -v

- name: Test
run: cd backend && go test -v
27 changes: 27 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node Build CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- name: Setup Node 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: npm
cache-dependency-path: "./frontend/package-lock.json"
- run: cd frontend && npm ci
- run: cd frontend && npm run build
- run: cd frontend && npm test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.frontend
data
85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
# server-ui-container-boilerplate
# server-ui-container-boilerplate
# Multi-Container Web Application with Docker

This repository contains a multi-container web application built using Docker, with separate containers for the server, UI, and MongoDB for data storage. The server-side code is written in Golang, and the user interface is developed using React. This setup allows you to easily deploy and manage the various components of your web application as isolated containers.

## Features

- **Server Container (Golang):** The server-side code is written in Golang, providing a fast and efficient backend for your application. It handles data processing and communication with the MongoDB container.

- **UI Container (React):** The user interface is built using React, a popular JavaScript library for creating dynamic and responsive web applications. The UI container serves as the frontend of your application.

- **MongoDB Container:** MongoDB is used as the database for storing and retrieving data. The MongoDB container ensures data persistence and can be easily configured to suit your application's needs.

- **Docker Compose:** The `docker-compose.yml` file is included to orchestrate and manage the deployment of these containers. Docker Compose simplifies the process of starting and connecting multiple containers.

## Getting Started

To get started with this multi-container web application, follow these steps:

1. **Clone the Repository:** Clone this repository to your local machine using `git clone`.

2. **Set Up Environment Variables:** Create a `.env` file in the project root directory and configure environment variables such as database connection settings, server configurations, and any secret keys. For initial setup, i have .env file already. But you can go ahead and make changes as per your requirement.

3. **Build and Start Containers:** Use Docker Compose to build and start the containers defined in the `docker-compose.yml` file. Run the following command:

```bash
docker-compose up --build
```

This command will start the server, UI, and MongoDB containers.

4. **Access the Application:** Once the containers are up and running, you can access the web application by opening your browser and navigating to `http://localhost:3000`.

5. **Customize and Extend:** Customize the server and UI code to suit your project's requirements. You can modify the Golang server code in the `server/` directory and the React UI code in the `ui/` directory.

6. **Scaling and Deployment:** Docker Compose makes it easy to scale your application and deploy it to various environments. You can configure additional settings for production, staging, or development environments as needed.

## Contributing

We welcome contributions to this project. If you find a bug, have a feature request, or want to contribute code, please open an issue or submit a pull request. Check out our [Contribution Guidelines](CONTRIBUTING.md) for more details.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

Thank you for choosing our multi-container web application template. I hope this setup simplifies the development and deployment of your server and UI components, along with MongoDB as your data store. If you have any questions or need assistance, feel free to reach out. Happy coding!


[Tech Stack](#Tech) |
[Pre-requisites](#prerequisites) |
[Basic commands](#Basic-commands-to-run-the-containers) |
[Database diagram](#Database-diagram)

## Tech
- Docker for creating containers for UI, Server and DB
- React for user interface and visualization
- Go for backend APIs
- Mongo for DB

## Prerequisites
- Docker (LTS)
- NodeJS (LTS)
- MongoDB (LTS)
- Go Compiler (1.19.2)

## Basic commands to run the application

```sh
sudo docker compose up [--build]
api1 -> http://localhost:4000 [non cors]
api2 -> http://localhost:4545 [cors] [use with web application]
mongo express -> http://localhost:8081
UI -> http://localhost:3000
```

## CI/CD Pipeline

- GO CI for API code -> Action Name -> [![Go](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/go.yml/badge.svg)](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/go.yml)

- Node CI for UI Code -> [![Node.js CI](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/node.js.yml/badge.svg)](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/node.js.yml)

- Docker CI -> [![Exam Center Docker CI](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/docker-image.yml/badge.svg)](https://github.com/chrispeterjeyaraj/server-ui-container-boilerplate/actions/workflows/docker-image.yml)
17 changes: 17 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
*.env
tmp

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
27 changes: 27 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM golang:1.19.2 AS development
WORKDIR /templateserver
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go install github.com/cespare/reflex@latest
EXPOSE 4000
# CMD reflex -g '*.go' go run main.go --start-service
ENTRYPOINT ["reflex", "-c", "reflex.conf"]

FROM golang:1.19.2 AS builder
ENV GOOS linux
ENV CGO_ENABLED 0
WORKDIR /templateserver
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o templateserver

FROM alpine:latest AS production
RUN apk add --no-cache ca-certificates
COPY --from=builder templateserver .
EXPOSE 4000
CMD ./templateserver



16 changes: 16 additions & 0 deletions backend/configs/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package configs

import (
"log"
"os"

"github.com/joho/godotenv"
)

func EnvMongoURI() string {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
return os.Getenv("MONGOURI")
}
40 changes: 40 additions & 0 deletions backend/configs/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package configs

import (
"context"
"fmt"
"log"
"time"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func ConnectDB() *mongo.Client {
client, err := mongo.NewClient(options.Client().ApplyURI(EnvMongoURI()))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}

//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB")
return client
}

// Client instance
var DB *mongo.Client = ConnectDB()

// getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
collection := client.Database("template").Collection(collectionName)
return collection
}
37 changes: 37 additions & 0 deletions backend/database/Todos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package database

import (
"context"
"time"

"go.mongodb.org/mongo-driver/bson"

"github.com/chrispeterjeyaraj/server-ui-container-boilerplate/backend/configs"
models "github.com/chrispeterjeyaraj/server-ui-container-boilerplate/backend/pkg/models"
)

func GetAllTodos(CollectionName string) (bool, []models.Todo) {
var results []models.Todo

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

collection := configs.GetCollection(configs.DB, CollectionName)

allTodos, errFind := collection.Find(ctx, bson.M{})

if errFind != nil {
return false, nil
}

for allTodos.Next(ctx) {
var singleTodo models.Todo
if errFind = allTodos.Decode(&singleTodo); errFind != nil {
return false, nil
}

results = append(results, singleTodo)
}

defer cancel()
return true, results
}
30 changes: 30 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module github.com/chrispeterjeyaraj/server-ui-container-boilerplate/backend

go 1.19

require go.mongodb.org/mongo-driver v1.10.3

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gorilla/mux v1.8.0
)

require github.com/stretchr/testify v1.7.0 // indirect

require (
github.com/avct/uasurfer v0.0.0-20191028135549-26b5daa857f1
github.com/dgryski/dgoogauth v0.0.0-20190221195224-5a805980a5f3
github.com/golang/snappy v0.0.4 // indirect
github.com/joho/godotenv v1.4.0
github.com/klauspost/compress v1.15.11 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
rsc.io/qr v0.2.0
)
Loading

0 comments on commit c500e95

Please sign in to comment.