示例#1
0
文件: main.go 项目: kellegous/404
func setup(r pork.Router, ctx *context.Context) error {
	root, err := findRoot()
	if err != nil {
		return err
	}

	c := pork.Content(pork.NewConfig(pork.None),
		http.Dir(root))

	// serves up static content
	r.RespondWithFunc("/", func(w pork.ResponseWriter, r *http.Request) {
		_, err := auth.SessionFromRequest(ctx, r)
		if err != nil {
			http.Redirect(w, r, "/auth/a", http.StatusTemporaryRedirect)
			return
		}

		c.ServePork(w, r)
	})

	// some debugging handlers
	r.RespondWithFunc("/info", func(w pork.ResponseWriter, r *http.Request) {
		sess, user, err := auth.UserFromRequest(ctx, r)
		if err != nil {
			panic(err)
		}

		fmt.Fprintln(w, sess, user)
	})

	return nil
}
示例#2
0
文件: serve.go 项目: kellegous/grund
func setup(data string, r pork.Router) {
	r.HandleFunc("/save", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "Invalid", http.StatusMethodNotAllowed)
			return
		}

		var req struct {
			Path string
			Data string
		}

		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			panic(err)
		}

		b, err := base64.StdEncoding.DecodeString(req.Data)
		if err != nil {
			panic(err)
		}

		if err := ioutil.WriteFile(filepath.Join(data, req.Path), b, os.ModePerm); err != nil {
			panic(err)
		}

		res := map[string]interface{}{
			"Error": nil,
		}
		w.Header().Set("Content-Type", "application/json")
		if err := json.NewEncoder(w).Encode(res); err != nil {
			panic(err)
		}
	})
}
示例#3
0
文件: http.go 项目: kellegous/404
func Setup(r pork.Router) {
	r.RespondWithFunc("/debug/goroutine", func(w pork.ResponseWriter, r *http.Request) {
		p := pprof.Lookup("goroutine")
		w.Header().Set("Content-Type", "text/plain;charset=utf-8")
		p.WriteTo(w, 2)
	})
}
示例#4
0
文件: hub.go 项目: kellegous/404
func Setup(r pork.Router, ctx *context.Context) error {
	var h hub

	h.start()

	hand := sockjs.NewHandler("/api/sock", sockjs.DefaultOptions, func(s sockjs.Session) {
		sess, err := authenticate(s, ctx)
		if err != nil {
			accessDenied(s)
			return
		}

		user, err := sess.User(ctx)
		if err != nil {
			accessDenied(s)
			return
		}

		h.enter(s, user)
		defer h.leave(s, user)

		for {
			msg, err := s.Recv()
			if err != nil {
				log.Printf("recv: %s", err)
				return
			}

			if err := h.dispatch(s, user, msg); err != nil {
				log.Printf("disp: %s", err)
				return
			}
		}
	})

	r.RespondWith("/api/sock/", pork.ResponderFor(hand))
	return nil
}
示例#5
0
文件: auth.go 项目: kellegous/404
func Setup(r pork.Router, ctx *context.Context) {
	r.RespondWithFunc("/auth/a", func(w pork.ResponseWriter, r *http.Request) {
		http.Redirect(w, r,
			configFromRequest(ctx.Cfg, r).AuthCodeURL(""),
			http.StatusTemporaryRedirect)
	})

	r.RespondWithFunc("/auth/z", func(w pork.ResponseWriter, r *http.Request) {
		code := r.FormValue("code")
		if code == "" {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		tx := oauth.Transport{
			Config: configFromRequest(ctx.Cfg, r),
		}

		_, err := tx.Exchange(code)
		if err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		var user ghUser
		if err := fetchGhUser(&tx, &user); err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		sess, err := createSessionFrom(ctx, &user, tx.Token)
		if err != nil {
			panic(err)
		}

		if err := setAuthCookie(w, ctx.Cfg, sess); err != nil {
			panic(err)
		}
	})

	r.RespondWithFunc("/auth/sock", func(w pork.ResponseWriter, r *http.Request) {
		sess, err := SessionFromRequest(ctx, r)
		if err != nil {
			http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
			return
		}

		buf, err := secure.Encrypt(sess.Key, ctx.Cfg.AesKey, ctx.Cfg.HmacKey)
		if err != nil {
			panic(err)
		}

		var res bytes.Buffer
		e := base62.NewEncoder(&res)
		if _, err := e.Write(buf); err != nil {
			panic(err)
		}
		e.Close()

		w.Header().Set("Content-Type", "text/plain")
		w.Write(res.Bytes())
	})

	r.RespondWithFunc("/auth/exit", func(w pork.ResponseWriter, r *http.Request) {
		sid, err := SessionIdFromRequest(ctx, r)
		if err != nil {
			panic(err)
		}

		if sid == nil {
			return
		}

		if err := store.DeleteSession(ctx, sid); err != nil {
			panic(err)
		}

		http.SetCookie(w, &http.Cookie{
			Name:     AuthCookieName,
			Value:    "",
			Path:     "/",
			MaxAge:   0,
			HttpOnly: true,
		})
	})
}