-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
executable file
·80 lines (73 loc) · 1.76 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"log"
"mime"
"net/http"
"os"
"github.com/labstack/echo"
_ "github.com/go-sql-driver/mysql"
)
// "ServerTest/trysql"
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
return c.String(http.StatusOK, id)
}
func SendPic(c echo.Context) error {
imgFile, err := os.Open("doityourself.jpg")
if err != nil {
log.Fatal(err)
}
defer imgFile.Close()
fInfo, _ := imgFile.Stat()
var size int64 = fInfo.Size()
buf := make([]byte, size)
fReader := bufio.NewReader(imgFile)
fReader.Read(buf)
avatar := []byte(buf)
return c.Blob(http.StatusOK, mime.TypeByExtension(".jpg"), avatar)
}
func SendMusic(c echo.Context) error {
return c.File("/home/qeek/Desktop/Git/GO1.8/src/ServerTest/Good Dog.mp4")
}
func AttachmentFile(c echo.Context) error {
return c.Attachment("/home/qeek/Desktop/Git/GO1.8/src/ServerTest/Good Dog.mp4", "Sample123.mp3")
}
func RedirectURL(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "More3")
}
func main() {
e := echo.New()
//Home page
e.Static("/", "view/")
//Picture Send
e.GET("More1", SendPic)
e.Static("More2", "doityourself.jpg")
//
e.GET("More3", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
//
e.GET("/users/:id", getUser)
//Json
e.GET("/GetJson", GetJson)
e.GET("/StreamJson", StreamJson)
e.GET("/Prettyprint", Prettyprint)
e.POST("/Theory", Theory)
//Sendfile
e.GET("/SendMusic", SendMusic)
e.GET("/SendFile", AttachmentFile)
/*
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/hello")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%T\n", db)
defer db.Close()
*/
//RedirectURL
e.GET("/RedirectURL", RedirectURL)
e.Logger.Fatal(e.Start(":1323"))
//trysql.Sqltest()
}