Mercurial > mercurial > hgweb_lesearcher.cgi
comparison lsserver.go @ 0:615a15029602 default tip
first commit.
author | pyon@macmini |
---|---|
date | Sun, 10 Nov 2019 08:39:41 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:615a15029602 |
---|---|
1 /* | |
2 server.go : server-program. | |
3 Version : 0.1 | |
4 Last Change: 2019-11-08 金 17:47:26. | |
5 */ | |
6 package main | |
7 | |
8 import ( | |
9 "bufio" | |
10 "fmt" | |
11 "log" | |
12 "net/http" | |
13 "os" | |
14 "strconv" | |
15 "strings" | |
16 ) | |
17 | |
18 const ( | |
19 version = "0.1" | |
20 | |
21 userdb = "user.gz.txt" | |
22 hhsdb = "hhsdb.gz.txt" | |
23 indexdb = "indexdb.gz.txt" | |
24 ) | |
25 | |
26 var ( | |
27 server string | |
28 | |
29 userhash map[string]user | |
30 hhshash map[string]hhs | |
31 ) | |
32 | |
33 type user struct { | |
34 id string | |
35 group int | |
36 name string | |
37 } | |
38 | |
39 type hhs struct { | |
40 no string | |
41 name string | |
42 kana string | |
43 addr string | |
44 birth string | |
45 sex string | |
46 dates []string | |
47 } | |
48 | |
49 func (h *hhs) Info() string { | |
50 dates := "" | |
51 for _, d := range h.dates { | |
52 dates += ":" + d | |
53 } | |
54 s := []string{h.name, h.addr, dates} | |
55 return strings.Join(s, ",") | |
56 } | |
57 | |
58 func init() { | |
59 server = "localhost:80" | |
60 } | |
61 | |
62 func main() { | |
63 load_user() | |
64 load_hhs() | |
65 load_index() | |
66 | |
67 // start Web-server | |
68 fmt.Println("Le:Searcher-Server start [", server, "] ( version", version, ")") | |
69 http.HandleFunc("/auth/", auth_handler) | |
70 http.HandleFunc("/search/", search_handler) | |
71 http.HandleFunc("/index/", index_handler) | |
72 log.Fatal(http.ListenAndServe(server, nil)) | |
73 } | |
74 | |
75 // DB-Load Functions | |
76 func load_user() error { | |
77 // userid:n | |
78 userhash = make(map[string]user) | |
79 | |
80 f, err := os.OpenFile(userdb, os.O_RDONLY, 0644) | |
81 if err != nil { | |
82 return err | |
83 } | |
84 scanner := bufio.NewScanner(f) | |
85 for scanner.Scan() { | |
86 s := strings.Split(scanner.Text(), ":") | |
87 g, _ := strconv.Atoi(s[1]) | |
88 u := user{id: s[0], group: g, name: s[2]} | |
89 userhash[s[0]] = u | |
90 } | |
91 if err := scanner.Err(); err != nil { | |
92 fmt.Fprintln(os.Stderr, "reading userdb", err) | |
93 } | |
94 if err := f.Close(); err != nil { | |
95 return err | |
96 } | |
97 return nil | |
98 } | |
99 | |
100 func load_hhs() error { | |
101 hhshash = make(map[string]hhs) | |
102 | |
103 f, err := os.OpenFile(hhsdb, os.O_RDONLY, 0644) | |
104 if err != nil { | |
105 return err | |
106 } | |
107 scanner := bufio.NewScanner(f) | |
108 for scanner.Scan() { | |
109 s := strings.Split(scanner.Text(), ",") | |
110 h := hhs{no: s[0], name: s[1]} | |
111 hhshash[s[0]] = h | |
112 } | |
113 if err := scanner.Err(); err != nil { | |
114 fmt.Fprintln(os.Stderr, "reading hhsdb", err) | |
115 } | |
116 if err := f.Close(); err != nil { | |
117 return err | |
118 } | |
119 return nil | |
120 } | |
121 | |
122 func load_index() error { | |
123 // hhsno:date | |
124 f, err := os.OpenFile(indexdb, os.O_RDONLY, 0644) | |
125 if err != nil { | |
126 return err | |
127 } | |
128 scanner := bufio.NewScanner(f) | |
129 for scanner.Scan() { | |
130 s := strings.Split(scanner.Text(), ":") | |
131 h := hhshash[s[0]] | |
132 h.dates = append(h.dates, s[1]) | |
133 hhshash[s[0]] = h | |
134 } | |
135 if err := scanner.Err(); err != nil { | |
136 fmt.Fprintln(os.Stderr, "reading indexdb", err) | |
137 } | |
138 if err := f.Close(); err != nil { | |
139 return err | |
140 } | |
141 return nil | |
142 } | |
143 | |
144 // HTTP Handlers | |
145 func auth_handler(w http.ResponseWriter, r *http.Request) { | |
146 // Toggle Le:Searcher-Client menu | |
147 // userid : UpdateDB, Index, M&M, BPrint, UserMng | |
148 // 7moon : 1, 1, 1, 1, 1 => manager :0 | |
149 // kaigo : 0, 1, 1, 1, 0 => staff :1 # acl by IPaddress | |
150 // chosha : 0, 0, 0, 0, 0 => chosa :2 | |
151 fmt.Println("auth") | |
152 | |
153 id := r.URL.Path[len("/auth/"):] | |
154 fmt.Println(id) | |
155 g := 0 | |
156 switch g { | |
157 case 0: | |
158 w.Write([]byte("0")) | |
159 case 1: | |
160 w.Write([]byte("1")) | |
161 default: | |
162 w.Write([]byte("2")) | |
163 } | |
164 } | |
165 | |
166 func search_handler(w http.ResponseWriter, r *http.Request) { | |
167 // /search/userid:hhsno:date | |
168 // n=00000000 -> name & address & dates | |
169 // n>00000000 -> tif image | |
170 fmt.Println("search") | |
171 | |
172 p := r.URL.Path[len("/search/"):] | |
173 s := strings.Split(p, ":") | |
174 if false { // invalid user | |
175 http.NotFound(w, r) | |
176 return | |
177 } | |
178 if strings.Compare(s[1], "00000000") == 0 { | |
179 h := hhshash[s[1]] | |
180 w.Write([]byte(h.Info())) | |
181 return | |
182 } | |
183 | |
184 } | |
185 | |
186 func index_handler(w http.ResponseWriter, r *http.Request) { | |
187 fmt.Println("index") | |
188 } | |
189 |