Initial Commit
This commit is contained in:
commit
a4cfebfbdf
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
*~
|
||||
*.bak
|
||||
examples/hello/hello
|
||||
examples/client/client
|
||||
examples/fileserver/fileserver
|
60
README.md
Normal file
60
README.md
Normal file
@ -0,0 +1,60 @@
|
||||
# Gopher protocol library for Golang
|
||||
|
||||
This is a standards compliant Gopher library for the Go programming language
|
||||
implementing the RFC 1436 specification. The library includes both client and
|
||||
server handling and examples of each.
|
||||
|
||||
## Installation
|
||||
|
||||
$ go get github.com/prologic/go-gopher
|
||||
|
||||
## Usage
|
||||
|
||||
```#!go
|
||||
import "github.com/prologic/go-gopher"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
### Client
|
||||
|
||||
```#!go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
)
|
||||
|
||||
func main() {
|
||||
res, _ := gopher.Get("gopher://gopher.floodgap.com/")
|
||||
bytes, _ = res.Dir.ToText()
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
```
|
||||
|
||||
### Server
|
||||
|
||||
```#!go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
)
|
||||
|
||||
func hello(w gopher.ResponseWriter, r *gopher.Request) {
|
||||
w.WriteInfo("Hello World!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
gopher.HandleFunc("/hello", hello)
|
||||
log.Fatal(gopher.ListenAndServe("localhost:70", nil))
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
56
examples/client/main.go
Normal file
56
examples/client/main.go
Normal file
@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
)
|
||||
|
||||
var (
|
||||
json = flag.Bool("json", false, "display gopher directory as JSON")
|
||||
)
|
||||
|
||||
func main() {
|
||||
var uri string
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if len(flag.Args()) == 1 {
|
||||
uri = flag.Arg(0)
|
||||
} else {
|
||||
uri = "gopher://gopher.floodgap.com/"
|
||||
}
|
||||
|
||||
res, err := gopher.Get(uri)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if res.Body != nil {
|
||||
contents, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Print(contents)
|
||||
} else {
|
||||
var (
|
||||
bytes []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if *json {
|
||||
bytes, err = res.Dir.ToJSON()
|
||||
} else {
|
||||
bytes, err = res.Dir.ToText()
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
}
|
29
examples/fileserver/main.go
Normal file
29
examples/fileserver/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
)
|
||||
|
||||
func cwd() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
root = flag.String("root", cwd(), "root directory to serve")
|
||||
)
|
||||
|
||||
flag.Parse()
|
||||
|
||||
gopher.Handle("/", gopher.FileServer(gopher.Dir(*root)))
|
||||
|
||||
log.Fatal(gopher.ListenAndServe("localhost:70", nil))
|
||||
}
|
27
examples/hello/main.go
Normal file
27
examples/hello/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
)
|
||||
|
||||
func index(w gopher.ResponseWriter, r *gopher.Request) {
|
||||
w.WriteItem(
|
||||
gopher.Item{
|
||||
Type: gopher.FILE,
|
||||
Selector: "/hello",
|
||||
Description: "hello",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func hello(w gopher.ResponseWriter, r *gopher.Request) {
|
||||
w.WriteInfo("Hello World!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
gopher.HandleFunc("/", index)
|
||||
gopher.HandleFunc("/hello", hello)
|
||||
log.Fatal(gopher.ListenAndServe("localhost:70", nil))
|
||||
}
|
Loading…
Reference in New Issue
Block a user