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

Hime doesn't have built-in router, you can use any net/http compatible router with hime.

Using net/http ServeMux

package main

import (
	"log"
	"net/http"

	"github.com/moonrhythm/hime"
)

func main() {
	mux := http.NewServeMux()
	mux.Handle("/", hime.Handler(index))
	mux.Handle("/hello/", http.StripPrefix("/hello", hime.Handler(hello)))

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

func index(ctx *hime.Context) error {
	return ctx.String("Hello, Hime!!!")
}

func hello(ctx *hime.Context) error {
	return ctx.String("Hello, %s", ctx.URL.Path[1:])
}
package main

import (
	"log"

	"github.com/moonrhythm/hime"
	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.Handle("/", hime.Handler(index))
	r.Handle("/hello/{name}", hime.Handler(hello))

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

func index(ctx *hime.Context) error {
	return ctx.String("Hello, Hime!!!")
}

func hello(ctx *hime.Context) error {
	name := mux.Vars(ctx.Request)["name"]
	return ctx.String("Hello, %s", name)
}
package main

import (
	"log"
	"net/http"

	"github.com/moonrhythm/hime"
	"github.com/julienschmidt/httprouter"
)

func main() {
	r := httprouter.New()
	r.Handler(http.MethodGet, "/", hime.Handler(index))
	r.Handler(http.MethodGet, "/hello/:name", hime.Handler(hello))

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

func index(ctx *hime.Context) error {
	return ctx.String("Hello, Hime!!!")
}

func hello(ctx *hime.Context) error {
	name := httprouter.ParamsFromContext(ctx).ByName("name")
	return ctx.String("Hello, %s", name)
}

Next, see how to use Template

Clone this wiki locally