• This repository has been archived on 16/Mar/2019
  • Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED] ๐Ÿ A minimalistic JSON API server in Go

โš ๏ธ DEPRECATED โš ๏ธ

This project is no longer maintained, please use something like gorilla/mux or echo.


Goat GoDoc Build Status Coverage Status

Goat is a minimalistic REST API server in Go. You can pronounce it like the goat, or go-at. Depends on how you like goats.

Contents

Usage

Parameters

You can use named parameters and access them through goat.Params, wich you can treat as any map[string]string.

package main

import (
    "net/http"

    "github.com/bahlo/goat"
)

func helloHandler(w http.ResponseWriter, r *http.Request, p goat.Params) {
    goat.WriteJSON(w, map[string]string{
        "hello": p["name"],
    })
}

func main() {
    r := goat.New()

    r.Get("/hello/:name", "hello_url", helloHandler)

    r.Run(":8080")
}

Subrouters

You can create subrouters to simplify your code

func main() {
    r := goat.New()

    r.Get("/hello/:name", "hello_url", helloHandler)

    sr := r.Subrouter("/user")
    {
        sr.Post("/login", "user_login_url", loginHandler)
        sr.Get("/logout", "user_logout_url", logoutHandler)
    }

    r.Run(":8080")
}

Indices

Every route can have a description (like user_login_url). These can be used to automagically generate an API index (like this). If you want to hide specific methods, just provide an empty string.

func main() {
    r := goat.New()

    r.Get("/", "", r.IndexHandler)
    r.Get("/hello/:name", "hello_url", helloHandler)

    sr := r.Subrouter("/user")
    {
        sr.Post("/login", "user_login_url", loginHandler)
        sr.Get("/logout", "user_logout_url", logoutHandler)
    }

    r.Run(":8080")
}

The above example would return the following response on /:

{
  "hello_url": "/hello/:name",
  "user_logout_url": "/user/logout"
}

Note: Indices are only supported for GET requests. Open an issue, if you want them on other methods, too

Middleware

You can easily include any middleware you like. A great guide to middleware is found here. Important is, that it's in the following format:

func(http.Handler) http.Handler

Example:

func main() {
    r := goat.New()

    r.Get("/hello/:name", "hello_url", helloHandler)
    r.Use(loggerMiddleware, gzipMiddleware)

    r.Run(":8080")
}

Wrapping middleware

Sometimes middleware isn't in the required format, so you have to build a wrapper around it. This example shows a wrapper around handlers.CombinedLoggingHandler from the Gorilla handlers:

func loggerMiddleware(h http.Handler) http.Handler {
    // Create logfile (you should check for errors)
    f, _ := os.Create("api.log")
    return handlers.CombinedLoggingHandler(f, h)
}

You can now safely use the middleware in Goat:

func main() {
    r := goat.New()

    r.Get("/hello/:name", "hello_url", helloHandler)
    r.Use(loggerMiddleware)

    r.Run(":8080")
}

Philosophy

I wanted to create a small, fast and reliable REST API server, which supports quick JSON and error output, good rooting and easy-to-use middleware.

I have split the files after responsibility to make it easy for everyone to dive in (start with goat.go).

Feedback

If you have problems, feel free to create an issue or drop me an email at [email protected]!

Credits

Thanks to Julien Schmidt for the amazing httprouter used in this project.

License

This project is licensed unter MIT, for more information look into the LICENSE file.

More Repositories

1

go-styleguide

๐Ÿ† Opinionated Styleguide for the Go language
1,491
star
2

iterm-colors

โ™ฅ๏ธ Awesome iTerm color schemes
247
star
3

sonyflake-rs

๐Ÿƒ A distributed unique ID generator inspired by Twitter's Snowflake.
Rust
172
star
4

mapdns

A simple, static DNS server.
Go
86
star
5

arne.me

My personal website
Rust
63
star
6

abutil

[UNMAINTAINED] ๐Ÿ†Ž A collection of often-used Golang helpers
Go
54
star
7

arnes-notes

๐Ÿง  My working notes.
SCSS
37
star
8

ing-ynab

๐Ÿ’ฐ Import your ING Germany bank statements via FinTS into YNAB.
Python
25
star
9

dotfiles

๐Ÿ‘จ๐Ÿปโ€๐Ÿ”ง My .files
Shell
15
star
10

generic-list-go

Go container/list but with generics
Go
15
star
11

serverless-podcast

[UNMAINTAINED] ๐Ÿ“ข Easy, cheap podcast hosting using Serverless and S3
HTML
14
star
12

homebridge-temperature-file

Simulate a temperature sensor from file
JavaScript
12
star
13

xkcd-substitutions

๐Ÿ“ฐ Browser extension to make reading the news more fun
CoffeeScript
10
star
14

discord-retention-bot

๐Ÿค– A bot that allows you to set a message retention for individual Discord text channels.
Rust
7
star
15

firechicken.club

An invite-only webring for personal websites.
Zig
7
star
16

COVIDget

๐Ÿฆ  Display regional COVID infections in an iOS 14 widget on your home screen.
Swift
5
star
17

mr-meeseeks

๐Ÿ™‹ Ansible deployment via Slack. I'm Mr. Meeseeks! Look at me!
JavaScript
5
star
18

hkpeople

๐Ÿ‘ช HomeKit Sensor to know when people (i.e. their phones) are home
Go
4
star
19

homebridge-humidity-file

Simulate a humidity sensor from file
JavaScript
4
star
20

scriptable-scripts

Scripts to use in the iOS app `Scriptable`
JavaScript
4
star
21

happyscribe2podlove

Smol cli tool that converts Happy Scribe JSON to VTT understood by Podlove.
Rust
4
star
22

spockhub

[DEPRECATED] Everyone likes the dark bar from GitHub Enterprise
CoffeeScript
3
star
23

bencode-go

Decode bencoded data.
Go
3
star
24

beats-status

Shows the currently playing Beats 1 track in your menubar
JavaScript
2
star
25

fradev-bot

๐Ÿ“ฎ The bot of the Frankfurt Developer Slack
JavaScript
2
star
26

scriptable-COVIDget

An alternative to COVIDget via Scriptable.
JavaScript
2
star
27

plextube

A dead-simple YouTube downloader, meant for Plex.
Dockerfile
2
star
28

arnes-weekly

๐Ÿ“ง The archives of Arneโ€™s Weekly. In this newsletter I will send you a weekly roundup of things I found interesting on the internet. The more our interests align the more you'll like it.
2
star
29

axiom-zig

An unofficial Zig SDK for Axiom.
Zig
2
star
30

empty

1
star
31

jan.work

My personal website
PHP
1
star
32

bahlo

1
star
33

next-netlify-starter

JavaScript
1
star