Giter Site home page Giter Site logo

learning-go's Introduction

A Simple Go API with a Wine Dataset

Available Endpoints

  • GET /status
    curl -X GET -H "Content-Type: application/json" http://localhost:8080/status
    
  • GET /wine
    • Optional query params start & count for pagination
    curl -X GET -H "Content-Type: application/json" "http://localhost:8080/wine?start=0&count=10"
    
  • GET /wine/[id]
    curl -X GET -H "Content-Type: application/json" http://localhost:8080/wine/100000
    
  • PUT /wine
    • Body should contain json formatted wine obj minus the id
    curl -X cation/json" -d '{"country":"US","description":"this is a wine","designation":"delicious","points":"104.9","price":"a lot","province":"here","region_1":"north","region_2":"carolina","taster_name":"Jazz Jackrabbit","taster_twitter_handle":"@jazzjackrabbit","title":"new wine","variety":"the spice of life","winery":"martha's vineyard"}' http://localhost:8080/wine```
    
    

Build and run

Just run docker build in the source directory.

docker build -t go-wine .

Then run the container with the desired port binding.

docker run -p 8080:8080 --rm go-wine

My Thoughts

This was my first experience with Go, so most of the time spent went into getting familiar with the language and its paradigms. I feel like I managed to take advantage of goroutines and channels to avoid race conditions (multiple http handlers/threads incrementing counters, modifying the records) in an idiomatic way. But by sharing a channel to a single state manager among the http handlers (it will block access to all synchronized state on every msg), I made things far slower than necessary. I'd fix that with with mutliple channels or something with more time. But the server should avoid race conditions and I avoided falling back on mutexes. This was my first attempt at "sharing memory by communicating"

I added receivers to the handler funcs to make them methods on a struct containing the state I needed to get into them. Being new to Go, this felt like such an odd way to get state into a function. But at least it wasn't global. The way Go's methods & function receivers work is really unique. I didn't go looking for reasons to use interfaces or type assertions.

If I had more time, I definitely would have modularized the project. I also might have found a real persistence solution for the data. Using Docker compose and throwing a postgres container into the mix would work, but ideally I'd target something on Azure/AWS and include a Terraform module.

learning-go's People

Contributors

jobennc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.