| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package main
- import (
- "fmt"
- "github.com/labstack/echo"
- "github.com/labstack/echo/middleware"
- "golang.org/x/crypto/bcrypt"
- "net/http"
- )
- func serveInterface(address string) {
- e := echo.New()
- e.GET("/", func(c echo.Context) error {
- return c.String(http.StatusOK, "ELEC0017 Project")
- //return c.JSON(http.StatusOK, &config)
- })
- e.GET("/new/admin", func(c echo.Context) error {
- name := c.QueryParam("username")
- pass := c.QueryParam("password")
- if len(name) == 0 {
- return c.String(400, "No username")
- }
- if len(pass) == 0 {
- return c.String(400, "No password")
- }
- hash, err := bcrypt.GenerateFromPassword([]byte(pass), 14)
- if err != nil {
- fmt.Printf("Failed hash password %s\n", err)
- return c.String(400, "Error")
- }
- cx := *config
- cx.Admins[name] = configAdmin{name, hash}
- saveConfig()
- return c.String(200, "OK")
- })
- //g := e.Group("/admin")
- e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
- user, ok := config.Admins[username]
- if !ok {
- return false, nil
- }
- err := bcrypt.CompareHashAndPassword(user.Hash, []byte(password))
- return err == nil, nil
- }))
- e.GET("/admin/config", func(c echo.Context) error {
- return c.JSON(http.StatusOK, &config)
- })
- e.Logger.Fatal(e.Start(address))
- }
|