Skip to content
Thanatat Tamtan edited this page Sep 23, 2023 · 8 revisions

Start hime web server

Hime web server can start very easily 😀

package main

import (
	"log"

	"github.com/moonrhythm/hime"
)

func main() {
	app := hime.New()
	app.Address(":8080")
	err := app.ListenAndServe()
	if err != nil {
		log.Fatal(err)
	}
}

Above code will start a web server using http.DefaultServeMux as handler at address :8080

Set handler

package main

import (
	"log"
	"net/http"

	"github.com/moonrhythm/hime"
)

func main() {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, Hime!"))
	})

	app := hime.New()
	app.Handler(h)
	app.Address(":8080")
	err := app.ListenAndServe()
	if err != nil {
		log.Fatal(err)
	}
}

Above code sets h as the handler

Using hime.Handler

You can use net/http handler to work with hime like the above example. But if you want to use hime.Handler for your handler you can use hime.Handler to wrap hime-style handler into http.Handler

package main

import (
	"log"

	"github.com/moonrhythm/hime"
)

func main() {
	h := hime.Handler(func(ctx *hime.Context) error {
		return ctx.String("Hello, Hime!")
	})

	app := hime.New()
	app.Handler(h)
	app.Address(":8080")
	err := app.ListenAndServe()
	if err != nil {
		log.Fatal(err)
	}
}

Next, see how to use Router

Clone this wiki locally