// GetWebService return a new gin.Engine func GetWebService(store storage.Storage, auth *Authentication) *echo.Echo { log.Printf("[DEBUG] [abraracourcix] Creating web service using %v", store) ws := v1.NewWebService(store) e := echo.New() // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Routes e.Get("/", ws.Help()) e.Get("/:url", ws.Redirect()) e.Get("/api/version", ws.DisplayAPIVersion()) v1 := e.Group("/api/v1") if auth != nil { log.Printf("[INFO] [abraracourcix] Setup secure mode : %v", auth) v1.Use(middleware.BasicAuth(func(user string, pwd string) bool { if user == auth.Username && pwd == auth.Password { return true } return false })) } v1.Get("/urls/:url", ws.URLShow()) v1.Post("/urls", ws.URLCreate()) v1.Get("/stats/:url", ws.URLStats()) return e }
func main() { // Bluemix or local config options -- just local in this repo var port string port = DEFAULT_PORT var host string host = DEFAULT_HOST e := echo.New() // Basic Authentication e.Use(mw.BasicAuth(func(usr, pwd string) bool { if usr == appUser && pwd == appPass { return true } return false })) // Routes e.Post("/send", send) log.Printf("Starting mailservice on %+v:%+v\n", host, port) // Start server e.Run(host + ":" + port) }
func main() { // Echo instance e := echo.New() // Debug mode e.Debug() //------------ // Middleware //------------ // Logger e.Use(mw.Logger()) // Recover e.Use(mw.Recover()) // Basic auth e.Use(mw.BasicAuth(func(usr, pwd string) bool { if usr == "joe" && pwd == "secret" { return true } return false })) // Gzip e.Use(mw.Gzip()) // Routes e.Get("/", hello) // Start server e.Run(":1323") }
func (s *ApiServer) RegisterMiddleware() { //s.Use(mw.Logger()) s.Use(mw.LoggerWithConfig(mw.LoggerConfig{ Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` + `"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` + `"latency_human":"${latency_human}","bytes_in":${bytes_in},` + `"bytes_out":${bytes_out}}` + "\n", Output: golog.GlobalSqlLogger, })) s.Use(mw.Recover()) s.Use(mw.BasicAuth(s.CheckAuth)) }
func echoServer() { e := echo.New() e.Use(middleware.Logger()) e.Post("/rsvp", echo.HandlerFunc(requestAddRsvp)) admin := e.Group("/rsvp") admin.Use(middleware.BasicAuth(checkAuth)) admin.Get("/list", echo.HandlerFunc(requestListRsvp)) admin.Get("/backup", echo.HandlerFunc(requestBoltBackup)) e.Static("/", *rootDir) fmt.Println("Starting Server:", *httpServ) e.Run(standard.New(*httpServ)) }
// AuthMiddleware checks login credentials. func AuthMiddleware(rootUsername, rootPassword string, s store.Store) echo.MiddlewareFunc { return middleware.BasicAuth(func(username, password string) bool { if username == rootUsername { return password == rootPassword } user, err := s.GetUserByUsername(username) if err != nil { log.Error("Failed to retrieve user: ", err) return false } return bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)) == nil }) }
// GetWebService return a new gin.Engine func GetWebService(auth *Authentication) *echo.Echo { log.Printf("[DEBUG] Creating web service") ws := v1.NewWebService() e := echo.New() // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Routes e.Get("/", ws.Help()) e.Get("/api/version", ws.DisplayAPIVersion()) v1 := e.Group("/api/v1") if auth != nil { log.Printf("[INFO] Setup secure mode : %v", auth) v1.Use(middleware.BasicAuth(func(user string, pwd string) bool { if user == auth.Username && pwd == auth.Password { return true } return false })) } return e }
func main() { app := cli.NewApp() app.Usage = "Serve temporary and unimportant files" app.Flags = []cli.Flag{ cli.IntFlag{ Name: "port, p", Value: 8080, Usage: "The port which to listen", }, cli.StringFlag{ Name: "upload-path, u", Value: "uploaded", Usage: "The directory on which to save uploaded data", }, } app.Action = func(c *cli.Context) { uppath.UploadedPath = c.String("upload-path") err := uppath.EnsureDirectories("data", "uploaded") if err != nil { log.Fatal(err) } err = persistence.Init() if err != nil { log.Fatal(err) } defer persistence.Close() vise := echo.New() vise.SetLogPrefix("vise") vise.Use(mw.Logger()) vise.Use(mw.Recover()) apiEndpoint := vise.Group("/api") apiEndpoint.POST("/save", api.SaveFile) apiEndpoint.GET("/links", api.GetLinks) apiEndpoint.GET("/:token/links", api.GetTokenLinks) apiEndpoint.GET("/download/:file", api.DownloadFile) private := vise.Group("/private") private.Use(mw.BasicAuth(func(usr, pwd string) bool { return usr == os.Getenv("VISE_USER") && pwd == os.Getenv("VISE_PWD") })) private.GET("/stats", func(c echo.Context) error { return c.JSON(http.StatusOK, persistence.DbStats()) }) private.GET("/inspect", func(c echo.Context) error { d, err := persistence.Inspect() if err != nil { return err } return c.JSON(http.StatusOK, d) }) vise.Use(mw.Logger()) vise.Use(mw.Recover()) //assets := http.FileServer(assetFS()) //vise.GET("/", assets) //vise.Get("/static/*", assets) destroyer.Scan() port := strconv.Itoa(c.Int("port")) if err != nil { log.Fatal(err) } //log.Fatal(http.ListenAndServe(":"+port, vise)) vise.Run(standard.New(":" + port)) } app.Run(os.Args) }
// Run This starts the api listening on the port supplied func Run(port int, database string) { db, err := models.NewDB(database) if err != nil { log.Panic(err) } env := &Env{db, "me"} // Echo instance e := echo.New() // Gives pretty formatting // e.SetDebug(true) if basicAuth != "" { e.Use(middleware.BasicAuth(func(username, password string) bool { match := fmt.Sprintf("%s:%s", username, password) if match == basicAuth { return true } return false })) } // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Route => handler e.GET("/", func(c echo.Context) error { message := "1, 2, 3. Lets go!" response := &responseMessage{ Message: message, } return c.JSON(http.StatusOK, response) }) e.POST("/alist", func(c echo.Context) error { uuid := getUUID() fmt.Println(uuid) fmt.Println(len(uuid)) message := fmt.Sprintf("I want to upload alist with uuid: %s", uuid) response := &responseMessage{ Message: message, } return c.JSON(http.StatusOK, response) }) e.PUT("/alist/:uuid", func(c echo.Context) error { uuid := c.Param("uuid") message := fmt.Sprintf("I want to alter alist with uuid: %s", uuid) response := &responseMessage{ Message: message, } return c.JSON(http.StatusOK, response) }) e.PATCH("/alist/:uuid", func(c echo.Context) error { uuid := c.Param("uuid") message := fmt.Sprintf("I want to alter alist with uuid: %s", uuid) response := &responseMessage{ Message: message, } return c.JSON(http.StatusOK, response) }) e.GET("/alist/:uuid", func(c echo.Context) error { uuid := c.Param("uuid") alist, err := env.db.GetAlist(uuid) if err != nil { message := fmt.Sprintf("Failed to find alist with uuid: %s", uuid) response := new(responseMessage) response.Message = message return c.JSON(http.StatusBadRequest, *response) } return c.JSON(http.StatusOK, *alist) }) e.GET("/alist/by/:uuid", func(c echo.Context) error { uuid := c.Param("uuid") alists, err := env.db.GetListsBy(uuid) if err != nil { message := fmt.Sprintf("Failed to find all lists.") response := new(responseMessage) response.Message = message return c.JSON(http.StatusBadRequest, *response) } return c.JSON(http.StatusOK, alists) }) // Start server listenOn := fmt.Sprintf(":%d", port) e.Run(standard.New(listenOn)) }
// OnStart ... func (s *Server) OnStart(c *common.Config, e *echo.Echo) error { go func() { s.sessionMgnt.Start() }() e.Use(middleware.BasicAuth(s.Auth)) e.POST("/api/v1/server/tasks", s.CreateTask) e.DELETE("/api/v1/server/tasks/:id", s.CancelTask) e.GET("/api/v1/server/tasks/:id", s.QueryTask) e.POST("/api/v1/server/tasks/status", s.ReportTask) return nil }