GoCRUD
Hub
goCRUD

Attempt #1 Dynamic Endpoints

The API Server #2

web.dev

This attempt is built with the built in mux router. It seems just simpler than Gorilla and httprouter. This API does not do anything but returning endpoint and value.

I did the versioning by adding a sub domain. I found it cleaner and more isolated than the recommended /v2/endpoint/value/ method. Using sub domains it is a complete different server used. Rather than nested folders. Here is the API server version 2.

package main

import (
  "encoding/json"
  //"fmt"
  "net/http"
  "strings"
)

func main() {
  http.HandleFunc("/", handler)
  http.Handle("/favicon.ico", http.NotFoundHandler())
  http.ListenAndServe(":9999", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {

  w.Header().Set("Access-Control-Allow-Origin", "*")
  w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,CREATE,DELETE")
  w.Header().Set("Access-Control-Allow-Headers", "*")
  w.Header().Set("Content-Type", "application/json")

  switch r.Method {
  case "DELETE":
    Delete(w, r)
  case "POST":
    Create(w, r)
  case "PUT":
    Update(w, r)
  default: //GET
    Select(w, r)
  }
}

func Select(w http.ResponseWriter, r *http.Request) {
  route, val := getpath(r)
  data := map[string]string{"method": "GET", "route": route, "value": val}
  json.NewEncoder(w).Encode(data)
}

func Update(w http.ResponseWriter, r *http.Request) {
  route, val := getpath(r)
  data := map[string]string{"method": "PUT", "route": route, "value": val}
  json.NewEncoder(w).Encode(data)
}

func Create(w http.ResponseWriter, r *http.Request) {
  route, val := getpath(r)
  data := map[string]string{"method": "POST", "route": route, "value": val}
  json.NewEncoder(w).Encode(data)
}

func Delete(w http.ResponseWriter, r *http.Request) {
  route, val := getpath(r)
  data := map[string]string{"method": "DELETE", "route": route, "value": val}
  json.NewEncoder(w).Encode(data)
}

func getpath(r *http.Request) (string, string) {
  path := strings.Split(r.URL.String(), "/")
  switch len(path) {
  case 3:
    return path[1], path[2]
  case 2:
    return path[1], ""
  default:
    return "", ""
  }
}

Create - POST (INSERT)

The endpoints are called from Javascript code via AJAX to avoid flickering and delays. First is the AJAX code for C in the CRUD and then you see the expected result.

var url = "https://api2.go4webdev.org/create";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Accept", "application/json");
xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("create").innerHTML = this.responseText
   }};
xhr.send();
Expected result "create"


Read - GET (SELECT)

The endpoints are called from Javascript code via AJAX to avoid flickering and delays. First is the AJAX code for the CRUD and then you see the expected result.

var url = "https://api2.go4webdev.org/test/1";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("Accept", "application/json");

xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("put").innerHTML = this.responseText
   }};

xhr.send();
Expected result "read + 1"


Update - PUT (UPDATE)

The endpoints are called from Javascript code via AJAX to avoid flickering and delays. Third is the AJAX code for the U in CRUD (Update) and then you see the expected result.

var url = "https://api2.go4webdev.org/update/1";

var xhr = new XMLHttpRequest();
xhr.open("PUT", url);
xhr.setRequestHeader("Accept", "application/json");

xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("update").innerHTML = this.responseText
   }};

xhr.send();
Expected result "update + 1"


Delete - DELETE (DELETE)

The endpoints are called from Javascript code via AJAX to avoid flickering and delays. Last is the AJAX code for D in CRUD (delete) and then you see the expected result.

var url = "https://api2.go4webdev.org/delete/1";

var xhr = new XMLHttpRequest();
xhr.open("DELETE", url);
xhr.setRequestHeader("Accept", "application/json");

xhr.onload = function () {
   if (xhr.readyState === 4) {
      document.getElementById("del").innerHTML = this.responseText
   }};

xhr.send();
Expected result "delete + 1"