Skip to content

Commit

Permalink
Implement a basic API
Browse files Browse the repository at this point in the history
The API endpoint is started along with the webserver.  Documentation for
it hasn't been written yet.
  • Loading branch information
zorchenhimer committed Jul 3, 2021
1 parent 52c1f02 commit 2570046
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ string.
# TODO

- Make the webpage look less terrible.
- Write an API for the server.
- Write some documentation for the API.
- Write a twitter bot that uses the above API.

# License
Expand Down
88 changes: 88 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
package api

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

"github.com/zorchenhimer/hacker-quotes"
)

type Api struct {
hq hacker.HackerQuotes
}

type Response struct {
Quotes []string
Error string
}

func New(hq hacker.HackerQuotes) (*Api, error) {
return &Api{hq: hq}, nil
}

func (a *Api) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/api") {
http.NotFound(w, r)
return
}

var err error
resp := &Response{Quotes:[]string{}}
f := r.URL.Query().Get("format")
c := r.URL.Query().Get("count")
count := 1

if c != "" {
count, err = strconv.Atoi(c)
if err != nil {
fmt.Println("[API] Error parsing count %q: %s", c, err)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"Quotes":[],"Error":"Invalid value for 'count'."}`))
return
}
}

var str string
for i := 0; i < count; i++ {
if f != "" {
str, err = a.hq.HackThis(f)
} else {
str, err = a.hq.Hack()
}

if err != nil {
handleBackendError(w, err)
return
}

resp.Quotes = append(resp.Quotes, str)
}

j, err := json.Marshal(resp)
if err != nil {
fmt.Println("[API] Unable to marshal response:", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf(`{"Quotes":[],"Error":"Something went wrong :C"}`)))
return
}

w.Write(j)
return
}

func handleBackendError(w http.ResponseWriter, err error) {
resp := &Response{Error: err.Error()}
j, merr := json.Marshal(resp)
if merr != nil {
fmt.Println("[API] Unable to marshal error:", merr, "; Original error:", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf(`{"Quotes":[],"Error":"Something went wrong :C"}`, merr, err)))
return
}

fmt.Println("[API] Error:", err)
w.WriteHeader(http.StatusBadRequest)
w.Write(j)
return
}
9 changes: 8 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/zorchenhimer/hacker-quotes"
"github.com/zorchenhimer/hacker-quotes/api"
"github.com/zorchenhimer/hacker-quotes/database"
"github.com/zorchenhimer/hacker-quotes/frontend"
)
Expand Down Expand Up @@ -62,8 +63,14 @@ func main() {
os.Exit(1)
}

a, err := api.New(hack)
if err != nil {
fmt.Printf("Unable to load api: %s\n", err)
os.Exit(1)
}

mux := http.NewServeMux()
//mux.Handle("/api", api)
mux.Handle("/api", a)
mux.Handle("/", web)

hs := &http.Server{
Expand Down

0 comments on commit 2570046

Please sign in to comment.