restfulApiServer

command module
v0.0.0-...-f6ea6d1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 4 Imported by: 0

README

Notes

(A very rough version of gibberish notes)

  • To implement a basic server, I used the net/http package. The skeleton for a basic server must have a function which
  1. handles the request a given pattern in the server and redirects to a function.
// This was done with the help of http.HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
	
//Example:

// HandleFunc registers the handler function for the given pattern in the DefaultServeMux.
http.HandleFunc("/", office)

//and here office is a handler function of type handler func(http.ResponseWriter, *http.Request)

//Example:
func office(responseWriter http.ResponseWriter, request *http.Request) {
// Fprintf(w io.Writer, format string, a ...any) (n int, err error)
// Fprintf formats according to a format specifier and writes to w.
// It returns the number of bytes written and any write error encountered.
fmt.Fprintf(responseWriter, "Homepage live: Where APIs flow smoother than Michael's awkward office moments!")
}
  1. listen and serves to an address.
// ListenAndServe(addr string, handler http.Handler) error
// ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
// The handler is typically nil, in which case the DefaultServeMux is used.
// ListenAndServe always returns a non-nil error.

http.ListenAndServe("127.0.0.1:8080", nil) // Using DefaultServeMux, hence sending nil for second parameters
  • To write respond back to the client, we create a new encode which takes a writer as argument and the encode the data (characters in this case).
// NewEncoder(w io.Writer) *json.Encoder
// NewEncoder returns a new encoder that writes to w.
jsonEncoder := json.NewEncoder(responseWriter)

// Encode(v any) error
// Encode writes the JSON encoding of v to the stream, followed by a newline character.
jsonEncoder.Encode(characters)

Next, used gorilla mux as it provides extended support than net/http package. Syntax is similar except we can extend out HandleFunc to handle particular http verbs. Example: httpRouter.HandleFunc("/", office).Methods("GET")

Trying to invoke with POST verb for that endpoint would return 405Method Not Allowed.

The HandleFunc would work even without .Methods but wouldn't be able to handle the http verb filtering.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL