comparison go/server.go @ 12:240752cbe11b

db-upload.(by go)
author pyon@macmini
date Tue, 13 Nov 2018 21:11:20 +0900
parents 82f9af6aa7e4
children f5ffc34f045a
comparison
equal deleted inserted replaced
11:799b6008db8e 12:240752cbe11b
1 /* 1 /*
2 server.go : server-program. 2 server.go : server-program.
3 Version : 1.1 3 Version : 1.3
4 Last Change: 2018-10-29 月 14:58:19. 4 Last Change: 2018-11-13 火 08:19:52.
5 5
6 install to: server_root/ 6 install to: server_root/
7 7
8 server_root/server 8 server_root/server
9 + db/ 9 + db/
30 server_root string 30 server_root string
31 logfile string 31 logfile string
32 ) 32 )
33 33
34 func init() { 34 func init() {
35 version = "1.2" // log version 35 version = "1.3" // manage-db version
36 port = ":3910" 36 port = ":3910"
37 server_root = filepath.Dir( os.Args[0] ) 37 server_root = filepath.Dir( os.Args[0] )
38 logfile = filepath.Join( server_root, "rsearcher.log" ) 38 logfile = filepath.Join( server_root, "rsearcher.log" )
39 } 39 }
40 40
55 55
56 // start Web-server 56 // start Web-server
57 fmt.Println( "server start [", server, "] ( program version", version, ")" ) 57 fmt.Println( "server start [", server, "] ( program version", version, ")" )
58 http.HandleFunc( "/", handler ) 58 http.HandleFunc( "/", handler )
59 http.HandleFunc( "/upload", upload_handler ) 59 http.HandleFunc( "/upload", upload_handler )
60 http.HandleFunc( "/mngdb/", mngdb_handler )
60 log.Fatal( http.ListenAndServe( server, nil ) ) 61 log.Fatal( http.ListenAndServe( server, nil ) )
61 } 62 }
62 63
63 func handler( w http.ResponseWriter, r *http.Request ) { 64 func handler( w http.ResponseWriter, r *http.Request ) {
64 file := filepath.Join( server_root, filepath.FromSlash( r.URL.Path ) ) 65 file := filepath.Join( server_root, filepath.FromSlash( r.URL.Path ) )
96 } 97 }
97 f.Close() 98 f.Close()
98 w.Write( []byte( fmt.Sprintf( "%d bytes are recieved.\n", n ) ) ) 99 w.Write( []byte( fmt.Sprintf( "%d bytes are recieved.\n", n ) ) )
99 } 100 }
100 101
102 func mngdb_handler( w http.ResponseWriter, r *http.Request ) {
103 fmt.Println( "[access]", r.RemoteAddr, "|", time.Now().Format( "2006-01-02 15:04" ), "|", "/mngdb" )
104 write_log( "[access] " + r.RemoteAddr + "manage-db" )
105 db := r.URL.Path[ len( "/mngdb/" ): ]
106
107 file := filepath.Join( server_root, "db", db )
108
109 f, err := os.Create( file )
110 if err != nil {
111 http.NotFound( w, r )
112 return
113 }
114 _, err = io.Copy( f, r.Body )
115 if err != nil {
116 http.NotFound( w, r )
117 return
118 }
119 f.Close()
120 }
121
101 func write_log( msg string ) { 122 func write_log( msg string ) {
102 f, err := os.OpenFile( logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644 ) 123 f, err := os.OpenFile( logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644 )
103 if err != nil { 124 if err != nil {
104 log.Fatal( err ) 125 log.Fatal( err )
105 } 126 }