Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,37 @@ o.Get("/favicon.ico", func(c okapi.Context) error {
o.Static("/static", "public/assets")
```

## TLS Server

```go
// Initialize TLS configuration for secure HTTPS connections
tls, err := okapi.LoadTLSConfig("path/to/cert.pem", "path/to/key.pem", "", false)
if err != nil {
panic(fmt.Sprintf("Failed to load TLS configuration: %v", err))
}
// Create a new Okapi instance with default config
// Configured to listen on port 8080 for HTTP connections
o := okapi.Default(okapi.WithAddr(":8080"))

// Configure a secondary HTTPS server listening on port 8443
// This creates both HTTP (8080) and HTTPS (8443) endpoints
o.With(okapi.WithTLSServer(":8443", tls))

// Register application routes and handlers
o.Get("/", func(c okapi.Context) error {
return c.JSON(http.StatusOK, okapi.M{
"message": "Welcome to Okapi!",
"status": "operational",
})
})
// Start the servers
// This will launch both HTTP and HTTPS listeners in separate goroutines
log.Println("Starting server on :8080 (HTTP) and :8443 (HTTPS)")
if err := o.Start(); err != nil {
panic(fmt.Sprintf("Server failed to start: %v", err))
}
```

---

## Contributing
Expand All @@ -320,7 +351,7 @@ Contributions are welcome!
4. Push to your fork
5. Open a Pull Request

---


---
## Give a Star! ⭐
Expand Down
12 changes: 3 additions & 9 deletions examples/group/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,11 @@ func main() {
return c.JSON(http.StatusOK, users)
})
// Get user
v1.Get("/users/:id", func(c okapi.Context) error {
return show(c)
})
v1.Get("/users/:id", show)
// Update user
v1.Put("/users/:id", func(c okapi.Context) error {
return update(c)
})
v1.Put("/users/:id", update)
// Create user
v1.Post("/users", func(c okapi.Context) error {
return store(c)
})
v1.Post("/users", store)

// Create a new group with a base path v2
v2 := api.Group("/v2")
Expand Down
6 changes: 3 additions & 3 deletions examples/middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package main

import (
"errors"
"fmt"
"github.com/jkaninda/okapi"
"log/slog"
Expand Down Expand Up @@ -109,8 +110,7 @@ func adminStore(c okapi.Context) error {
func adminUpdate(c okapi.Context) error {
var newBook Book
if ok, err := c.ShouldBind(&newBook); !ok {
errMessage := fmt.Sprintf("Failed to bind book: %v", err)
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid input " + errMessage})
return c.AbortBadRequest(err)
}
for _, book := range books {
if book.ID == newBook.ID {
Expand All @@ -121,7 +121,7 @@ func adminUpdate(c okapi.Context) error {
return c.JSON(http.StatusOK, book)
}
}
return c.JSON(http.StatusNotFound, okapi.M{"error": "Book not found"})
return c.AbortNotFound(errors.New("book not found"))
}
func index(c okapi.Context) error {
return c.JSON(http.StatusOK, books)
Expand Down
80 changes: 80 additions & 0 deletions examples/tls/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* MIT License
*
* Copyright (c) 2025 Jonas Kaninda
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package main

import (
"fmt"
"github.com/jkaninda/okapi"
"log"
"net/http"
"time"
)

func main() {
// Initialize TLS configuration for secure HTTPS connections
tls, err := okapi.LoadTLSConfig("path/to/cert.pem", "path/to/key.pem", "", false)
if err != nil {
panic(fmt.Sprintf("Failed to load TLS configuration: %v", err))
}

// Create a new Okapi instance with default config
// Configured to listen on port 8080 for HTTP connections
o := okapi.Default(okapi.WithAddr(":8080"))

// Configure a secondary HTTPS server listening on port 8443
// This creates both HTTP (8080) and HTTPS (8443) endpoints
o.With(okapi.WithTLSServer(":8443", tls))

// Register application routes and handlers

o.Get("/", func(c okapi.Context) error {
return c.JSON(http.StatusOK, okapi.M{
"message": "Welcome to Okapi!",
"status": "operational",
})
})

// Example parameterized route demonstrating path variables
o.Get("/greeting/:name", greetingHandler)

// Start the server(s)
// This will launch both HTTP and HTTPS listeners in separate goroutines
log.Println("Starting server on :8080 (HTTP) and :8443 (HTTPS)")
if err := o.Start(); err != nil {
panic(fmt.Sprintf("Server failed to start: %v", err))
}
}

// greetingHandler handles personalized greeting requests
func greetingHandler(c okapi.Context) error {
name := c.Param("name") // Extract name from URL path

// Return personalized greeting as JSON
return c.JSON(http.StatusOK, okapi.M{
"message": fmt.Sprintf("Hello %s!", name),
"timestamp": time.Now().UTC().Format(time.RFC3339),
"user_agent": c.Request.UserAgent(),
})
}
Loading