In this pages, we will learn how to use Mahakam in your project.
Installation
To install Mahakam, you need at least Go 1.18 installed on your machine.
go get -u github.com/seiortech/mahakam
Basic Usage
Mahakam is a HTTP Framework for go written over netpoll. Designed to be used with net/http package. It’s mean you can use it with net/http package or just use our own http server.
Using mahakam package
Here is the minimal code to start a server using Mahakam http server.
// main.go
package main
import (
"log"
"net/http"
"github.com/seiortech/mahakam"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
mux.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "Hello, JSON!"}`))
})
s := mahakam.NewServer(":8080", mux)
if err := s.ListenAndServe(); err != nil {
log.Fatalln("Failed to start server:", err)
}
}
Using net/http package
Since Mahakam implements the http.Handler
interface, you can use it with net/http package. You can still use any features of mahakam with net/http package.
// main.go
package main
import (
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
mux.HandleFunc("/json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"message": "Hello, JSON!"}`))
})
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatalln("Failed to start server:", err)
}
}
What’s next?
Now you know how to use Mahakam in your project. You can see another example in Mahakam repository