Git Product home page Git Product logo

doggy's Introduction

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)
}

doggy's People

Contributors

sysulq avatar

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.