-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (77 loc) · 2.01 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/gin-gonic/gin"
)
var router *gin.Engine
type Page struct{
Title string
Body []byte
}
func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadPage(title string) (*Page,error){
filename := title + ".txt"
body,err := ioutil.ReadFile(filename)
fmt.Println(body)
if err !=nil{
log.Fatal(err)
return nil,err
}
return &Page{Title : title, Body:body},nil
}
func handler(w http.ResponseWriter , r *http.Request){
fmt.Fprintf(w,"Hi there,I love %s",r.URL.Path[1:])
}
func viewHandler(w http.ResponseWriter , r *http.Request){
vars:=mux.Vars(r)
title := vars["fileName"]
p,_ := loadPage(title)
fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
}
func render(c *gin.Context, data gin.H, templateName string) {
switch c.Request.Header.Get("Accept") {
case "application/json":
// Respond with JSON
c.JSON(http.StatusOK, data["payload"])
case "application/xml":
// Respond with XML
c.XML(http.StatusOK, data["payload"])
default:
// Respond with HTML
c.HTML(http.StatusOK, templateName, data)
}
}
func main() {
//p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
//p1.save()
//p2, _ := loadPage("TestPage")
//fmt.Println(string(p2.Body))
//router := mux.NewRouter().StrictSlash(true)
//router.HandleFunc("/", handler)
//router.HandleFunc("/view/{fileName}",viewHandler)
//log.Fatal(http.ListenAndServe(":8000", router))
router = gin.Default()
router.LoadHTMLGlob("templates/*")
initializeRoutes()
// router.GET("/", func(c *gin.Context) {
// // Call the HTML method of the Context to render a template
// c.HTML(
// // Set the HTTP status to 200 (OK)
// http.StatusOK,
// // Use the index.html template
// "index.html",
// // Pass the data that the page uses (in this case, 'title')
// gin.H{
// "title": "Home Page",
// },
// )
// })
router.Run(":2020")
}