-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
73 lines (60 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
const (
port = "8081"
path = "http://localhost:" + port
)
func serveOpenAPIYAML(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "openapi.yaml")
}
func serveManifest(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, ".well-known/ai-plugin.json")
}
func RootHandler(w http.ResponseWriter, r *http.Request) {
response := map[string]string{"message": "Welcome! To proceed please choose path: /hi or /pi"}
json.NewEncoder(w).Encode(response)
}
func hiHandler(w http.ResponseWriter, r *http.Request) {
hiMap := map[string]string{
"English": "Hi",
"Thai": "สวัสดี",
"Chinese": "你好",
"Arabic": "مرحبا",
"Hebrew": "שלום",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(hiMap)
}
func piHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "3.1415926535897932384")
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
file, err := os.Open("image.jpg")
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
return
}
http.ServeContent(w, r, "image.jpg", stat.ModTime(), file)
}
func main() {
http.HandleFunc("/.well-known/ai-plugin.json", serveManifest)
http.HandleFunc("/openapi.yaml", serveOpenAPIYAML)
http.HandleFunc("/hi", hiHandler)
http.HandleFunc("/pi", piHandler)
http.HandleFunc("/image", imageHandler)
http.HandleFunc("/", RootHandler)
fmt.Printf("Server is running on %s\n", path)
log.Fatal(http.ListenAndServe(":"+port, nil))
}