๐Ÿ’พ Archived View for source.community โ€บ ckaznocha โ€บ gemini โ€บ blob โ€บ main โ€บ doc.go captured on 2023-07-10 at 13:28:09. Gemini links have been rewritten to link to archived content

View Raw

More Information

โฌ…๏ธ Previous capture (2023-01-29)

โžก๏ธ Next capture (2024-02-05)

๐Ÿšง View Differences

-=-=-=-=-=-=-

                                                         .
,-. ,-. . . ,-. ,-. ,-.    ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . .
`-. | | | | |   |   |-'    |   | | | | | | | | | | | | | |  | |
`-' `-' `-^ '   `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-|
                                                             /|
                                                            `-'

Profile for ckaznocha

ckaznocha / gemini

git clone https://source.community/ckaznocha/gemini.git

Branches

Log

Tree

/doc.go (main)

โ†‘ /

blob

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โ”‚ 
  18โ”‚ 	package main
  19โ”‚ 
  20โ”‚ 	import (
  21โ”‚ 		"context"
  22โ”‚ 		"fmt"
  23โ”‚ 		"log"
  24โ”‚ 		"os"
  25โ”‚ 		"os/signal"
  26โ”‚ 
  27โ”‚ 		"source.community/ckaznocha/gemini"
  28โ”‚ 	)
  29โ”‚ 
  30โ”‚ 	func greetHandler(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
  31โ”‚ 		var name string
  32โ”‚ 
  33โ”‚ 		switch {
  34โ”‚ 		case r.Subject != nil:
  35โ”‚ 			name = r.Subject.CommonName
  36โ”‚ 		case r.URI.Query != "":
  37โ”‚ 			name = r.URI.RawQuery
  38โ”‚ 		default:
  39โ”‚ 			w.Input(ctx, "What is your name?", false)
  40โ”‚ 
  41โ”‚ 			return
  42โ”‚ 		}
  43โ”‚ 
  44โ”‚ 		fmt.Fprintf(w.Success(ctx, ""), "Hello, %s!", name)
  45โ”‚ 	}
  46โ”‚ 
  47โ”‚ 	func main() {
  48โ”‚ 		logger := log.New(
  49โ”‚ 			os.Stdout,
  50โ”‚ 			"[Gemini Example] ",
  51โ”‚ 			log.LstdFlags|log.LUTC|log.Lmicroseconds|log.Lmsgprefix|log.Lshortfile,
  52โ”‚ 		)
  53โ”‚ 
  54โ”‚ 		logger.Println("Server starting")
  55โ”‚ 
  56โ”‚ 		mux := gemini.NewServeMux()
  57โ”‚ 
  58โ”‚ 		mux.HandleFunc("/greet", greetHandler)
  59โ”‚ 
  60โ”‚ 		s := &gemini.Server{
  61โ”‚ 			Handler: mux,
  62โ”‚ 			LogHandler: func(message string, isError bool) {
  63โ”‚ 				logger.Printf("gemini server: %s", message)
  64โ”‚ 			},
  65โ”‚ 		}
  66โ”‚ 
  67โ”‚ 		s.RegisterOnShutdown(func() {
  68โ”‚ 			s.LogHandler("shutting down", false)
  69โ”‚ 		})
  70โ”‚ 
  71โ”‚ 		ctx, cancel := context.WithCancel(context.Background())
  72โ”‚ 		defer cancel()
  73โ”‚ 
  74โ”‚ 		logger.Println("Server started")
  75โ”‚ 
  76โ”‚ 		go func() {
  77โ”‚ 			defer cancel()
  78โ”‚ 
  79โ”‚ 			s.LogHandler("starting", false)
  80โ”‚ 			err := s.ListenAndServeTLS("", "testdata/cert.pem", "testdata/key.pem")
  81โ”‚ 			s.LogHandler(fmt.Sprintf("exited: %s\n", err), true)
  82โ”‚ 		}()
  83โ”‚ 
  84โ”‚ 		ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
  85โ”‚ 		defer stop()
  86โ”‚ 
  87โ”‚ 		<-ctx.Done()
  88โ”‚ 
  89โ”‚ 		logger.Println("Shutdown starting")
  90โ”‚ 		defer logger.Println("Shutdown complete")
  91โ”‚ 
  92โ”‚ 		if err := s.Shutdown(context.Background()); err != nil {
  93โ”‚ 			logger.Printf("Error during shutdown: %s\n", err)
  94โ”‚ 		}
  95โ”‚ 	}
  96โ”‚ */
  97โ”‚ package gemini
โ”€โ”€โ”€โ”€โ•ฏ

ยท ยท ยท

๐Ÿก Home

FAQs

Privacy Policy

Terms & Conditions

Official Gemlog

info@source.community

ยฉ 2023 source.community