Giter VIP home page Giter VIP logo

http-helper's Introduction

http-helper

A simple http helper to support router and argument processing

NOTE This repository was archived and replaced by mini-gin

Sample #1: handler with http.HandlerFunc

package main

import (
    "github.com/rosbit/http-helper"
    "net/http"
    "fmt"
)

func main() {
    r := helper.NewHelper()

    r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        c.String(http.StatusOK, "hello")
    })

    r.Get("/json/:msg", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        msg := c.Param("msg")
        c.JSON(http.StatusOK, map[string]interface{} {
            "code": http.StatusOK,
            "msg": msg,
        })
    })

    r.Post("/json", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        var i interface{}
        code, err := c.ReadJSON(&i)
        if err != nil {
            c.Error(code, err.Error())
            return
        }
        c.JSONPretty(http.StatusOK, i, " ")
    })

    r.Get("/jump", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        url := c.QueryParam("u")
        if url == "" {
            c.Error(http.StatusBadRequest, "argument u expected")
            return
        }
        c.Redirect(http.StatusFound, url)
    })

    r.Post("/form/:name", func(w http.ResponseWriter, r *http.Request) {
        c := helper.NewHttpContext(w, r)
        n := c.Param("name")
        v := c.FormValue(n)
        c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
    })

    r.Run()
    // or r.Run(":8080")
    // or http.ListenAndServe(":8080", r)
}

Sample #2: handler with argument helper.Context

package main

import (
    "github.com/rosbit/http-helper"
    "net/http"
    "fmt"
)

func main() {
    r := helper.NewHelper()

    r.GET("/hello", func(c *helper.Context) {
        c.String(http.StatusOK, "hello")
    })

    r.GET("/json/:msg", func(c *helper.Context) {
        msg := c.Param("msg")
        c.JSON(http.StatusOK, map[string]interface{} {
            "code": http.StatusOK,
            "msg": msg,
        })
    })

    r.POST("/json", func(c *helper.Context) {
        var i interface{}
        code, err := c.ReadJSON(&i)
        if err != nil {
            c.Error(code, err.Error())
            return
        }
        c.JSONPretty(http.StatusOK, i, " ")
    })

    r.GET("/jump", func(c *helper.Context) {
        url := c.QueryParam("u")
        if url == "" {
            c.Error(http.StatusBadRequest, "argument u expected")
            return
        }
        c.Redirect(http.StatusFound, url)
    })

    r.POST("/form/:name", func(c *helper.Context) {
        n := c.Param("name")
        v := c.FormValue(n)
        c.String(http.StatusOK, fmt.Sprintf("value of %s: %s\n", n, v))
    })

    r.Run()
    // or r.Run(":8080")
    // or http.ListenAndServe(":8080", r)
}

http-helper's People

Contributors

rosbit avatar

Stargazers

 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.