This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/httpserver/httpserver.go

66 lines
1.5 KiB
Go

package httpserver
import (
"log"
"net/http"
"path/filepath"
"text/template"
"git.netflux.io/rob/elon-eats-my-tweets/config"
"github.com/labstack/echo/v4"
"golang.org/x/oauth2"
)
type handler struct {
oauth2Config *oauth2.Config
}
func newHandler(cfg config.Config) *handler {
return &handler{
&oauth2.Config{
ClientID: cfg.Twitter.ClientID,
ClientSecret: cfg.Twitter.ClientSecret,
RedirectURL: cfg.Twitter.CallbackURL,
Scopes: []string{"tweet.read", "tweet.write", "users.read", "offline.access"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://twitter.com/i/oauth2/authorize",
TokenURL: "https://twitter.com/i/oauth2/token",
},
},
}
}
func (h *handler) getIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index", nil)
}
func (h *handler) getLogin(c echo.Context) error {
url := h.oauth2Config.AuthCodeURL(
// TODO: implement state token
"state",
oauth2.SetAuthURLParam("code_challenge", "challenge"),
oauth2.SetAuthURLParam("code_challenge_method", "plain"),
)
return c.Redirect(http.StatusTemporaryRedirect, url)
}
func (h *handler) getCallback(c echo.Context) error {
log.Printf("got params = %+v", c.QueryParams())
return c.String(http.StatusOK, "Received response")
}
func Start(cfg config.Config) error {
e := echo.New()
e.Renderer = &Template{
templates: template.Must(template.ParseGlob(filepath.Join(cfg.PublicPath, "views/", "*.html"))),
}
h := newHandler(cfg)
e.GET("/", h.getIndex)
e.GET("/login", h.getLogin)
e.GET("/callback", h.getCallback)
return e.Start(":8000")
}