leesah

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2025 License: MIT Imports: 14 Imported by: 0

README

Go LEESAH

Leesah-game is an event-driven application development game that challenges players to build an event-driven application. The application handles different types of tasks that it receives as events on a Kafka-based event stream. The tasks vary from very simple to more complex.

This is the Python library to play Leesah!

Getting started

There are two versions of the the Leesah game! On is for local play, directly in the terminal. While the other is running on the Nais platform, and you learn how to be a developer in Nav and use Nais. This library is used by both versions, but the following documentation is just for local play.

Setting up local environment

You need Go to use this library, check out go.dev/learn to get started with Go.

Start by creating the directory leesah-game.

mkdir leesah-game
cd leesah-game
go mod init leesah-game
touch main.go
Install the library

There is only one dependency you need to play, and that is the go-leesah library.

go get github.com/navikt/go-leesah
Fetch Kafka certificates

You need some certificates to connect to the Kafka cluster, which is available at leesah.io/certs. The username is always leesah-game, and the password will be distributed.

You can also use the one-liner below:

curl -u leesah-game:<see presentation> -o leesah-certs.zip https://leesah.io/certs && unzip leesah-certs.zip

Using the command above you will end up with leesah-certs.yaml in the leesah-game directory you made earlier.

Example code

To make it easy to start we have made a working example that answer the first question, team-registration, with a dummy name and color. All you need to do is update TEAM_NAME and HEX_CODE, and your ready to compete!

Copy the content below into main.go.

package main

import (
	"log/slog"

	"github.com/navikt/go-leesah"
)

// 1. Ensure credential files are in the certs directory
// 2. Set `teamName` to your preferred team name
// 3. Set `teamColor` to your preferred team color

const (
    teamName  = "CHANGE ME"
    teamColor = "CHANGE ME"
)

func main() {
    log := slog.Default()
	ignoredCategories := []string{
		// "team-registration",
	}

	rapid, err := leesah.NewLocalRapid(teamName, log, ignoredCategories)
	if err != nil {
		log.Error("failed to create rapid", "error", err)
		return
	}
	defer rapid.Close()

	for {
		question, err := rapid.GetQuestion()
		if err != nil {
			slog.Error("can't get new question", "error", err)
		}

		var answer string
		switch question.Category {
		case "team-registration":
			answer = handleTeamRegistration(question)
		}

		if answer != "" {
			if err := rapid.Answer(answer); err != nil {
				log.Error("can't post answer", "error", err)
			}
		}
	}
}

func handleTeamRegistration(question leesah.Question) string {
	return teamColor
}
Kjør lokalt

Så kan du kjøre det:

go run .

Documentation

Index

Constants

View Source
const (
	MessageTypeQuestion MessageType = "SPØRSMÅL"
	MessageTypeAnswer   MessageType = "SVAR"
	MessageTypeFeedback MessageType = "KORREKTUR"

	FeedbackTypeCorrect   FeedbackType = "KORREKT"
	FeedbackTypeIncorrect FeedbackType = "FEIL"
)
View Source
const (
	// LeesahTimeformat is Python's time format for Leesah messages, which is a form of RFC3339.
	LeesahTimeformat = "2006-01-02T15:04:05.999999"
)

Variables

This section is empty.

Functions

func NewLocalRapid

func NewLocalRapid(teamName string, log *slog.Logger, ignoredCategories []string) (*rapid, error)

NewLocalRapid creates a new Rapid instance with a local configuration. The local configuration is read from "leesah-certs.yaml". It is used when playing the local edition of Leesah. You can override the path to the local certification by setting the environment variable QUIZ_CERTS. You can also override the topic by setting the environment variable QUIZ_TOPIC, or else the first topic in the file will be used.

func NewRapid

func NewRapid(teamName string, config RapidConfig) (*rapid, error)

NewRapid creates a new Rapid instance with the given configuration. It is used when playing the Nais-edition of Leesah.

Types

type ErrorIncorrectAnswer added in v1.0.0

type ErrorIncorrectAnswer struct {
	// QuestionID is the ID of the question that was answered incorrectly.
	QuestionID string
	Category   string
}

func NewErrorIncorrectAnswer added in v1.0.0

func NewErrorIncorrectAnswer(questionID, category string) *ErrorIncorrectAnswer

func (*ErrorIncorrectAnswer) Error added in v1.0.0

func (e *ErrorIncorrectAnswer) Error() string

type FeedbackMessage added in v1.0.0

type FeedbackMessage struct {
	QuestionID string       `json:"spørsmålId"`
	Feedback   FeedbackType `json:"korrektur"`
	Category   string       `json:"kategori"`
}

FeedbackMessage is a message with just the fields needed when you have received feedback.

type FeedbackType added in v1.0.0

type FeedbackType string

FeedbackType is an enum for the type of feedback in the Leesah Kafka topic.

type Message

type Message struct {
	Answer        string       `json:"svar"`
	Category      string       `json:"kategori"`
	Created       string       `json:"@opprettet"`
	AnswerID      string       `json:"svarId"`
	Question      string       `json:"spørsmål,omitempty"`
	QuestionID    string       `json:"spørsmålId"`
	AnswerFormat  string       `json:"svarformat,omitempty"`
	TeamName      string       `json:"lagnavn"`
	Type          MessageType  `json:"@event_name"`
	Documentation string       `json:"dokumentasjon"`
	Feedback      FeedbackType `json:"korrektur,omitempty"`
}

Message is a struct that represents a message in the Leesah Kafka topic.

func (Message) ToQuestion

func (m Message) ToQuestion() Question

ToQuestion converts a Message to a simpler Question model.

type MessageType

type MessageType string

MessageType is an enum for the type of message in the Leesah Kafka topic.

type Question

type Question struct {
	ID            string
	Category      string
	Question      string
	AnswerFormat  string
	Documentation string
}

Question is a simplified version of a Message with just the fields you need to play the game.

type RapidConfig

type RapidConfig struct {
	Broker            string
	CAPath            string
	CertPath          string
	GroupID           string
	IgnoredCategories []string
	KafkaDir          string
	Log               *slog.Logger
	PrivateKeyPath    string
	Topic             string
}

RapidConfig is a struct that represents the configuration for a Rapid instance. This is used when creating a new Rapid instance for the NAIS platform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL