Skip to content

Instantly share code, notes, and snippets.

@hhatto
Last active June 3, 2021 00:35
Show Gist options
  • Save hhatto/3350c5cef6f16e9375ee064493358244 to your computer and use it in GitHub Desktop.
Save hhatto/3350c5cef6f16e9375ee064493358244 to your computer and use it in GitHub Desktop.
module gendoukage
go 1.16
require github.com/gorilla/mux v1.8.0 // indirect
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
)
type InfoPayload struct {
Reward string `json:"reward"`
IsRankDown bool `json:"is_rank_down"`
FindAt string `json:"find_at"`
}
type ClaimURLInput struct {
GithubID string `json:"github-id"`
Code string `json:"code"`
}
type ClaimURLPayload struct {
ClaimURL string `json:"claim_url"`
Reward string `json:"reward"`
GithubID string `json:"github_id"`
IsRankDown bool `json:"is_rank_down"`
}
func fetchInfo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(204)
return
}
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
githubID := vars["id"]
var payload *InfoPayload
if githubID == "notreward" {
payload = &InfoPayload{
Reward: "0",
IsRankDown: false,
FindAt: "",
}
} else {
payload = &InfoPayload{
Reward: "100000000000000000000",
IsRankDown: false,
FindAt: "",
}
}
res, _ := json.Marshal(payload)
w.Write(res)
}
func fetchFindClaimUrl(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(204)
return
}
reqBody, _ := ioutil.ReadAll(r.Body)
var input ClaimURLInput
if err := json.Unmarshal(reqBody, &input); err != nil {
log.Fatal(err)
}
payload := ClaimURLPayload{
ClaimURL: "https://dummy.duumy",
Reward: "200000000000000000000",
GithubID: "dummy",
IsRankDown: false,
}
json.NewEncoder(w).Encode(payload)
}
func rootPage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello gendou-kage Server")
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", rootPage)
router.HandleFunc("/v1/info/{id}", fetchInfo).Methods("GET", "OPTIONS")
router.HandleFunc("/v1/findClaimUrl", fetchFindClaimUrl).Methods("POST", "OPTIONS")
http.ListenAndServe(fmt.Sprintf(":%d", 8088), router)
}
@hhatto
Copy link
Author

hhatto commented May 25, 2021

$ go run main.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment