Last active
June 3, 2021 00:35
-
-
Save hhatto/3350c5cef6f16e9375ee064493358244 to your computer and use it in GitHub Desktop.
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
module gendoukage | |
go 1.16 | |
require github.com/gorilla/mux v1.8.0 // indirect |
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
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) | |
} |
Author
hhatto
commented
May 25, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment