Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ynwd/server
A simple and idiomatic golang framework for handling HTTP
https://github.com/ynwd/server
golang handler middleware routing server web
Last synced: about 1 month ago
JSON representation
A simple and idiomatic golang framework for handling HTTP
- Host: GitHub
- URL: https://github.com/ynwd/server
- Owner: ynwd
- Created: 2022-01-08T10:24:03.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-10T00:16:26.000Z (almost 3 years ago)
- Last Synced: 2024-06-21T02:03:52.748Z (6 months ago)
- Topics: golang, handler, middleware, routing, server, web
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Server
[![][build]](https://github.com/fastrodev/server/actions/workflows/build.yml) [![Coverage Status][cov]](https://coveralls.io/github/fastrodev/server?branch=main) [![][reference]](https://pkg.go.dev/github.com/fastrodev/server?tab=doc)A simple and idiomatic golang framework for handling HTTP. It uses standard golang requests and responses.
## Get startedInit folder
```
mkdir app && cd app
```
Init app
```
go mod init app
```
Install
```
go get github.com/fastrodev/server
```
Create main.go file:
```go
package mainimport (
"fmt"
"log"
"net/http""github.com/fastrodev/server"
)func main() {
s := server.New()
s.Get("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Helo World")
})
log.Panic(s.ListenAndServe())
}
```
## Handler
You can use golang standard `http.ResponseWriter` and `http.Request` for the handler signature.
```go
package mainimport (
"fmt"
"log"
"net/http""github.com/fastrodev/server"
)func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Helo World")
}func main() {
s := server.New()
s.Get("/", handler)
log.Panic(s.ListenAndServe())
}
```
## Middleware
You can access all properties and methods of `http.ResponseWriter` and `r *http.Request` before they are handled by the handler.
```go
package mainimport (
"fmt"
"log"
"net/http"
"time""github.com/fastrodev/server"
)func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Helo World")
}func middleware(w http.ResponseWriter, r *http.Request, next server.Next) {
dt := time.Now()
fmt.Println(dt)
next(w, r)
}func main() {
s := server.New()
s.Get("/", handler, middleware)
log.Panic(s.ListenAndServe())
}
```## Routing
You can add multiple handlers for a path with different methods. You can also use common url parameters or with regex.
```go
package mainimport (
"encoding/json"
"fmt"
"log"
"net/http""github.com/fastrodev/server"
)func main() {
s := server.New()
s.Get("/api/user", getAllUser)
s.Get("/api/user/:name", getUserHandler)
s.Get("/api/account/:id([0-9]+)", getUserByIDHandler)
s.Post("/api/user", postHandler)
s.Put("/api/user", putHandler)
log.Panic(s.ListenAndServe())
}func getAllUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Helo World")
}func getUserHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.Path)
}func getUserByIDHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.Path)
}func postHandler(w http.ResponseWriter, r *http.Request) {
type User struct {
Name string
Age int
}
var u Usererr := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}fmt.Fprintf(w, "User: %+v", u)
}func putHandler(w http.ResponseWriter, r *http.Request) {
type User struct {
Name string
Age int
}
var u Usererr := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}fmt.Fprintf(w, "User: %+v", u)
}```
## Serverless
You can deploy your codes to [google cloud function](https://cloud.google.com/functions). With this approach, you don't call the `ListenAndServe` function again.
```go
package functionimport (
"encoding/json"
"fmt"
"net/http""github.com/fastrodev/server"
)func Main(w http.ResponseWriter, r *http.Request) {
createServer().ServeHTTP(w, r)
}func createServer() *server.Server {
s := server.New()
s.Get("/api/user", getAllUser)
s.Get("/api/user/:name", getUserHandler)
s.Get("/api/account/:id([0-9]+)", getUserByIDHandler)
s.Post("/api/user", postHandler)
s.Put("/api/user", putHandler)
return s
}func getAllUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Helo World")
}func getUserHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.Path)
}func getUserByIDHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, r.URL.Path)
}func postHandler(w http.ResponseWriter, r *http.Request) {
type User struct {
Name string
Age int
}
var u Usererr := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}fmt.Fprintf(w, "User: %+v", u)
}func putHandler(w http.ResponseWriter, r *http.Request) {
type User struct {
Name string
Age int
}
var u Usererr := json.NewDecoder(r.Body).Decode(&u)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}fmt.Fprintf(w, "User: %+v", u)
}```
## Contributing
We appreciate your help! The main purpose of this repository is to improve performance and readability, making it faster and easier to use.[build]: https://github.com/fastrodev/server/actions/workflows/build.yml/badge.svg
[reference]: https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white "reference"
[cov]: https://coveralls.io/repos/github/fastrodev/server/badge.svg?branch=main