-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (80 loc) · 1.55 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"
"html/template"
"io"
"log"
"net/http"
"os"
"github.com/gernest/utron"
"github.com/gernest/utron/controller"
"github.com/gernest/utron/router"
)
type indexView struct {
t *template.Template
}
func newIndexView(file string) (*indexView, error) {
t, err := template.ParseFiles(file)
if err != nil {
return nil, err
}
return &indexView{t: t}, nil
}
func (v indexView) Render(out io.Writer, name string, data interface{}) error {
return v.t.ExecuteTemplate(out, name, data)
}
type Upload struct {
controller.BaseController
Routes []string
}
func (u *Upload) Index() {
u.Ctx.Template = "index.html"
}
//Save saves the uploaded file
func (u *Upload) Save() {
r := u.Ctx.Request()
err := r.ParseMultipartForm(15485760)
if err != nil {
fmt.Println(err)
return
}
f, h, err := r.FormFile("upload")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
o, err := os.OpenFile(h.Filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Println(err)
return
}
defer o.Close()
io.Copy(o, f)
fmt.Println("OK")
}
func NewUpload() controller.Controller {
return &Upload{
Routes: []string{
"get;/;Index",
"post;/upload;Save",
"get;/delete/{id};Delete",
},
}
}
func main() {
app := utron.NewApp()
v, err := newIndexView("index.html")
if err != nil {
app.Log.Errors(err)
return
}
app.View = v
app.Router.Options = &router.Options{
View: v,
}
app.Router.Add(NewUpload)
port := ":8090"
app.Log.Info("staring server on port", port)
log.Fatal(http.ListenAndServe(port, app))
}