Backend REST API or MCP JSON RPC






package n

import (
    "errors"
    "log"
    "net/http"
)

var page string

func Welcome() {
    log.Println("Hello, Go!")
    //http.Handle("/welcome", http.HandlerFunc(welcome))
    http.HandleFunc("/welcome", welcome)
    e := http.ListenAndServe(":8999", nil)
    if e != nil && !errors.Is(e, http.ErrServerClosed) {
        log.Fatal(e)
    }
}

func welcome(rw http.ResponseWriter, req *http.Request) {
    rw.Write([]byte("Welcome to Norman!"))
    //fmt.Fprint(rw, page)
}


 

package main

import (
    "encoding/json"
    "log"
    "net/http"
)

func main() {

    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(map[string]string{
            "Host":    r.Host,
            "Content": w.Header().Get("Content-Type"),
        })
    })
    e := http.ListenAndServe(":8080", nil)
    if e != nil {
        log.Fatal(e)
    }
}




Nama FunctionVisibilityBisa dipanggil dari...
Welcome() (huruf BESAR)Exported (public)Package luar (misal main bisa panggil n.Welcome())
welcome() (huruf kecil)Unexported (private)Hanya dalam package n saja







package main

import "aws_restart/n"

func main() {
    // Your code here
    n.Welcome()
}

//go run .\a.go
// go run . a
// curl http://localhost:8999/welcome





REST API:


Kelebihan: Simple, standard HTTP methods (GET/POST/PUT/DELETE), caching mudah, stateless, tooling abundant, browser-friendly.


Kekurangan: Overhead HTTP per request, sulit handle complex operations, batch operations rumit, tidak ideal untuk real-time, versioning API lebih kompleks.




package n

import (
    "encoding/json"
    "errors"
    "log"
    "net/http"
    "time"
)

type DateInput struct {
    Date string `json:"date"`
}

var input DateInput

type IntOutput struct {
    Result int64 `json:"result"`
}

var page string

func Welcome() {
    log.Println("Hello, Go!")
    http.Handle("/welcome", http.HandlerFunc(welcome))
    err:= http.ListenAndServe(":8999", nil)
    if err!= nil && !errors.Is(err, http.ErrServerClosed) {
        log.Fatal(err)
    }
}

func welcome(rw http.ResponseWriter, req *http.Request) {
    err := json.NewDecoder(req.Body).Decode(&input)
    if err != nil {
        http.Error(rw, err.Error(), http.StatusBadRequest)
        return
    }

    parsedDate, err := time.Parse("2006-01-02", input.Date)
    if err != nil {
        http.Error(rw, err.Error(), http.StatusBadRequest)
        return
    }

    dayOfYear := parsedDate.Unix()
    output := IntOutput{Result: dayOfYear}
    rw.Header().Set("Content-Type", "application/json")
    json.NewEncoder(rw).Encode(output)
    rw.Write([]byte("Welcome to Just Enough, Norman!"))
    //fmt.Fprint(rw, page)
}

/*
curl -X POST http://localhost:8999/welcome \
  -H "Content-Type: application/json" \
  -d '{"date":"2025-12-16"}'

curl -X POST http://localhost:8999/welcome `
  -H "Content-Type: application/json" `
  -d '{"date":"2025-12-16"}'
*/






MCP JSON-RPC:


Kelebihan: Single endpoint, batch requests supported, lightweight, structured request/response, cocok untuk tool integration dan AI context, better untuk complex operations, ideal untuk streaming data.


Kekurangan: Lebih niche/kurang standard, debugging lebih susah, tidak cache-friendly, tidak langsung browser-compatible, learning curve lebih tinggi.



































Comments