aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYotam Nachum <me@yotam.net>2019-10-26 13:40:38 +0300
committerYotam Nachum <me@yotam.net>2019-10-26 13:40:38 +0300
commitc0098146ccf95b8181b29dcdaea47834f0ccf580 (patch)
tree33cf604f0ac467764e1c576e65fbf71a673fd99c
parentInitial commit (diff)
downloadgo-gemini-c0098146ccf95b8181b29dcdaea47834f0ccf580.tar.gz
go-gemini-c0098146ccf95b8181b29dcdaea47834f0ccf580.zip
Add example server
The example server is a simple server that only have one page (root path) and respond with an hardcoded text.
-rw-r--r--Makefile11
-rw-r--r--cmd/example/example.go38
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)
+ }
+}