Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmijatovic/go-fiber-multithread
Testing fiber api running multiple processes
https://github.com/dmijatovic/go-fiber-multithread
Last synced: 20 days ago
JSON representation
Testing fiber api running multiple processes
- Host: GitHub
- URL: https://github.com/dmijatovic/go-fiber-multithread
- Owner: dmijatovic
- License: apache-2.0
- Created: 2023-09-03T18:47:59.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-03T19:16:22.000Z (over 1 year ago)
- Last Synced: 2024-11-13T16:54:11.846Z (3 months ago)
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fiber basic api
This repo tests basic fiber api setup while learning go at the same time ;-).
Fiber documentation is [here](https://docs.gofiber.io/)
## Initialize Go module
```bash
# initialize new app/module
go mod init fiber-api
```## Install fiber package
```bash
# install fiber package
go get github.com/gofiber/fiber/v2
```## Run fiber using multiple threads
```go
func main() {
// define max number of processes
// by default prefork will spawn process on each processor core
runtime.GOMAXPROCS(4)// create fiber app
app := fiber.New(fiber.Config{
// enable multiple processes to run
Prefork: true,
})// ROUTES
app.Get("/", Home)
app.Get("/data", getData)
app.Post("/data", postData)// start api in separate thread
go startApi(app)// Create channel to signify a signal being sent
c := make(chan os.Signal, 1)
// When an interrupt is sent, notify the channel
signal.Notify(c, os.Interrupt)
// This blocks the main thread until an interrupt is received
_ = <-c// Log closing of app processes
if fiber.IsChild() {
fmt.Println("Gracefully shutting down child process...")
} else {
fmt.Println("Gracefully shutting down parent process...")
}
// SHUTDOWN
_ = app.Shutdown()
}func startApi(app *fiber.App) {
// listen to 8080 port
err := app.Listen(":8080")
if err != nil {
panic(err)
}
}```