Example rpc for initializer.RegisterHttp

func GetLeaderBoardData(w http.ResponseWriter, r *http.Request) {
	body, err := io.ReadAll(r.Body)

	if err != nil {
		return
	}

//How?
	runtime.NakamaModule.LeaderboardRecordsList(, )
}

:tv: Media:

Example rpc for initializer.RegisterHttp doesn’t exist. How exactly can I use it?

type HttpHandler struct {
	Context      context.Context
	NakamaModule runtime.NakamaModule
	Logger       runtime.Logger
	DB           *sql.DB
}

func (h *HttpHandler) GetLeaderBoardData(w http.ResponseWriter, r *http.Request) {}

That’s how I solved it. Is there a way to use a handler without making it like this?

Hello @hjs0n,

RegisterHttp is meant for public (no auth) APIs, custom callback handlers or anything that requires lower-level control over the http request flow. We don’t yet have detailed documentation on this but there’s plenty of resources that cover this topic online as it follows the standard Go Http handler interface.

If you do not require the above, the standard way to have custom APIs in Nakama is using RegisterRpc.

If this is not what you’re looking for, please clarify what you’re trying to achieve.

You can also have a look at our template project to get started.

Best.

Hello, @sesposito

What I want is to get data such as storage, leaderboard, etc. using rpc without authentication. I thought registerHttp could use it like that. If I use registerRpc, I have to create an account for dummy, but I don’t want to. Is there any other way?

And it works well with the code above, is there a problem? If there is a problem, please let me know another way.

Ok if you’d like to bypass Nakama standard auth then yeah, RegisterHttp is the way to go, I see no issue with the approach I just wanted to make sure you’re aware that the endpoints will be publicly accessible.

type HttpHandler struct {
	NakamaModule runtime.NakamaModule
	Logger       runtime.Logger
}

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	logger.Info("Hello World!")

	handler := &HttpHandler{
		NakamaModule: nk,
		Logger:       logger,
	}
}

Is there any problem with creating global structure instances when doing InitModule like above, except for having open access to endpoints?
I really don’t know if that’s the best way.

There shouldn’t be an issue, if you want to avoid the global struct you can use a closure to pass the required dependencies to the handlers.

Ok, Thank you for your response.