Giter Site home page Giter Site logo

chi-template's Introduction

Chi Template

This template deploys a base ready-to-use Chi app.

Deploy on Railway

Features

  • Two examples of custom middleware:

    • TrustProxy: Checks if the request IP matches one of the provided ranges/IPs, then inspects common reverse proxy headers and sets the corresponding, fields in the HTTP request struct for use by middleware or handlers that are next.

      Useful for trusting the X-Forwarded-For and X-Forwarded-Proto headers that Railway's proxy sets.

    • Logger: The Logger middleware gathers metrics from the upstream handlers (status code, duration, bytes written) and logs them to stdout.

  • Comes with some helpful internal packages:

    • logger: The internal logger package is centered around Go's slog package but has some pre-configured loggers for added ease of use.

    • router: This is where the Chi router is initialized and global middlewares are registered like the TrustProxy and Loggermiddlewares. Along with registering some handlers and path prefixes.

    • responder: Comes with a few utility functions to send json (formatted and unformatted) or plaintext responses to the http client.

    • tools: Simple tools package, currently only has a EnvPortOr function that reads the PORT variable from the environment or falls back to the provided port.

    • server: This is where the http.Server settings are configured and the server is started from.

    • routes: Where all the handlers and paths live, this template comes with the following prefixes and handlers:

      /: Returns a greeting message.

      /health: Returns just a 200 status code.

      /api/: The api prefix, with a subrouter.

      /api/v1/: The v1 prefix with a subrouter, registered on the /api/ subrouter.

      /api/v1/temperature: Simple mock api endpoint.

      /api/v1/forecast/3day: Just another mock api endpoint, but with a path parameter.

How to use

  • Download the Go modules: go mod download

  • Start the server: go run main.go

  • Open the browser or any api test program to http://127.0.0.1:3000

  • Start coding!

Notes

  • The http server listens on port 3000 if no environment variable PORT is found, and once on Railway a PORT variable is automatically generated, and EnvPortOr will use that PORT instead of 3000.

chi-template's People

Contributors

brody192 avatar

Watchers

 avatar

Forkers

renlyltd

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.