-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The API endpoint is started along with the webserver. Documentation for it hasn't been written yet.
- Loading branch information
1 parent
52c1f02
commit 2570046
Showing
3 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters