u := config.AuthCodeURL(oauthState)
prepares the URL, a call to http.Redirect()
accepts u
and the server redirects the browser).r.FormValue("code")
) via the redirection URL provided in step 1.Download the oauth2 library for import.
go get golang.org/x/oauth2
The app structure I followed is pretty simple:
main.go
use net/http
to start a servermain.go
package main
import (
"fmt"
"log"
"net/http"
"foundry-automations/handlers"
)
func main() {
server := &http.Server{
Addr: fmt.Sprintf(":8000"),
Handler: handlers.New(),
}
log.Printf("Starting HTTP Server. Listening at %q", server.Addr)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Printf("%v", err)
} else {
log.Println("Server closed!")
}
}
/handlers
and add two .go filesbase.go
<service>.go
where <service>
is a meaningful representation of the authorization server you are relying on (e.g., google apis)In base.go
create func New() http.Handler {}
and set up a mux
with two handlers, "/auth/<service>/login"
and "/auth/<service>/callback"
, then return mux
. The helper functions passed as the second argument of mux.HandleFunc
will live in your <service>.go
implementation
base.go
package handlers
import (
{
"net/http"
)
func New() http.Handler {
mux := http.NewServeMux()
// Root
mux.Handle("/", http.FileServer(http.Dir("templates/")))
// // OauthGoogle
// mux.HandleFunc("/auth/google/login", oauthGoogleLogin)
// mux.HandleFunc("/auth/google/callback", oauthGoogleCallback)
mux.HandleFunc("/auth/pco/login", pcoLogin)
mux.HandleFunc("/auth/pco/callback", pcoCallback)
return mux
}
}