Skip to content

Instantly share code, notes, and snippets.

@cheikhsimsol
cheikhsimsol / class_routing.go
Last active February 3, 2024 06:39
Classic routing
message := "Hello World"
// Map a route to handle requests to the root path ("/")
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// get code
fmt.Fprint(w, message)
case http.MethodPost:
// patch Code
message := "Hello World"
r := gin.Default()
r.GET("/", func(c *gin.Context){
c.JSON(...)
})
mux := http.NewServeMux()
// Map a route to handle requests to the root path ("/")
mux.HandleFunc("GET 127.0.0.1/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
})
mux.HandleFunc("GET localhost/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Bonjour")
})
package main
import (
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
package main
...
func main() {
mux := http.NewServeMux()
// Map a route to handle requests to the root path ("/")
mux.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) {
...
func main() {
mux := http.NewServeMux()
conf := &oauth2.Config{
ClientID: "RANDOM",
ClientSecret: "RANDOM",
Scopes: []string{"offline_access"},
Endpoint: oauth2.Endpoint{
TokenURL: "https://oauth.wiremockapi.cloud/oauth/token",
mux.HandleFunc("GET /consume", func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
code := r.FormValue("code")
state := r.FormValue("state")
if state != "state" {
http.Error(w, "Error, mismatched state", http.StatusBadRequest)
return
}
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"golang.org/x/oauth2"
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func launchSecondServer() {
target, _ := url.Parse("http://localhost:8081/base/")
// Create a reverse proxy
proxy := &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
log.Println("Forwarding:", r.In.URL.String())
r.SetURL(target)
r.Out.Host = r.In.Host
},
}