Initial Golang version

This commit is contained in:
Dustin Pianalto
2021-07-09 00:45:30 -08:00
parent 04c4ef935f
commit bff0af76c3
11 changed files with 168 additions and 337 deletions
+39
View File
@@ -0,0 +1,39 @@
package rest_server
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/dustinpianalto/snowflake/internal/generator"
)
const (
REST_PORT = ":50052"
)
type snowflake struct {
Id uint64 `json:"id"`
IdStr string `json:"id_str"`
}
func getSnowflake(w http.ResponseWriter, r *http.Request) {
outputChan := make(chan uint64, 1)
defer close(outputChan)
generator.Generator.RequestChan <- outputChan
id := <-outputChan
s := snowflake{
Id: id,
IdStr: strconv.FormatUint(id, 10),
}
json.NewEncoder(w).Encode(s)
}
func RunRESTServer() {
http.HandleFunc("/snowflake", getSnowflake)
log.Printf("REST Server Listening on 0.0.0.0%s", REST_PORT)
log.Fatal(http.ListenAndServe(REST_PORT, nil))
}