doggy

package module
v0.0.0-...-9967220 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 15 Imported by: 0

README

Doggy

Build Status Go Report Card

Lightweight, idiomatic and stable for building Go 1.7+ HTTP services. It aims to provide a composable way to develop HTTP services.

dependency

Generate api struct

curl -s https://api.github.com/repos/chimeracoder/gojson | gojson -name=Repository -tags=schema,json

Generate model package

xo mysql://user:passwd@host:port/db -o . --template-path templates --ignore-fields updateTime

Example

package main

import (
	"net/http"
	"net/url"
	"time"

	"github.com/hnlq715/doggy"
	"github.com/hnlq715/doggy/httpclient"
	"github.com/hnlq715/doggy/middleware"
	"github.com/hnlq715/doggy/render"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {

	m := doggy.NewMux()

	m.Handle("/metrics", promhttp.Handler())
	m.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
		processTime := 4 * time.Second
		ctx := r.Context()
		select {
		case <-ctx.Done():
			return
		case <-time.After(processTime):
		}
		render.Text(w, 200, "pong")
	})

	m.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
		data := make(map[string]interface{})
		u, _ := url.Parse("http://httpbin.org/get")
		u.RawQuery = r.Form.Encode()
		err := httpclient.Get(r.Context(), u.String()).ToJSON(&data)
		if err != nil {
			render.Text(w, 200, err.Error())
			return
		}
		render.JSON(w, 200, data)
	})

	n := doggy.Classic()
	n.Use(middleware.NewPrometheus())
	n.UseHandler(m)

	doggy.ListenAndServeGracefully(n)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Classic

func Classic() *negroni.Negroni

Classic returns a new Negroni instance with the default middleware already in the stack.

Recovery - Panic Recovery Middleware Logger - Request/Response Logging TraceID - Trace ID Middleware RealIP - Get Real Client IP CloseNotify - Notify Client Close Timeout - Stop Process When Timeout

func DecodeSchema

func DecodeSchema(dst interface{}, src map[string][]string) error

DecodeSchema decodes a map[string][]string to a struct.

func ListenAndServe

func ListenAndServe(handler http.Handler) error

ListenAndServe always returns a non-nil error.

func ListenAndServeGracefully

func ListenAndServeGracefully(handler http.Handler) error

ListenAndServeGracefully always returns a non-nil error.

func New

func New(handlers ...negroni.Handler) *negroni.Negroni

New returns a new Negroni instance with no middleware preconfigured.

func NewHttpRouter

func NewHttpRouter() *httprouter.Router

NewHttpRouter returns a new httprouter instance.

func NewMux

func NewMux() *mux.Router

NewMux returns a new router instance.

func ValidateStruct

func ValidateStruct(s interface{}) error

ValidateStruct use tags for fields. result will be equal to `false` if there are any errors.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL