-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpserver.go
More file actions
54 lines (48 loc) · 1.71 KB
/
httpserver.go
File metadata and controls
54 lines (48 loc) · 1.71 KB
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
package govuegui
import (
"fmt"
"log"
"net/http"
"github.com/as27/golib/js/vuejsdev"
"github.com/as27/golib/js/vueresourcemin"
"github.com/as27/golib/js/vueroutermin"
"github.com/gorilla/mux"
"github.com/skratchdot/open-golang/open"
)
// DefaultPathPrefix defines the prefix for the all gui specific tasks
var DefaultPathPrefix = "/govuegui"
// DefaultServerPort defines the port of the gui server, when using
// `govuegui.Serve()`
var DefaultServerPort = ":2700"
// NewRouter returns a router from the gorillatoolkit
// http://www.gorillatoolkit.org/pkg/mux
// The router already includes all the paths which are needed
// for the gui. It can be called like:
// r := govuegui.NewRouter()
// // Add you own routes
// r.HandleFunc("/products/{key}", ProductHandler)
func NewRouter(g *Gui) *mux.Router {
r := mux.NewRouter()
r.Handle(g.PathPrefix+"/", g)
r.Handle(g.PathPrefix+"/data", g)
r.Handle(g.PathPrefix+"/data/ws", g)
r.HandleFunc(g.PathPrefix+"/lib/vue.min.js", vuejsdev.Handler)
r.HandleFunc(g.PathPrefix+"/lib/vue-router.min.js", vueroutermin.Handler)
r.HandleFunc(g.PathPrefix+"/lib/vue-resource.min.js", vueresourcemin.Handler)
r.Handle(g.PathPrefix+"/app.css", g)
r.Handle(g.PathPrefix+"/custom.css", g)
r.Handle(g.PathPrefix+"/app.js", g)
// Allow the template to add files
r.Handle(g.PathPrefix+"/files/{filename}", g)
return r
}
// Serve wraps the http.ListenAndServe() function, but adds the
// routes for the gui.
func Serve(g *Gui) error {
r := NewRouter(g)
log.Println("Serving gvg on port: ", g.ServerPort)
gURL := fmt.Sprintf("http://localhost%s%s/", g.ServerPort, g.PathPrefix)
log.Println("Open your browser and go to ", gURL)
open.Run(gURL)
return http.ListenAndServe(g.ServerPort, r)
}