๐พ Archived View for source.community โบ ckaznocha โบ gemini โบ blob โบ main โบ doc.go captured on 2022-06-03 at 23:33:07. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2022-01-08)
โก๏ธ Next capture (2023-01-29)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/ckaznocha/gemini.git
View raw contents of /doc.go (main)
โโโโโฎ 1โ /* 2โ Package gemini provides a server implementation for the Gemini protocol. 3โ 4โ This package should feel familiar to people comfortable with Go's net/http 5โ however it differs where it makes sense. 6โ 7โ Handlers receive a context directly rather than attaching it to a request. 8โ 9โ There is no support for starting a server without TLS. 10โ 11โ The ResponseWriter provides methods for handing different response types rather 12โ than a single abstract response. 13โ 14โ An example of a server using this package. The server greets a user, accepting 15โ input from either a prompt or a client cert. The example includes a graceful 16โ shutdown, log handler, and a shutdown func: 17โ package main 18โ 19โ import ( 20โ "context" 21โ "fmt" 22โ "log" 23โ "os" 24โ "os/signal" 25โ 26โ "source.community/ckaznocha/gemini" 27โ ) 28โ 29โ func greetHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) { 30โ var name string 31โ 32โ switch { 33โ case r.Subject != nil: 34โ name = r.Subject.CommonName 35โ case r.URI.Query != "": 36โ name = r.URI.RawQuery 37โ default: 38โ w.Input(ctx, "What is your name?", false) 39โ 40โ return 41โ } 42โ 43โ fmt.Fprintf(w.Success(ctx, ""), "Hello, %s!", name) 44โ } 45โ 46โ func main() { 47โ logger := log.New( 48โ os.Stdout, 49โ "[Gemini Example] ", 50โ log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lmsgprefix|log.Lshortfile, 51โ ) 52โ 53โ logger.Println("Server starting") 54โ 55โ mux := gemini.NewServeMux() 56โ 57โ mux.HandleFunc("/greet", greetHandler) 58โ 59โ s := &gemini.Server{ 60โ Handler: mux, 61โ LogHandler: func(message string, isError bool) { 62โ logger.Printf("gemini server: %s", message) 63โ }, 64โ } 65โ 66โ s.RegisterOnShutdown(func() { 67โ s.LogHandler("shutting down", false) 68โ }) 69โ 70โ ctx, cancel := context.WithCancel(context.Background()) 71โ defer cancel() 72โ 73โ logger.Println("Server started") 74โ 75โ go func() { 76โ defer cancel() 77โ 78โ s.LogHandler("starting", false) 79โ err := s.ListenAndServeTLS("", "testdata/cert.pem", "testdata/key.pem") 80โ s.LogHandler(fmt.Sprintf("exited: %s\n", err), true) 81โ }() 82โ 83โ ctx, stop := signal.NotifyContext(ctx, os.Interrupt) 84โ defer stop() 85โ 86โ <-ctx.Done() 87โ 88โ logger.Println("Shutdown starting") 89โ defer logger.Println("Shutdown complete") 90โ 91โ if err := s.Shutdown(context.Background()); err != nil { 92โ logger.Printf("Error during shutdown: %s\n", err) 93โ } 94โ } 95โ 96โ */ 97โ package gemini โโโโโฏ
ยท ยท ยท
ยฉ 2022 source.community