Created
June 10, 2015 07:50
-
-
Save flyingzl/9ffb3301ac1e6d9fcb42 to your computer and use it in GitHub Desktop.
golang server and client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"html/template" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
type String string | |
type Struct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, s) | |
} | |
func (s Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
str := fmt.Sprintf("%v%v%v", s.Greeting, s.Punct, s.Who) | |
fmt.Fprint(w, str) | |
} | |
func handlerFunc(w http.ResponseWriter, r *http.Request) { | |
if r.Method == "GET" { | |
t, err := template.ParseFiles("login.tpl") | |
if err != nil { | |
fmt.Fprintln(w, `Can't find template "login.tpl"`) | |
return | |
} | |
t.Execute(w, nil) | |
} else { | |
r.ParseMultipartForm(32 << 20) | |
fmt.Fprintln(w, r.FormValue("name")) | |
file, handler, err := r.FormFile("filename") | |
if err != nil { | |
fmt.Fprintln(w, err) | |
return | |
} | |
defer file.Close() | |
fmt.Fprintln(w, handler.Header) | |
f, err := os.OpenFile("./upload/"+handler.Filename, os.O_CREATE|os.O_WRONLY, 0666) | |
if err != nil { | |
fmt.Fprintln(w, err) | |
return | |
} | |
defer f.Close() | |
io.Copy(f, file) | |
} | |
} | |
func main() { | |
// your http.Handle calls here | |
http.Handle("/string", String("I'm a frayed knot.")) | |
http.Handle("/struct", Struct{"Hello", ":", "Gophers!"}) | |
http.HandleFunc("/", handlerFunc) | |
log.Println("server is running on http://localhost:4000/ ") | |
log.Fatal(http.ListenAndServe("localhost:4000", nil)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"mime/multipart" | |
"net/http" | |
"os" | |
"path" | |
) | |
func postFile(filename string, url string) error { | |
bodyBuffer := &bytes.Buffer{} | |
bodyWriter := multipart.NewWriter(bodyBuffer) | |
fileWriter, err := bodyWriter.CreateFormFile("filename", path.Base(filename)) | |
if err != nil { | |
fmt.Println("error writing to buffer") | |
return err | |
} | |
fh, err := os.Open(filename) | |
if err != nil { | |
fmt.Println("error opening file") | |
return err | |
} | |
defer fh.Close() | |
_, err = io.Copy(fileWriter, fh) | |
if err != nil { | |
return err | |
} | |
contentType := bodyWriter.FormDataContentType() | |
bodyWriter.Close() | |
resp, err := http.Post(url, contentType, bodyBuffer) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
resp_body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return err | |
} | |
fmt.Println(resp.Status) | |
fmt.Println(string(resp_body)) | |
return nil | |
} | |
func main() { | |
url := "http://localhost:4000" | |
file := "C:/Users/Public/Pictures/Sample Pictures/Koala.jpg" | |
postFile(file, url) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>File input</title> | |
</head> | |
<body> | |
<form action="/" method="post" enctype="multipart/form-data"> | |
<label>Name:</label> | |
<input name="name" /> | |
<p></p> | |
<input type="file" name="filename" /> | |
<p> | |
<input type="submit" value="Submit" /> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment