Welcome to dipra. Dipra is mini framework golang to build service application. Dipra have high performance speed. Makes native source codes. Suitable for build REST API.
- HTTP with all method and static file.
- Data binding request body(JSON,XML) and query raw.
- Response String,JSON,JSONP,XML and File.
- Suport middlere handler for al and by route.
- Enable and disable log.
Install must be have GO system on your PC. If you have it, you can install with cmd.
- Install package
go get -u github.com/didikprabowo/dipra
- Import code Kindly import package at top code program, for exampe :
import (
"github.com/didikprabowo/dipra"
)
main.go
package main
import (
"net/http"
"github.com/didikprabowo/dipra"
)
func main() {
r := dipra.Default()
r.GET("/hello-world", func(c *dipra.Context) error {
return c.JSON(http.StatusOK, dipra.M{
"data": "Hello world.",
})
})
r.Run(":6000")
}
Route base http server default golang. It leverages sync pool
to use memory. By besides, dipra router will find priority routing which request it.
For use route must be define path and handler, but can be use middleware. For example :
r := dipra.Default()
r.GET("/GET", func(c *dipra.Context) error {
return c.JSON(http.StatusOK, dipra.M{
"data": "Hello Get.",
})
})
r.POST("/POST", func(c *dipra.Context) error {
return c.JSON(http.StatusOK, dipra.M{
"data": "Hello Post.",
})
})
package main
import (
"fmt"
"math/rand"
"net/http"
"github.com/didikprabowo/dipra"
)
func main() {
r := dipra.Default()
r.Use(dipra.Logger())
// Normal Group
v1 := r.Group("/v1")
{
// {basepath}/v1
v1.GET("/", func(c *dipra.Context) error {
return c.String(200, fmt.Sprintf("Welcome to api version v1 %+f\n", rand.Float64()))
})
}
// Group use middleware
v2 := r.Group("/v2", func(hf dipra.HandlerFunc) dipra.HandlerFunc {
return func(c *dipra.Context) (err error) {
if c.Query("name") == "jhon" {
return c.String(http.StatusUnprocessableEntity, "Can't continue")
}
hf(c)
return
}
})
{
// {basepath}/v2
v2.GET("/", func(c *dipra.Context) error {
return c.String(200, fmt.Sprintf("Welcome to api version v2 %+f\n", rand.Float64()))
})
}
err := r.Run(":9020")
if err != nil {
panic(err)
}
}
To get request from body raw you can bind with type mime(JSON,XML). By besides, you be able to bind query raw with format ?key=value&key=value
. For example used :
// Example : /ping/1
r := dipra.Default()
r.GET("/ping/:id", func(c *dipra.Context) error {
id := c.Param("id")
return c.JSON(http.StatusOK, dipra.M{
"data": id,
})
})
r := dipra.Default()
r.GET("/json", func(c *dipra.Context) error {
var p Personal
err := c.ShouldJSON(&p)
if err != nil {
return c.JSON(http.StatusBadRequest, dipra.M{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, dipra.M{
"data": p,
})
})
// example : ?id=2&name=didik
r.GET("/query", func(c *dipra.Context) error {
var p Personal
singleID := p.Query("id")
err := c.ShouldQuery(&p)
if err != nil {
return c.JSON(http.StatusBadRequest, dipra.M{
"error": err.Error(),
})
}
return c.JSON(http.StatusOK, dipra.M{
"data": p,
"id" : singleID,
})
})
Dipra can write response in the from of String,JSON,JSONP, XML and File. Fro example :
r := dipra.Default()
r.GET("/json", func(c *dipra.Context) error {
p := Personal{
Name: "Didik Prabowo",
Address: "Wonogiri",
}
return c.JSON(http.StatusOK, dipra.M{
"data": p,
})
})
r.GET("/jsonp", func(c *dipra.Context) error {
p := Personal{
Name: "Didik Prabowo",
Address: "Wonogiri",
}
return c.JSONP(http.StatusOK, dipra.M{
"data": p,
})
})
r.GET("/string", func(c *dipra.Context) error {
return c.String(200, "Welcome to dipra")
})
r.GET("/xml", func(c *dipra.Context) error {
p := Personal{
Name: "Didik Prabowo",
Address: "Wonogiri",
}
return c.XML(http.StatusOK, dipra.M{
"data": p,
})
})
r.GET("/file", func(c *dipra.Context) error {
return c.File("public/p.png")
})
Dipra provide for you want get static file as .css, .html,.png and etc. For example :
main.go
package main
import (
"github.com/didikprabowo/dipra"
)
func main() {
r := dipra.Default()
// {{basepath}}/get-image
r.GET("/get-image", func(c *dipra.Context) error {
return c.File("public/p.png")
})
// {{basepath}}/static/p.png
r.Static("/static", "./public")
r.Run(":9000")
}
Dipra provide middleware handle for handler function. For example you can print log in middleware. Any 2 method how to define middleware : Define in all route and spesific route.
package main
import (
"net/http"
"github.com/didikprabowo/dipra"
"github.com/didikprabowo/dipra/middleware"
)
route := dipra.Default()
route.Use(middleware.Logger())
route.GET("/", func(c *dipra.Context) error {
return c.JSON(http.StatusOK, dipra.M{
"status": true,
})
})
package main
import (
"net/http"
"github.com/didikprabowo/dipra"
"github.com/didikprabowo/dipra/middleware"
)
route := dipra.Default()
// default :: route.Use(dipra.CORS())
route.Use(middleware.CorsWithConifg(middleware.CORSConfig{
AllowOrigins: []string{"https://www.google.com"},
AllowMethod: []string{"*"},
AllowHeaders: []string{"*"},
}))
route.GET("/", func(c *dipra.Context) error {
return c.JSON(http.StatusOK, dipra.M{
"data": "Example cors",
})
})
It's for handle error panic, you can use this middleware for handle it.
Please instance middleware recovery
for used it. For example :
main.go
package main
import (
"github.com/didikprabowo/dipra"
"github.com/didikprabowo/dipra/middleware"
)
func main() {
r := dipra.Default()
r.Use(middleware.Recovery())
r.GET("/", func(c *dipra.Context) error {
panic("Error panic")
})
err := r.Run(":9020")
if err != nil {
panic(err)
}
}
You can create new custom middleare at route or at all route.
package main
import (
"log"
dipra "github.com/didikprabowo/dipra"
)
func main() {
s := dipra.Default()
s.Use(TesMiddlewareAll)
s.GET("/ping", Get, TesMiddleware)
err := s.Run(":6000")
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
}
func Get(c *dipra.Context) error {
return c.JSON(200, dipra.M{
"data": "tes",
})
}
func TesMiddlewareAll(next dipra.HandlerFunc) dipra.HandlerFunc {
return func(c *dipra.Context) error {
return next(c)
}
}
func TesMiddleware(next dipra.HandlerFunc) dipra.HandlerFunc {
return func(c *dipra.Context) error {
return next(c)
}
}
- Didik Prabowo - https://kodingin.com