Ejemplo n.º 1
0
func main() {
	go func() {
		sigc := make(chan os.Signal)
		signal.Notify(sigc, syscall.SIGUSR1, syscall.SIGHUP)
		defer signal.Stop(sigc)

		log.Debug("updating feeds", "trigger", "init")
		feedreader.Update()

		for {
			select {
			case now := <-time.After(30 * time.Minute):
				log.Debug("updating feeds", "trigger", "clock", "time", now.String())
			case sig := <-sigc:
				log.Debug("updating feeds", "trigger", "signal", "signal", sig.String())
			}
			feedreader.Update()
		}
	}()

	rt := web.NewRouter(web.Routes{
		{"GET", `/`, feedreader.HandleListEntries},
		{"GET", `/sources`, feedreader.HandleListSources},
		{web.AnyMethod, `.*`, feedreader.Handle404},
	})
	if err := http.ListenAndServe(":8000", rt); err != nil {
		log.Error("HTTP server failed", "error", err.Error())
	}
}
Ejemplo n.º 2
0
Archivo: app.go Proyecto: husio/apps
func NewApplication(ctx context.Context) http.Handler {
	statics := http.FileServer(http.Dir("."))
	handleStatics := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		statics.ServeHTTP(w, r)
	}
	handleUI := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "gallery/statics/index.html")
	}

	return &application{
		ctx: ctx,
		rt: web.NewRouter(web.Routes{
			{`/`, web.RedirectHandler("/ui/", http.StatusMovedPermanently), "GET"},
			{`/ui/.*`, handleUI, "GET"},

			{`/api/v1/images`, handleUploadImage, "PUT"},
			{`/api/v1/images`, handleListImages, "GET"},
			{`/api/v1/images/{id}\.jpg`, handleServeImage, "GET"},
			{`/api/v1/images/{id}/tags`, handleTagImage, "PUT"},
			{`/api/v1/images/{id}`, handleImageDetails, "GET"},

			{`.*`, handleStatics, "GET"},

			{`.*`, web.StdJSONHandler(http.StatusNotFound), web.AnyMethod},
		}),
	}
}
Ejemplo n.º 3
0
Archivo: app.go Proyecto: husio/apps
func NewApplication(ctx context.Context) http.Handler {
	return &application{
		ctx: ctx,
		rt: web.NewRouter(web.Routes{
			{"POST", `/`, handleAuthenticate},
			{"GET", `/key/{key-id}`, handlePublicKey},

			{web.AnyMethod, `.*`, web.StdJSONHandler(http.StatusNotFound)},
		}),
	}
}
Ejemplo n.º 4
0
Archivo: urls.go Proyecto: husio/apps
func init() {
	app := application{
		rt: web.NewRouter(web.Routes{
			{"/api/notes", handleListNotes, "GET"},
			{"/api/notes", handleAddNote, "POST"},
			{"/api/notes/{note-id}", handleGetNote, "GET"},
			{"/api/.*", handleApi404, web.AnyMethod},

			{"/", handleIndex, "GET"},
			{"/ui", handleIndex, "GET"},
			{"/login", handleLogin, "GET"},
			{"/.*", handle404, web.AnyMethod},
		}),
	}
	http.Handle("/", &app)
}
Ejemplo n.º 5
0
Archivo: app.go Proyecto: husio/apps
func NewApp(ctx context.Context) http.Handler {
	return &application{
		ctx: ctx,
		rt: web.NewRouter(web.Routes{
			{"GET", `/`, handleListTopics},
			{"GET", `/t`, handleListTopics},
			{"GET", `/t/new`, handleCreateTopic},
			{"POST", `/t/new`, handleCreateTopic},
			{"GET", `/t/{topic-id}`, handleTopicDetails},
			{"POST", `/t/{topic-id}/comment`, handleCreateComment},

			{"GET", `/login`, auth.LoginHandler("google")},
			{"GET", `/login/success`, auth.HandleLoginCallback},

			{web.AnyMethod, `.*`, handle404},
		}),
	}
}
Ejemplo n.º 6
0
	"golang.org/x/net/context"

	"github.com/husio/x/log"
	"github.com/husio/x/web"
)

func main() {
	if err := http.ListenAndServe(":8000", rt); err != nil {
		log.Error("HTTP server error", "error", err.Error())
	}
}

var rt = web.NewRouter(web.Routes{
	{"GET", `/{id}`, handleGetPaste},
	{"POST", `/`, handlePostPaste},
	{"PUT", `/{id}`, handlePutPaste},
	{web.AnyMethod, `.*`, handle404},
})

var db = struct {
	mu  sync.Mutex
	mem map[string][]byte
}{
	mem: make(map[string][]byte),
}

func handle404(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusNotFound)
	fmt.Fprintln(w, "not found")
}
Ejemplo n.º 7
0
Archivo: paste.go Proyecto: husio/apps
	"golang.org/x/oauth2"
	oauth2gh "golang.org/x/oauth2/github"

	"github.com/husio/apps/paste/notes"
	"github.com/husio/x/auth"
	"github.com/husio/x/cache"
	"github.com/husio/x/envconf"
	"github.com/husio/x/storage/pg"
	"github.com/husio/x/tmpl"
	"github.com/husio/x/web"
)

var router = web.NewRouter("", web.Routes{
	web.GET("/login", "login", auth.LoginHandler("github")),
	web.GET("/login/success", "", auth.HandleLoginCallback),

	web.GET("/n/{note-id}", "note-details", notes.HandleDisplayNote),

	web.GET(`/static/.*`, "", handleStaticDir),
})

var statics http.Handler

func handleStaticDir(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	statics.ServeHTTP(w, r)
}

func handleApi404(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	web.StdJSONErr(w, http.StatusNotFound)
}

func main() {