Created
March 14, 2021 07:09
-
-
Save pottava/140b19cb533199a30288cd79d0651063 to your computer and use it in GitHub Desktop.
ojichat の API 化
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 ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"github.com/greymd/ojichat/generator" | |
) | |
func handler(w http.ResponseWriter, r *http.Request) { | |
config := generator.Config{TargetName: "Cloud Run", EmojiNum: 4} | |
if names, ok := r.URL.Query()["name"]; ok && len(names[0]) > 0 { | |
config.TargetName = names[0] | |
} | |
message := fmt.Sprintf("Hi %s!", config.TargetName) | |
if candidate, err := generator.Start(config); err == nil { | |
message = candidate | |
} | |
fmt.Fprint(w, message) | |
} | |
func main() { | |
port := "8080" | |
if candidate, found := os.LookupEnv("PORT"); found { | |
port = candidate | |
} | |
http.HandleFunc("/", handler) | |
if err := http.ListenAndServe(":"+port, nil); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment