Esempio n. 1
0
// Register creates routes for each static resource
func Register(config Config, r *pat.Router) {
	//log.Debug("registering not found handler for static package", nil)
	//r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
	//	render.HTML(w, http.StatusNotFound, "error", render.DefaultVars(req, map[string]interface{}{"error": "Page not found"}))
	//})

	log.Printf("registering static content handlers for static package\n")
	for _, file := range config.AssetNames()() {
		if strings.HasPrefix(file, "static/") {
			path := strings.TrimPrefix(file, "static")
			log.Printf("registering handler for static asset %s\n", path)

			b, err := config.Asset()("static" + path)
			if err != nil {
				panic(err)
			}

			mimeType := http.DetectContentType(b)

			log.Printf("using mime type %s\n", mimeType)

			r.Path(path).Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
				w.Header().Set("Content-Type", mimeType)
				w.Header().Set("Cache-control", "public, max-age=259200")
				w.WriteHeader(200)
				w.Write(b)
			})
		}
	}
}
Esempio n. 2
0
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
	web := &Web{
		config: cfg,
		asset:  asset,
	}

	pat.Path("/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
	pat.Path("/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
	pat.Path("/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
	pat.Path("/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
	pat.Path("/").Methods("GET").HandlerFunc(web.Index())

	return web
}
Esempio n. 3
0
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
	web := &Web{
		config: cfg,
		asset:  asset,
	}

	WebPath = cfg.WebPath

	log.Printf("Serving under http://%s%s/", cfg.UIBindAddr, WebPath)

	pat.Path(WebPath + "/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
	pat.Path(WebPath + "/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
	pat.Path(WebPath + "/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
	pat.Path(WebPath + "/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
	pat.StrictSlash(true).Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index())

	return web
}
Esempio n. 4
0
// Register creates routes for each static resource
func Register(config render.Config, r *pat.Router) {
	log.Debug("registering not found handler for static package", nil)
	r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		render.HTML(w, http.StatusNotFound, "error", render.DefaultVars(req, map[string]interface{}{"error": "Page not found"}))
	})

	log.Debug("registering static content handlers for static package", nil)
	for _, file := range config.AssetNames()() {
		if strings.HasPrefix(file, "static/") {
			path := strings.TrimPrefix(file, "static")
			log.Trace("registering handler for static asset", log.Data{"path": path})

			var mimeType string
			switch {
			case strings.HasSuffix(path, ".css"):
				mimeType = "text/css"
			case strings.HasSuffix(path, ".js"):
				mimeType = "application/javascript"
			default:
				mimeType = mime.TypeByExtension(path)
			}

			log.Trace("using mime type", log.Data{"type": mimeType})

			r.Path(path).Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
				if b, err := config.Asset()("static" + path); err == nil {
					w.Header().Set("Content-Type", mimeType)
					w.Header().Set("Cache-control", "public, max-age=259200")
					w.WriteHeader(200)
					w.Write(b)
					return
				}
				// This should never happen!
				log.ErrorR(req, errors.New("it happened ¯\\_(ツ)_/¯"), nil)
				r.NotFoundHandler.ServeHTTP(w, req)
			})
		}
	}
}
Esempio n. 5
0
func CreateAPIv1(conf *config.Config, r *pat.Router) *APIv1 {
	log.Println("Creating API v1")
	apiv1 := &APIv1{
		config: conf,
	}

	stream = goose.NewEventStream()

	r.Path("/api/v1/messages").Methods("GET").HandlerFunc(apiv1.messages)
	r.Path("/api/v1/messages").Methods("DELETE").HandlerFunc(apiv1.delete_all)
	r.Path("/api/v1/messages").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}").Methods("GET").HandlerFunc(apiv1.message)
	r.Path("/api/v1/messages/{id}").Methods("DELETE").HandlerFunc(apiv1.delete_one)
	r.Path("/api/v1/messages/{id}").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/download").Methods("GET").HandlerFunc(apiv1.download)
	r.Path("/api/v1/messages/{id}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("GET").HandlerFunc(apiv1.download_part)
	r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/release").Methods("POST").HandlerFunc(apiv1.release_one)
	r.Path("/api/v1/messages/{id}/release").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/events").Methods("GET").HandlerFunc(apiv1.eventstream)
	r.Path("/api/v1/events").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	go func() {
		for {
			select {
			case msg := <-apiv1.config.MessageChan:
				log.Println("Got message in APIv1 event stream")
				bytes, _ := json.MarshalIndent(msg, "", "  ")
				json := string(bytes)
				log.Printf("Sending content: %s\n", json)
				apiv1.broadcast(json)
			}
		}
	}()

	return apiv1
}
Esempio n. 6
0
// Register registers a healthcheck route with the router
func Register(r *pat.Router, path string, healthy func() bool) {
	r.Path(path).Methods("GET").HandlerFunc(Handler(healthy))
}
Esempio n. 7
0
func CreateAPIv2(conf *config.Config, r *pat.Router) *APIv2 {
	log.Println("Creating API v2")
	apiv2 := &APIv2{
		config: conf,
	}

	r.Path("/api/v2/messages").Methods("GET").HandlerFunc(apiv2.messages)
	r.Path("/api/v2/messages").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)

	r.Path("/api/v2/search").Methods("GET").HandlerFunc(apiv2.search)
	r.Path("/api/v2/search").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)

	r.Path("/api/v2/jim").Methods("GET").HandlerFunc(apiv2.jim)
	r.Path("/api/v2/jim").Methods("POST").HandlerFunc(apiv2.createJim)
	r.Path("/api/v2/jim").Methods("PUT").HandlerFunc(apiv2.updateJim)
	r.Path("/api/v2/jim").Methods("DELETE").HandlerFunc(apiv2.deleteJim)
	r.Path("/api/v2/jim").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)

	r.Path("/api/v2/outgoing-smtp").Methods("GET").HandlerFunc(apiv2.listOutgoingSMTP)
	r.Path("/api/v2/outgoing-smtp").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)

	return apiv2
}