Giter Site home page Giter Site logo

rootgo's Introduction

Root

Simple and lightweight HTTP router/server for Golang

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣷⠀⠀⠀⢀⣤⣾⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⠀⠀⠀⠹⣧⡀⠀⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣷⣦⣄⠘⣷⡀⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠛⠛⠀⠘⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⠀⢴⣾⠿⠛⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⢲⣶⣶⣶⡖⠒⠒⠒⠒⠒⠒⠒⠒⠒⠒⠀⠀
⠀⠀⠀⠀⠀⢠⡶⠾⠆⠀⠀⠀⠀⣸⣿⠁⠈⠙⢷⡶⠶⣦⣄⡀⠀⠀⠀⠀⠀⠀
⠀⠸⠷⣦⡀⠘⢷⣄⠀⠀⠀⠀⢠⡿⣯⡀⠀⠀⠈⢿⡄⠀⠉⠁⠀⠀⣠⡄⠀⠀
⠀⠀⢠⡾⠿⠛⠻⠿⠿⠶⠶⠾⠛⢁⣸⣷⣦⣀⠀⠈⠁⠀⠀⢀⣤⣾⡋⠀⠀⠀
⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⢀⣴⠿⣯⣀⡀⠉⠙⠛⠓⠒⠚⠋⠁⠈⢿⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠀

Current features overview

  • Adding GET, POST, PATCH, PUT, DELETE endpoints
  • Routes with params
  • Getting value of params, several params with the same name presented as slice
  • Router can be used both with standard library and our server wrapper
  • Server wrapper that implements some high level utils(Write, WriteFiles)
  • Working with views

Coming features

  • [Server] Implement caching(in memory and in redis or any other db that implement Cache interface in /caching/cache.go)

Usage example

func main(){
	r := router.NewRouter("/api")

	server := api.NewServer(r, api.Options{Port: "8000"})

	server.ASSETS("/assets")

	server.GET("/users/contacts/:id", func(ctx *api.Context) {
		ids := ctx.Params.Get("id")
		ctx.WriteJSON("GET id:"+id[0], 200)
	})

	server.POST("/users", func(ctx *api.Context) {
		user := new(User)
		err := ctx.Body.Decode(user)

		if err != nil {
			ctx.WriteError(types.Error{Message: "You entered wrong values", Status: 400})
		}

		if err := ctx.Write(user, 200); err != nil {
			ctx.WriteError(types.Error{Message: "Internal server error", Status: 500})
		}
	})

	server.GET("/document", func(ctx *api.Context) {
		file, err := os.ReadFile("./assets/doc.pdf")
		if err != nil {
			ctx.WriteError(types.Error{Message: err.Error(), Status: 500})
		}

		ctx.WriteFile(200, file, api.PDFFileType)
	})

	server.Run()
}

Usage only router without server wrapper

func main(){
	r := router.NewRouter("/api")

	r.GET("/users/:id", func(ctx *router.Context, w http.ResponseWriter, r *http.Request) {
		ids := ctx.Params.Get("id")
		io.WriteString(w, "Your user's ID is:"+ids[0])
	})

	r.POST("/users", func(ctx *router.Context, w http.ResponseWriter, r *http.Request) {
		user := new(User)
		err := ctx.Body.Decode(user)

		if err != nil {
			util.WriteError(w, types.Error{Message: "Wrong creation", Status: 400})
		}

		io.WriteString(w, "user was successfully created")
	})

	log.Fatalln(http.ListenAndServe(":"+cfg.Http.Port, r))
	

Licensed by MIT

rootgo's People

Contributors

mxrcury avatar

Watchers

 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.