gnunez / go-web-starter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Golang Web Application Starter Project

This repository contains a sample Golang web application project that serves as a starting point for building your own web applications.

Features

  • Basic HTTP server listening on port 8080 with Status and Echo handlers, which provide a solid starting point for implementing custom handlers that receive and return JSON data.
  • /status endpoint for health checks:
    • A simple example of a handler that returns JSON data, which can be used as a starting point for creating more complex handlers that receive and return JSON.
  • /echo endpoint for echoing request messages:
    • Demonstrates how to read JSON data from a request and return it in the response. This can be adapted for more advanced use cases where processing or modification of the data is needed.

Getting Started

Prerequisites

  • Golang installed on your system
  • Docker installed on your system

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/golang-webapp-starter.git
  1. Change directory to the project folder:
cd golang-webapp-starter
  1. Build the project:
make build run

or

go build 
  1. Run the server:
make  run

or

./server 

The server will start listening on port 8080.

Usage

Flags

  • --config or -c: Specify the path to the configuration file (default is $HOME/.server.yaml)
  • --webroot or -w: Define the web root folder (default is ./web/dist)

Endpoints

  1. /status: Health check endpoint. Returns a JSON object with a status key and the value "Ok".

  2. /echo: Accepts a JSON object with a message key and echoes the request message. Logs the entire request and the message.

Functions

Main Functions

  • Execute(): Executes the root command and initializes the server.
  • initConfig(): Reads the configuration file and environment variables.

HTTP Handlers

  • Status(): HTTP handler for the /status endpoint. Returns the health check status.
  • Echo(): HTTP handler for the /echo endpoint. Returns the echoed message from the request.

License

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Acknowledgments

  • Cobra for CLI support
  • Viper for configuration management

About


Languages

Language:Go 65.1%Language:Dockerfile 18.0%Language:JavaScript 11.6%Language:Makefile 5.3%