Skip to content

Instantly share code, notes, and snippets.

@pottava
Created March 14, 2021 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pottava/140b19cb533199a30288cd79d0651063 to your computer and use it in GitHub Desktop.
Save pottava/140b19cb533199a30288cd79d0651063 to your computer and use it in GitHub Desktop.
ojichat の API 化
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