diff options
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | cmd/example/example.go | 38 |
2 files changed, 49 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ca8bd62 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +all: build + +build: gemini-example + +gemini-example: cmd/example/*.go *.go + go build -o gemini-example git.sr.ht/~yotam/go-gemini/cmd/example + +clean: + rm -rf gemini-example + +.PHONY: all build clean diff --git a/cmd/example/example.go b/cmd/example/example.go new file mode 100644 index 0000000..6e7ece2 --- /dev/null +++ b/cmd/example/example.go @@ -0,0 +1,38 @@ +package main + +import ( + "io/ioutil" + "log" + "net/url" + "strings" + + gemini "git.sr.ht/~yotam/go-gemini" +) + +type ExampleHandler struct { +} + +func (h ExampleHandler) Handle(r gemini.Request) gemini.Response { + u, err := url.Parse(r.URL) + if err != nil { + body := ioutil.NopCloser(strings.NewReader(err.Error())) + return gemini.Response{40, "text/gemini", body} + } + + if u.Path != "/" { + body := ioutil.NopCloser(strings.NewReader("Not Found")) + return gemini.Response{50, "text/gemini", body} + } + + body := ioutil.NopCloser(strings.NewReader("Hello World")) + return gemini.Response{20, "text/gemini", body} +} + +func main() { + handler := ExampleHandler{} + + err := gemini.ListenAndServe(":1965", "server.crt", "server.key", handler) + if err != nil { + log.Fatal(err) + } +} |