leesah

package module
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: MIT Imports: 14 Imported by: 0

README

Go LEESAH

Leesah-game er et hendelsedrevet applikasjonsutviklingspill som utfordrer spillerne til å bygge en hendelsedrevet applikasjon. Applikasjonen håndterer forskjellige typer oppgaver som den mottar som hendelser på en Kafka-basert hendelsestrøm. Oppgavene varierer fra veldig enkle til mer komplekse.

Go-bibliotek for å spille LEESAH!

Kom i gang

Det finnes to versjoner av Leesah Game! En hvor man lager en applikasjon som kjører på Nais, og en hvor man spiller lokalt direkte fra terminalen sin. Dette biblioteket kan brukes i begge versjoner, men denne dokumentasjonen dekker kun lokal spilling.

Vi har et eget template-repo som ligger under navikt/leesah-game-template-go for å spille Nais-versjonen.

Sett opp lokalt miljø

Du trenger Go for å bruke biblioteket, sjekk ut go.dev/learn for å komme i gang.

Start med å opprette en katalog leesah-game.

mkdir leesah-game
cd leesah-game
go mod init leesah-game
touch main.go
Installer biblioteket

Det er kun en ekstern avhengighet du trenger, og det er biblioteket go-leesah.

go get github.com/navikt/go-leesah
Hent Kafkasertifakt

Sertifikater for å koble seg på Kafka ligger tilgjengelig på leesah.io/certs, brukernavn er leesah-game, passord bli utdelt.

Du kan også bruke kommandoen nedenfor:

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

Nedenfor er det et fungerende eksempel som du kan lime inn i main.go som svarer på spørsmålet om lagregistrerings med et navn, og en farge (hex-kode):

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{
		// "lagregistrering",
	}

	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 "lagregistrering":
			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 (
	// 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 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"`
}

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

const (
	MessageTypeQuestion MessageType = "SPØRSMÅL"
	MessageTypeAnswer               = "SVAR"
)

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 category, question, and answer format

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 on the NAIS platform

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳