https://github.com/adrian-lin-1-0-0/simple
Simple is a simple web framework written in Go
https://github.com/adrian-lin-1-0-0/simple
Last synced: 5 months ago
JSON representation
Simple is a simple web framework written in Go
- Host: GitHub
- URL: https://github.com/adrian-lin-1-0-0/simple
- Owner: adrian-lin-1-0-0
- License: mit
- Created: 2023-05-03T10:56:49.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-30T12:23:13.000Z (over 1 year ago)
- Last Synced: 2024-11-26T02:23:14.572Z (7 months ago)
- Language: Go
- Size: 26.4 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple
Simple is a simple web framework written in Go
[](https://goreportcard.com/report/github.com/adrian-lin-1-0-0/simple)
[](https://codebeat.co/projects/github-com-adrian-lin-1-0-0-simple-master)## Functionality
- Layer 7 routing proxy (only URL path)
> Enables more granular routing rules based on HTTP URL paths.
- Middleware
> Allows users to define custom middleware functions to modify or enhance the request/response pipeline.
- Panic recover
> Automatically recovers from panics in your code to prevent your server from crashing.
- Path Routing
> Allows users to define custom routes based on URL paths.
- Group Routing
> Allows users to group routes together and apply common middleware or configuration to them.## Class Diagram
```mermaid
---
title: Simple Web Framework
---
classDiagramclass Proxy{
+ GET(proxyPath : String, target : String, handlers : HandlerFunc[])
+ POST(proxyPath : String, target : String, handlers : HandlerFunc[])
+ PUT(proxyPath : String, target : String, handlers : HandlerFunc[])
+ DELETE(proxyPath : String, target : String, handlers : HandlerFunc[])
}note for Simple"
- Simple and all groups share the same router.
- Simple need to implement the ServeHTTP method of the Handler interface.
net/http
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}"
class Simple{
- handler(c : *Context)
+ ServeHTTP(http.ResponseWriter, *http.Request)
}note for group "
All groups share the same router
"class group{
- middleware : HandlerFunc[]
- parent : *group
- router : *router
}
class router{
- addRouter(method : String , fullPath : String ,handlers : HandlerFunc[])
}note for node "
Trie tree node.
As shown in the diagram below (Trie Router).
"class node{
- fullPath : String
- path : String
- child : *node[]
- wildChild : Boolean
- handlers : HandlerFunc[]
}class Context{
+ Writer : http.ResponseWriter
+ Req : *http.Request
+ Params : HashMap
- handlers : HandlerFunc[]
}class HandlerFunc{
<>
- handler(c : *Context)
}Proxy "1" o-- "1" Simple
Simple "1" o-- "1" group
Simple "1" o-- "1" router
group "1" o-- "0..*" group
node "1" o-- "0..*" node
group "0..1" o-- "1*" router
router "1" o-- "0..*" node
node ..> Context
Simple ..> Context
Context o--> HandlerFunc
HandlerFunc ..> Context
``````mermaid
---
title: Trie Router
---
graph TB
/ --->|api| /api
/ --->|productpage| /productpage
/ --->|details| /details
/ --->|reviews| /reviews
/reviews --->|:product_id| /reviews/:product_id
/details --->|:product_id| /details/:product_id
/api --->|v1| /v1
```## Getting started
### Installation
```sh
go get github.com/adrian-lin-1-0-0/simple
```### A Simple Example
```go
package mainimport (
"log""github.com/adrian-lin-1-0-0/simple"
)func main() {
r := simple.Default()
r.Use(func(ctx *simple.Context) {
println("middleware")
ctx.Next()
})r.GET("/hello", func(c *simple.Context) {
c.HTML("Hello
")
})v1 := r.Group("/v1")
v1.GET("/hello", func(c *simple.Context) {
a := []int{1, 2, 3}
//recovery
println(a[3]) // panic: runtime error: index out of range [3] with length 3
c.HTML("Hello v1
")
})v2 := r.Group("/v2")
v2.GET("/hello", func(c *simple.Context) {
c.HTML("
Hello v2
")
})log.Fatal(
r.Run(":8888", func() {
println("Server is running on port http://localhost:8888")
}),
)
}
```A Proxy Example
```go
package mainimport (
"log""github.com/adrian-lin-1-0-0/simple"
)func main() {
proxy := simple.DefaultProxy()
proxy.GET("/api/v1", "http://localhost:8888/v1")
proxy.GET("/api/v2", "http://localhost:8888/v2")log.Fatal(
proxy.Run(":9999", func() {
println("Server is running on port http://localhost:9999")
}),
)
}
```