-
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.
- Loading branch information
1 parent
dd21bc9
commit 01d9c7e
Showing
5 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/peterramaldes/billgo | ||
|
||
go 1.18 |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
func handlers(mux *http.ServeMux) { | ||
mux.Handle("/ping", &pingHandler{}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
func notFound(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte(`{"error": "not found"}`)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
handlers(mux) | ||
http.ListenAndServe(":3000", mux) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
pingRe = regexp.MustCompile(`^\/ping*$`) | ||
) | ||
|
||
type pingHandler struct{} | ||
|
||
func (h *pingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("content-type", "application/json") | ||
switch { | ||
case r.Method == http.MethodGet && pingRe.MatchString(r.URL.Path): | ||
h.Ping(w, r) | ||
return | ||
default: | ||
notFound(w, r) | ||
return | ||
} | ||
} | ||
|
||
func (h *pingHandler) Ping(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"status": "alive"}`)) | ||
} |