leesah

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 13 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.

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-game-cert.ekstern.dev.nav.no/certs, brukernavn og passord skal du få utdelt.

Du kan også bruke kommandoen nedenfor:

wget --user <username> --password <password> -O leesah-creds.zip https://leesah-game-cert.ekstern.dev.nav.no/certs && unzip leesah-creds.zip 
Eksempelkode

Nedenfor er det et fungerende eksempel som svarer på spørsmålet om lagregistrerings med et navn, og en farge (hex-kode):

package main

import (
	"fmt"
	"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() {
	rapid, err := leesah.NewLocalRapid(teamName, slog.Default())
	if err != nil {
		slog.Error("failed to create rapid", "error", err)
		return
	}
	defer rapid.Close()

	if err := rapid.Run(Answer); err != nil {
		slog.Error("failed to run rapid", "error", err)
	}
}

func Answer(question leesah.Question, log *slog.Logger) (string, bool) {
    log.Info(fmt.Sprintf("%+v", question))

	switch question.Category {
	case "team-registration":
		return teamColor, true
	}

	return "", false
}
Kjør lokalt

Først må du sette opp avhengigheten:

go get github.com/navikt/go-leesah

Så kan du kjøre det:

go run .

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Answer       string      `json:"svar"`
	Category     string      `json:"kategorinavn"`
	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"`
}

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

type MessageType

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

	// LeesahTimeformat is Python's time format for Leesah messages, which is a form of RFC3339
	LeesahTimeformat = "2006-01-02T15:04:05.999999"
)

type MinimalMessage

type MinimalMessage struct {
	Type MessageType `json:"@event_name"`
}

MinimalMessage is a simplified version of a Message with just the type

type Question

type Question struct {
	Category     string
	Question     string
	AnswerFormat string
}

Question is a simplified version of a Message with just the category and question

type Rapid

type Rapid struct {
	// contains filtered or unexported fields
}

func NewLocalRapid

func NewLocalRapid(teamName string, log *slog.Logger) (*Rapid, error)

NewLocalRapid creates a new Rapid instance with a local configuration. The local configuration is read from "certs/student-creds.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_CERT. You can also override the topic by setting the environment variable QUIZ_TOPIC.

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.

func (*Rapid) Answer added in v0.3.0

func (r *Rapid) Answer(answer string) error

func (*Rapid) Close

func (r *Rapid) Close()

Close closes the Kafka writer and reader

func (*Rapid) GetQuestion added in v0.3.0

func (r *Rapid) GetQuestion() (Question, error)

type RapidConfig

type RapidConfig struct {
	Broker              string
	Topic               string
	GroupID             string
	KafkaCertPath       string
	KafkaPrivateKeyPath string
	KafkaCAPath         string
	Log                 *slog.Logger
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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