Skip to content

Commit

Permalink
Merge pull request #3 from shogo82148/minimum-implementation-of-http-api
Browse files Browse the repository at this point in the history
implement minimum http api
  • Loading branch information
shogo82148 authored Aug 31, 2021
2 parents f4c118d + c2b2206 commit 4242d56
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
11 changes: 10 additions & 1 deletion provider/github-app-token/cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package main

import (
"net/http"

githubapptoken "github.com/shogo82148/actions-github-app-token/provider/github-app-token"
"github.com/shogo82148/ridgenative"
)

func main() {
// TODO: implement me!
h := githubapptoken.NewHandler()
http.Handle("/", h)
ridgenative.ListenAndServe(":8080", nil)
}
99 changes: 99 additions & 0 deletions provider/github-app-token/github-app-token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package githubapptoken

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
)

type Handler struct{}

func NewHandler() *Handler {
return &Handler{}
}

type requestBody struct {
GitHubToken string `json:"github_token"`
}

type responseBody struct {
Message string `json:"message,omitempty"`
Warning string `json:"warning,omitempty"`
}

type errorResponseBody struct {
Message string `json:"message"`
}

type validationError struct {
message string
}

func (err *validationError) Error() string {
return err.message
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

data, err := io.ReadAll(r.Body)
if err != nil {
h.handleError(w, r, fmt.Errorf("failed to read the request body: %w", err))
return
}
var payload *requestBody
if err := json.Unmarshal(data, &payload); err != nil {
h.handleError(w, r, &validationError{
message: fmt.Sprintf("failed to unmarshal the request body: %v", err),
})
return
}

resp, err := h.handle(ctx, payload)
if err != nil {
h.handleError(w, r, err)
return
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("failed to write the response: %v", err)
}
}

func (h *Handler) handle(ctx context.Context, req *requestBody) (*responseBody, error) {
return &responseBody{}, nil
}

func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) {
log.Println(err)
status := http.StatusInternalServerError
var body *errorResponseBody

var validation *validationError
if errors.As(err, &validation) {
status = http.StatusBadRequest
body = &errorResponseBody{
Message: validation.message,
}
}

if body == nil {
body = &errorResponseBody{
Message: "Internal Server Error",
}
}
data, err := json.Marshal(body)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.WriteHeader(status)
w.Write(data)
}
4 changes: 4 additions & 0 deletions provider/github-app-token/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/shogo82148/actions-github-app-token/provider/github-app-token

go 1.17

require github.com/shogo82148/ridgenative v1.1.1

require github.com/aws/aws-lambda-go v1.26.0 // indirect
24 changes: 24 additions & 0 deletions provider/github-app-token/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aws/aws-lambda-go v1.19.1/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/aws/aws-lambda-go v1.26.0 h1:6ujqBpYF7tdZcBvPIccs98SpeGfrt/UOVEiexfNIdHA=
github.com/aws/aws-lambda-go v1.26.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shogo82148/ridgenative v1.1.1 h1:Y52XH1YHOwoK/7/KERkLrrIL4cLIizi3fkqWxc/TVB0=
github.com/shogo82148/ridgenative v1.1.1/go.mod h1:m2Z35nMJAmNkC39D6nBIP9ocVdnD399loEeZwXOhPl8=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4242d56

Please sign in to comment.