Skip to content

Instantly share code, notes, and snippets.

@apacheservices68
Last active November 1, 2023 03:08
Show Gist options
  • Save apacheservices68/3f72f7098d354ae0522c478c9e28ee5e to your computer and use it in GitHub Desktop.
Save apacheservices68/3f72f7098d354ae0522c478c9e28ee5e to your computer and use it in GitHub Desktop.

Revisions

  1. apacheservices68 revised this gist Nov 1, 2023. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions ec2.go
    Original file line number Diff line number Diff line change
    @@ -26,9 +26,7 @@ func (e Ec2Info) String() string {
    }

    func main() {
    // METHOD 1
    logs := make(chan string)

    go logLogs(logs)
    handleHello := makeHello(logs)

    @@ -64,7 +62,7 @@ func makeHello(logger chan string) func(http.ResponseWriter, *http.Request) {
    body := make(chan []byte, 1)
    url := "http://169.254.169.254/latest/dynamic/instance-identity/document"
    //url := "http://localhost:8888"
    logger <- r.Host
    logger <- r.RemoteAddr
    go Getter(url, body)
    time.Sleep(time.Millisecond * 10)
    tmp := <-body
    @@ -81,7 +79,7 @@ func makeHello(logger chan string) func(http.ResponseWriter, *http.Request) {

    func logLogs(logger chan string) {
    for item := range logger {
    fmt.Println("1. Item", item)
    fmt.Println("Client IP : ", item)
    }
    }

  2. apacheservices68 created this gist Nov 1, 2023.
    103 changes: 103 additions & 0 deletions ec2.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    package main

    import (
    "encoding/json"
    "fmt"
    "io"
    "log"
    "net/http"
    "time"
    )

    type Ec2Info struct {
    AccountId string `json:"accountId"`
    Architecture string `json:"architecture"`
    AvailabilityZone string `json:"availabilityZone"`
    ImageId string `json:"imageId"`
    InstanceId string `json:"instanceId"`
    InstanceType string `json:"instanceType"`
    PendingTime string `json:"pendingTime"`
    PrivateIp string `json:"privateIp"`
    Region string `json:"region"`
    }

    func (e Ec2Info) String() string {
    return fmt.Sprintf("\n[EC2] Instance Id = %v; Private IP = %d, Region = %v", e.InstanceId, e.PrivateIp, e.Region)
    }

    func main() {
    // METHOD 1
    logs := make(chan string)

    go logLogs(logs)
    handleHello := makeHello(logs)

    // METHOD 2
    passer := &DataPasser{logs: make(chan string)}
    go passer.log()

    http.HandleFunc("/1", handleHello)
    http.HandleFunc("/2", passer.handleHello)
    http.ListenAndServe(":8080", nil)
    }

    func Getter(url string, body chan []byte) {
    resp, err := http.Get(url)
    if err != nil {
    log.Printf("%+v\n", err)
    }
    b, err := io.ReadAll(resp.Body)
    if err != nil {
    log.Printf("%+v\n", err)
    }
    // close response body
    resp.Body.Close()
    // print response body
    log.Printf("%+v\n", string(b))
    body <- b
    }

    // METHOD 1

    func makeHello(logger chan string) func(http.ResponseWriter, *http.Request) {
    return func(w http.ResponseWriter, r *http.Request) {
    body := make(chan []byte, 1)
    url := "http://169.254.169.254/latest/dynamic/instance-identity/document"
    //url := "http://localhost:8888"
    logger <- r.Host
    go Getter(url, body)
    time.Sleep(time.Millisecond * 10)
    tmp := <-body
    ins := Ec2Info{}
    if err := json.Unmarshal(tmp, &ins); err != nil {
    log.Println("Parse json got an error ", err.Error())
    }
    type_ := "application/json"
    w.Header().Set("Content-Type", type_)
    fmt.Fprintf(w, "%v", ins)
    //io.WriteString(w, "Hello world!")
    }
    }

    func logLogs(logger chan string) {
    for item := range logger {
    fmt.Println("1. Item", item)
    }
    }

    // METHOD 2

    type DataPasser struct {
    logs chan string
    }

    func (p *DataPasser) handleHello(w http.ResponseWriter, r *http.Request) {
    p.logs <- r.URL.String()
    io.WriteString(w, "Hello world")
    }

    func (p *DataPasser) log() {
    for item := range p.logs {
    fmt.Println("2. Item", item)
    }
    }