-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.36 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
package main
import (
"fmt"
"net/http"
"sync"
"demosearchengine/handlers"
)
// Metadata struct defines the expected structure for the API.
type Metadata struct {
ID string `json:"id"`
Title string `yaml:"title"`
Version string `yaml:"version"`
Maintainers []struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
} `yaml:"maintainers"`
Company string `yaml:"company"`
Website string `yaml:"website"`
Source string `yaml:"source"`
License string `yaml:"license"`
Description string `yaml:"description"`
}
// Using sync.Map instead of a slice to improve thread safety.
var database sync.Map
func main() {
// Initialize a sync.Map instance
var database sync.Map
// Create a closure handler function that passes the database to persistMetadata
persistHandler := func(w http.ResponseWriter, r *http.Request) {
handlers.PersistMetadata(&database, w, r)
}
// Create a closure handler function that passes the database to searchMetadata
searchHandler := func(w http.ResponseWriter, r *http.Request) {
handlers.SearchMetadata(&database, w, r)
}
http.HandleFunc("/metadata", persistHandler)
http.HandleFunc("/search", searchHandler)
fmt.Println("Server is running on http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}