Coder Social home page Coder Social logo

fission-restapi-sample's Introduction

RESTful API in Golang

In this sample, we create a simple guestbook application to demonstrates how to create a set of CURD RESTful APIs with fission function.

Installation

  1. Add the cockroachdb helm chart
helm repo add cockroachdb https://charts.cockroachdb.com/
helm repo update
  1. Install cockroachdb operator using helm
helm install my-release --values cockroachdb.yaml cockroachdb/cockroachdb
  1. Zip the files in the rest-api folder
zip -j restapi-go-pkg.zip rest-api/*
  1. Use fission spec to create application
fission spec apply
  1. Check application status

After fission spec apply, you can check that functions, http triggers and package are successfully created.

$ fission spec list
Functions:
NAME           ENV EXECUTORTYPE MINSCALE MAXSCALE MINCPU MAXCPU MINMEMORY MAXMEMORY TARGETCPU SECRETS CONFIGMAPS
restapi-delete go  newdeploy    1        3        0      0      0         0         80                
restapi-get    go  newdeploy    1        3        0      0      0         0         80                
restapi-post   go  newdeploy    1        3        0      0      0         0         80                
restapi-update go  newdeploy    1        3        0      0      0         0         80                

Environments:
NAME IMAGE               BUILDER_IMAGE           POOLSIZE MINCPU MAXCPU MINMEMORY MAXMEMORY EXTNET GRACETIME
go   fission/go-env-1.16 fission/go-builder-1.16 3        0      0      0         0         false  0

Packages:
NAME           BUILD_STATUS ENV LASTUPDATEDAT
restapi-go-pkg succeeded    go  04 Mar 22 14:31 IST

HTTP Triggers:
NAME        METHOD   URL                             FUNCTION(s)    INGRESS HOST PATH                            TLS ANNOTATIONS
restdelete  [DELETE] /guestbook/messages/{id:[0-9]+} restapi-delete false   *    /guestbook/messages/{id:[0-9]+}     
restget     [GET]    /guestbook/messages/            restapi-get    false   *    /guestbook/messages/                
restgetpart [GET]    /guestbook/messages/{id:[0-9]+} restapi-get    false   *    /guestbook/messages/{id:[0-9]+}     
restpost    [POST]   /guestbook/messages             restapi-post   false   *    /guestbook/messages                 
restupdate  [PUT]    /guestbook/messages/{id:[0-9]+} restapi-update false   *    /guestbook/messages/{id:[0-9]+}     

If the build status of the package shows failed, try rebuilding it.

fission pkg rebuild --name restapi-go-pkg

Usage

  1. Create a post
$ curl -v -X POST \
    http://${FISSION_ROUTER}/guestbook/messages \
    -H 'Content-Type: application/json' \
    -d '{"message": "hello world!"}'
  1. Get all posts/single post
curl -v -X GET http://${FISSION_ROUTER}/guestbook/messages/

You should see a list posts are returned.

[
    {
        "id": 366739357484417025,
        "message": "hello world!",
        "timestamp": 1531990369
    },
    {
        "id": 366739413774237697,
        "message": "hello world!",
        "timestamp": 1531990387
    },
    {
        "id": 366739416644550657,
        "message": "hello world!",
        "timestamp": 1531990399
    }
]

Now, let's try to get a single post with post id.

curl -v -X GET http://${FISSION_ROUTER}/guestbook/messages/366456868654284801

Or, you can try to get the messages in a specific time range with start and end.

curl -X GET 'http://${FISSION_ROUTER}/guestbook/messages/?start=1531990369&end=1531990387'
  1. Update post
$ curl -v -X PUT \
    http://${FISSION_ROUTER}/guestbook/messages/366456868654284801 \
    -H 'Content-Type: application/json' \
    -d '{"message": "hello world again!"}'
  1. Delete post
$ curl -X DELETE \
    http://${FISSION_ROUTER}/guestbook/messages/366456868654284801 \
    -H 'Cache-Control: no-cache'

Spec generation

fission spec init
fission env create --name go --image fission/go-env-1.16 --builder fission/go-builder-1.16 --spec
fission pkg create --name restapi-go-pkg --src restapi-go-pkg.zip --env go --spec
fission fn create --name restapi-delete --executortype newdeploy --maxscale 3 --env go --pkg restapi-go-pkg --entrypoint MessageDeleteHandler --spec
fission fn create --name restapi-update --executortype newdeploy --maxscale 3 --env go --pkg restapi-go-pkg --entrypoint MessageUpdateHandler --spec
fission fn create --name restapi-post --executortype newdeploy --maxscale 3 --env go --pkg restapi-go-pkg --entrypoint MessagePostHandler --spec
fission fn create --name restapi-get --executortype newdeploy --maxscale 3 --env go --pkg restapi-go-pkg --entrypoint MessageGetHandler --spec
fission httptrigger create --url /guestbook/messages --method POST --function restapi-post --spec --name restpost
fission httptrigger create --url "/guestbook/messages/{id:[0-9]+}" --method PUT --function restapi-update --spec --name restupdate
fission httptrigger create --url "/guestbook/messages/{id:[0-9]+}" --method GET --function restapi-get --spec --name restgetpart
fission httptrigger create --url "/guestbook/messages/{id:[0-9]+}" --method DELETE --function restapi-delete --spec --name restdelete
fission httptrigger create --url "/guestbook/messages/" --method GET --function restapi-get --spec --name restget

fission-restapi-sample's People

Contributors

blackfly19 avatar life1347 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.